YTTFakeServer
A stub HTTP response provider for iOS.
Install / Use
/learn @yatatsu/YTTFakeServerREADME
YTTFakeServer
YTTFakeServer is subClass for NSURLProtocol. It returns stub HTTP response for NSURLConnection or NSURLSession request.
Description
Provides stub response
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.
For test or mock development
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.
Real connection
YTTFakeServer is subClass of NSURLProtocol. You can alse use it for just mitmProxy with real connection.
Usage
To run the example project, clone the repo, and run pod install from the Example directory first.
Basic
configure
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 isNSURLCacheStorageNotAllowed)enableReachabilityCheck(default is YES)
register Protocol
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]];
Advanced
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.
NOTE:
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
Requirements
It depends on Reachability with a option.
So if you want to enable it, you also add SystemConfiguration.framework.
Installation
YTTFakeServer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "YTTFakeServer"
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request :D
Acknowledgment
YTTFakeServer inspired with these articles and libraries.
- Infinite Blog - Using NSURLProtocol for Injecting Test Data
- InfiniteLoopDK/ILTesting
- Effective web API testing on iOS
- AliSoftware/OHHTTPStubs
Author
yatatsu, yatatsukitagawa@gmail.com
License
YTTFakeServer is available under the MIT license. See the LICENSE file for more info.
