【问题标题】:SWHttpTrafficRecorder AFNetworking 3.0 compatibilitySWHttpTrafficRecorder AFNetworking 3.0 兼容性
【发布时间】:2016-12-10 00:51:27
【问题描述】:

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

【问题讨论】:

    标签: objective-c afnetworking afnetworking-3


    【解决方案1】:

    对于任何好奇的人来说,它们是不兼容的。如果你想要一个兼容版本的 SWHttpTrafficRecorder,那么你将需要调整 NSURLSessionConfig。我冒昧,这里是一个实现了方法调配的 pod 版本:

    https://github.com/Amindv1/SWHttpTrafficRecorder

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      相关资源
      最近更新 更多