SWHttpTrafficRecorder AFNetworking 3.0 兼容性

SWHttpTrafficRecorder AFNetworking 3.0 compatibility

我正在使用 OHHTTPStubs 存根 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 流量?

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

https://github.com/Amindv1/SWHttpTrafficRecorder