-
Notifications
You must be signed in to change notification settings - Fork 0
YHVConfiguration
Library is pretty configuration is pretty simple. All configuration can be specified on singleton VCR instance using +setupWithConfiguration: method and adjusted during cassette insertion with +insertCassetteWithConfiguration: method.
Attribute: Required
Reference on path where recorded cassettes is stored. This path will be used to compose path to concrete cassette using cassettePath.
NOTE: It is desirable what this property point to bundle directory (with .bundle extension).
Attribute: Required
Reference on path where cassette is stored or will be stored (relative to cassettesPath).
NOTE: VCR is capable to serialize cassettes using one of supported file types: Property List (cassette path should have plist extension) and JSON (cassette path should have json extension). JSON serializer used by default in case if extension is missing from cassette path.
Reference on object which can be used to filter requests for recording/playback. Object can be array with list of allowed hosts or YHVHostFilterBlock block which allow dynamically decide whether request should be recorded/stub played or not.
// Record only requests sent to apple.com
configuration.hostFilter = @[@"apple.com"];
// Record all requests which has been sent to httpbin service.
configuration.hostsFilter = ^BOOL (NSString *host) {
return [host rangeOfString:@"httpbin"].location != NSNotFound;
};Default: YHVMatcher.method, YHVMatcher.scheme, YHVMatcher.host, YHVMatcher.port, YHVMatcher.path and YHVMatcher.query.
Reference on list of registered matchers which will be used by VCR to identify whether stubbed request can be used instead of original or not.
Available predefined matchers described in YHVMatcher typedef.
Default: YHVRecordOnce.
Recording mode used to figure out whether request can be stored on cassette at this moment or not.
Available record modes described in YHVRecordMode typedef.
Default: YHVChronologicalPlayback.
Playback mode is used to figure out how data should be passed to URL loading system.
Response on request is not single entry in cassette and consist from: response instance (NSHTTPURLResponse) and response body (NSData or NSError in case if error happened during request processing). There can be multiple response body entries on cassette in case if body was too big to send it with single packet.
Sometimes cassette may contain stubs for multiple requests and they randomly (in order of sending) located on cassette's tape. So, there is no guarantee what after one response body packet will be another one for same request. Stubs playback in this case controlled by specified mode.
Available playback modes described in YHVPlaybackMode typedef.
Reference on YHVPathFilterBlock block which allow to filter out sensitive data from request URI path segment, before it will be stored as stub on cassette.
/**
* In example below, we return path segment where any occurrence of our
* username replaced with 'bob'.
*/
configuration.pathFilter = ^NSString *(NSURLRequest *request) {
return [request.URI.path stringByReplacingOccurrencesOfString:@"<username>" withString:@"bob"];
};Reference on object which can be used to filter sensitive data from request URI query segment, before it will be stored as stub on cassette.
Object can be NSDictionary instance where keys represent name of query parameter and value is original data replacement. It is possible to remove query fields with value by specifying [NSNull null] for it in dictionary.
Object also can be YHVQueryParametersFilterBlock block which allow dynamically change query arguments.
/**
* In example below, we remove 'token' query and replace 'signature' with
* own value which will be stored as stub.
*/
configuration.queryParametersFilter = @{ @"token": [NSNull null], @"signature": @"secret-signature" };
// This block is identical to filter configuration with NSDictionary above.
configuration.queryParametersFilter = ^(NSURLRequest *request, NSMutableDictionary *queryParameters) {
[queryParameters removeObjectForKey:@"token"];
queryParameters[@"signature"] = @"secret-signature";
};Reference on object which can be used to filter sensitive data from request POST body, before it will be stored as stub on cassette.
Object can be NSDictionary instance where keys represent name of keys and value is original data replacement. It is possible to remove header fields with value by specifying [NSNull null] for it in dictionary.
NSDictionary can be used only if application/json or application/x-www-form-urlencoded data is sent along with request.
Object also can be YHVPostBodyFilterBlock block which allow dynamically change header fields.
/**
* In example below allow to remove 'fullName' and replace 'pwd' field in
* POST body represented by 'application/json' or
* 'application/x-www-form-urlencoded' content-type.
*/
configuration.postBodyFilter = @{ @"fullName": [NSNull null], @"pwd": @"pwd" };
/**
* In example below, we replace 'sender:alex' string in POST body with
* 'sender:bob' which will be part of stored stub.
*/
configuration.postBodyFilter = ^NSData * (NSURLRequest *request, NSData *body) {
NSString *message = [[NSString alloc] initWithData: body encoding:NSUTF8StringEncoding];
message = [request.URI.path stringByReplacingOccurrencesOfString:@"sender:alex" withString:@"sender:bob"];
return [message dataUsingEncoding:NSUTF8StringEncoding];
};Reference on object which can be used to filter sensitive data from request response body, before it will be stored as stub on cassette.
Object can be NSDictionary instance where keys represent name of header fields and value is original data replacement. It is possible to remove header fields with value by specifying [NSNull null] for it in dictionary.
NSDictionary can be used only if application/json or application/x-www-form-urlencoded data is sent along with request.
Object also can be YHVResponseBodyFilterBlock block which allow dynamically change header fields.
/**
* In example below allow to remove 'token' from response body represented
* by 'application/json' or 'application/x-www-form-urlencoded' content-type.
*/
configuration.responseBodyFilter = @{ @"token": [NSNull null] };
/**
* In example below, we remove 'sender:bob' string from response body before
* it will be stored as stub.
*/
configuration.responseBodyFilter = ^NSData * (NSURLRequest *request, NSHTTPURLResponse *response, NSData *data) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
responseString = [request.URI.path stringByReplacingOccurrencesOfString:@"sender:bob" withString:@""];
return [responseString dataUsingEncoding:NSUTF8StringEncoding];
};Reference on YHVBeforeRecordRequestBlock block which allow to make final adjustments to NSURLRequest instance, before it will be stored as stub on cassette.
/**
* In example below, we change used HTTP method.
*/
configuration.beforeRecordRequest = ^NSURLRequest *(NSURLRequest *request) {
NSMutableURLRequest *changedRequest = [request mutableCopy];
changedRequest.HTTPMethod = @"GET";
return changedRequest;
};Reference on YHVBeforeRecordResponseBlock block which allow to make final adjustments to NSURLRequest instance, before it will be stored as stub on cassette.
/**
* In example below, we remove received service data from stub.
*/
configuration.beforeRecordRequest = ^NSArray * (NSURLRequest *request, NSHTTPURLResponse *response, NSData *data) {
return @[response];
};