YTTFakeServer 0.1.0

YTTFakeServer 0.1.0

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.

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 is NSURLCacheStorageNotAllowed)
  • 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

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Acknowledgment

YTTFakeServer inspired with these articles and libraries.

Author

yatatsu, [email protected]

License

YTTFakeServer is available under the MIT license. See the LICENSE file for more info.