VCRURLSession
描述
VCRURLSession
允许您录制测试套件的 HTTP 请求和响应。您可以在未来的测试运行中重播它们,以进行快速、确定性和准确的测试。
要使用 VCRURLSession
,您必须配置您的 NSURLSession
实例
NSURLSession *session = [VCRURLSession prepareURLSession:[NSURLSession sharedSession]];
不涉及 swizzling!
用法
录制
- (void)record
{
// Set up `protocolClasses` and return new session
self.session = [VCRURLSession prepareURLSession:[NSURLSession sharedSession]];
// Create new empty cassette to record HTTP requests on
VCRURLSessionCassette *cassette = [[VCRURLSessionCassette alloc] init];
// Start recording HTTP requests
[VCRURLSession startRecordingOnCassette:cassette];
// Make some HTTP request
[[self.session dataTaskWithURL:[NSURL URLWithString:@"https://www.github.com"]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[VCRURLSession stopRecording];
[cassette writeToFile:self.path];
}] resume];
}
重放
- (void)replay
{
// Set up `protocolClasses` and return new session
self.session = [VCRURLSession prepareURLSession:[NSURLSession sharedSession]];
// Load cassette
VCRURLSessionCassette *cassette = [[VCRURLSessionCassette alloc] initWithContentsOfFile:self.path];
[VCRURLSession startReplayingWithCassette:cassette mode:VCRURLSessionReplayModeStrict];
// Make some HTTP request
[[self.session dataTaskWithURL:[NSURL URLWithString:@"https://www.github.com"]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"%zd - %@", httpResponse.statusCode, httpResponse.URL);
}] resume];
}
功能
重放会消费响应
假设在录制期间对同一资源(GET /users
)进行了多次请求。在重放时,将按相同的顺序消耗它们。
GET /users
200 OK
[]
POST /users?name=John
201 Created
{"id": 1, "name": "John"}
GET /users
200 OK
[{"id": 1, "name": "John"}]
DELETE /users/1
204 No Content
GET /users
200 OK
[]
以 gzip 格式存储
VCRURLSessionCassette *cassette = [[VCRURLSessionCassette alloc] init];
[cassette writeCompressedToFile:@"/tmp/cassette.json.gz"];
返回静态响应
[VCRURLSession setStaticResponseHandler:^VCRURLSessionResponse *_Nullable(NSURLRequest *_Nonnull request) {
NSString *contentType = request.allHTTPHeaderFields[@"Content-Type"];
if ([contentType hasPrefix:@"image/"]) {
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"test_image"]);
return [VCRURLSessionResponse responseWithURL:request.URL statusCode:200 headerFields:nil data:imageData error:nil];
}
return nil;
}];
录制过滤
VCRURLSessionCassette *cassette = [[VCRURLSessionCassette alloc] init];
cassette.recordFilter = ^BOOL(NSURLRequest *request) {
NSString *contentType = request.allHTTPHeaderFields[@"Content-Type"];
// Do not record images
return [contentType hasPrefix:@"image/"];
};
重放速度
录制阶段保存每个请求的响应时间。之后按相同的响应时间返回响应。这可以通过在 cassette 上设置 replaySpeed
属性来更改。
VCRURLSessionCassette *cassette = [[VCRURLSessionCassette alloc] initWithContentsOfFile:self.path];
// If a request took 500ms, now it will only take 50ms
cassette.replaySpeed = 10.0f;
[VCRURLSession startReplayingWithCassette:cassette mode:VCRURLSessionReplayModeStrict];
日志
这将启用记录并回放请求/响应的日志。这仅适用于DEBUG
构建。
[VCRURLSession setLogLevel:VCRURLSessionLogLevelInfo];
许可协议(MIT)
版权所有 (C) 2016 Johannes Plunien
以下条件下,任何人获得本软件和相关文档(“软件”)的副本,有权免费使用该软件而不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供方进行上述操作,前提是:
上述版权声明和本许可协议应当包含在本软件所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的担保,无论是明示的、隐含的还是与特定的用途、无侵权相关的担保。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论此类责任基于合同、侵权或其他原因,从、因或与软件或其使用或其他交易有关。