TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by KITAGAWA Tatsuya.
YTTFakeServer is subClass for NSURLProtocol
. It returns stub HTTP response for NSURLConnection
or NSURLSession
request.
YTTFakeServer provides fake response for HTTP request.
In most simple way to use, you will put bundle in your project, and set configuration with YTTFakeServerConfiguration
.
YTTFakeServer find the response data from bundle by request path, and return response with the data.
You can also set HTTPHeader, HTTPstatus with YTTFakeServerDelegate
.
YTTFakeServer is helper for not only test code. Because it does not need to change the code of request part whether for test or for production, it helps you with mock development of HTTP request.
YTTFakeServer is subClass of NSURLProtocol
. You can alse use it for just mitmProxy with real connection.
To run the example project, clone the repo, and run pod install
from the Example directory first.
First, configure with YTTFakeServerConfiguration
. You can set custom bundle.
[YTTFakeServer configure:^(YTTFakeServerConfiguration *configuration) {
configuration.hosts = @["http://your.host/"];
configuration.delegate = self; // delegate
configuration.delay = 1.0; // delay interval
configuration.resourceBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"YourCustomBundle" ofType:@"bundle"]];
configuration.resourceFileExtension = @"json"; // resource type
}];
For example, if you want to set response json with path like api/foo/bar
,
you just set bundle which has api/foo/bar.json
. It's very simple.
YTTFakeServerConfiguration
has other some options.
schemes
(default are http, https)ignoringFileExtentions
(default are .jpg, .png, .css, .js)cacheStoragePolicy
(default is NSURLCacheStorageNotAllowed
)enableReachabilityCheck
(default is YES)If you use NSURLConnection
, you need to register NSURLProtocol
like this.
[NSURLProtocol registerClass:[YTTFakeServer class]];
If you use NSURLSession
, use NSURLSessionConfiguration
.
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.protocolClasses = @[[YTTFakeServer class]];
YTTFakeServerDelegate
provides API for several usage.
this is custom response sample.
- (YTTFakeServerResponse *)YTTFakeServerClient:(id<NSURLProtocolClient>)client responseForRequest:(NSURLRequest *)request
{
NSString *path = request.URL.path;
if ([path isEqualToString:@"/api/auth"]) {
NSDictionary *param = [NSURLProtocol propertyForKey:@"parameters" inRequest:request];
if (![param[@"password"] isEqualToString:@"1234"]) {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"YTTFakeServer" ofType:@"bundle"]];
NSData *responseData = [[NSData alloc] initWithContentsOfFile:[bundle pathForResource:@"auth_error" ofType:@"json" inDirectory:@"api"]];
return [[YTTFakeServerResponse alloc] initWithURL:request.URL headers:request.allHTTPHeaderFields status:400 responseData:responseData];
}
}
return nil;
}
And more several delegate methods are supported. All methods are optional. Check the sample or document.
If you use NSURLSession, and you want to check HTTPBody, use NSURLProtocol:setProperty:forKey:inRequest
.
Please check the sample and issue of AliSoftware/OHHTTPStubs, issue52
It depends on Reachability with a option.
So if you want to enable it, you also add SystemConfiguration.framework
.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
YTTFakeServer inspired with these articles and libraries.
yatatsu, [email protected]
YTTFakeServer is available under the MIT license. See the LICENSE file for more info.