I'm using OHHTTPStubs to stub http requests and I'm trying to use SWHttpTrafficRecorder to record the traffic produced by AFNetworking. For some reason I can't get the traffic recorder to record any traffic produced by my AFNetworking AFHTTPSessionManager. I'm passing in the config and all but it just fails to create any files or recognize that there are any web requests being made. Here's the code for running the recorder:
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);
}
Does anyone know if SWTrafficRecorder and AFNetworking 3.0 are compatible? If so then why aren't my requests being recorder? If not then what other library can I use to record http traffic from AFNetworking?
For anyone that's curious, they're not compatible. If you want a compatible version of SWHttpTrafficRecorder then you're going to need to swizzle the NSURLSessionConfig. I've taken the liberty and here's a version of the pod with method swizzling implemented: