SWHttpTrafficRecorder AFNetworking 3.0兼容性

时间:2016-12-10 00:51:27

标签: objective-c afnetworking afnetworking-3

我正在使用OHHTTPStub来存根http请求,而我正在尝试使用SWHttpTrafficRecorder来记录AFNetworking产生的流量。出于某种原因,我无法让流量记录器记录我的AFNetworking AFHTTPSessionManager产生的任何流量。我正在传递配置,但它只是无法创建任何文件或识别出有任何Web请求。这是运行录像机的代码:

NSError *e;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *bpdDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"BPDTests"];

NSLog(@"setting up traffic recorder");

self.recorder = [SWHttpTrafficRecorder sharedRecorder];
__weak BPDKAPIClient_Tests *weakSelf = self;

// This block determines the name that will be given to the file generated
// by each http request
self.recorder.fileNamingBlock = ^NSString*(NSURLRequest *request, NSURLResponse *response, NSString *defaultName)
{
    NSString *name = [weakSelf fileNameForRequest:request];

    NSLog(@"new name: %@, default name: %@", name, defaultName);

    return name;
};

// This block determines if we will record the http request
self.recorder.recordingTestBlock = ^BOOL(NSURLRequest *request)
{
    NSString *path = [weakSelf filePathForRequest:request];

    NSLog(@"are we deciding to record?");

    (![[NSFileManager defaultManager] fileExistsAtPath:path]) ? NSLog(@"Yes") : NSLog(@"No");

    return ![[NSFileManager defaultManager] fileExistsAtPath:path];
};

// This line forces the singleton configuration to initialize it's session manager and by extension the session
// configuration. This way we can actually pass in the configuration to the recorder
__unused AFHTTPSessionManager *m = self.apiClient.apiClientConfig.httpSessionManager;

NSLog(@"config passed in: %@", self.apiClient.apiClientConfig.httpSessionManagerConfiguration);

[self.recorder startRecordingAtPath:bpdDir
            forSessionConfiguration:self.apiClient.apiClientConfig.httpSessionManagerConfiguration
                              error:&e];

if (e)
{
    NSLog(@"error recording: %@", e);
}

有人知道SWTrafficRecorder和AFNetworking 3.0是否兼容?如果是这样,为什么我的请求不是录音机?如果没有,那么我可以使用哪些其他库来记录来自AFNetworking的HTTP流量?

1 个答案:

答案 0 :(得分:0)

对于任何好奇的人来说,他们都不兼容。如果你想要兼容的SWHttpTrafficRecorder版本,那么你需要调整NSURLSessionConfig。我已经采取了自由,这是一个带有方法调配的pod的版本:

https://github.com/Amindv1/SWHttpTrafficRecorder