Fake server for iOS testing.
Objective-C Ruby
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
NLTHTTPStubServer.xcodeproj
NLTHTTPStubServer.xcworkspace
NLTHTTPStubServer
NLTHTTPStubServerTests
.gitignore
CHANGELOG.md
LICENSE
NLTHTTPStubServer.podspec
Podfile
Podfile.lock
README.md

README.md

NLTHTTPStubServer

Japanese

Fake server for iOS testing.

Can register fake response by expect or stub.

[[[server expect] forPath:@"/api/"] andJSONResponseResource:@"fake-response" ofType:@"json"];

Getting Ready

Podfile:

pod 'NLTHTTPStubServer'
  • Import NLTHTTPStubServer.h at top of your TestCase.

First Step

The most simply example with GHUnit async test case. Servers URL is localhost:12345 on default.

- (void)testMostSimply {
    
    // Get shared server instance.
    server = [NLTHTTPStubServer sharedServer];

    // Register fake response for localhost:12345/fake
    NSData *data = [@"RESPONSE" dataUsingEncoding:NSUTF8StringEncoding];
    [[[server expect] forPath:@"/fake"] andPlainResponse:data];
    
    // GHUnit: Setup async test
    [self prepare];

    // Access to localhost:12345/fake
    __weak id that = self;
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:12345/fake"]]
                                       queue:[[NSOperationQueue alloc] init]
                           completionHandler:^(NSURLResponse *res, NSData *data, NSError *err) {

                                // Getting a fake response!
                                GHAssertEqualStrings(toString(data), @"RESPONSE", nil);

                                // GHUnit: notify!
                               [that notify:kGHUnitWaitStatusSuccess];
                           
                           }];
    
    // GHUnit: wait for status...
    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:10];

    // Invoked all expects?
    [server verify];
}

Next Step

Get a server instance and clear

Get shared instance.

NLTHTTPStubServer *server =[NLTHTTPStubServer sharedServer];

Remove all fake responses.

[server clear];

Expecations and verifycation

[[server expect] forPath:@"/fake"];

Register fake response. Server will response this fake if requested /fake. After this setup the functionality under test should be invoked followed by

[server verify];

When expected response has not been invoked, verify method will raise an exception.

Stubs

[[server stub] forPath:@"/fake"]

stub is like expect, But stub is existing if invoked it. verify ignores response that registered by stub.

Features

NLTPath

NLTPath generate complicated path. For example, This request has two GET parameters.

[[server expect] forPath:[NLTPath pathWithPathString:@"/fake" andParameters:@{
        @"k1" : @"v1",
        @"k2" : @"v2",
}]];

This request can matches /fake?k1=v1&k2=v2 or /fake?k2=v2&k1=v1.

anyValue

Can use [NLTPath anyValue] to parameters value.

[[server expect] forPath:[NLTPath pathWithPathString:@"/fake" andParameters:@{
        @"k1" : [NLTPAth anyValue]
}]];

This request can matches /fake?k1=hogeeeeeeee, /fake?k1=fugaaaaaaaaaa and mores.

HTTP Method

[[server stub] forPath:@"/fake" HTTPMethod:@"POST"];

Status code

[[[server stub] forPath:@"/fake"] andStatusCode:200];

Simulate waiting

[[[server stub] forPath:@"/fake"] andProcessingTime:10.0f];

Checking POST body

[[[server expect] forPath:@"/fake" HTTPMethod:@"POST"] andCheckPostBody:^(NSData *postBody) {
        NSString *postBodyString = [that toString:postBody];
        GHAssertEqualStrings(postBodyString, @"POST_BODY", nil);
    }];

Supporting content-types

[[[server expect] forPath:@"/fake"] and{ContentType}Response...]
  • JSON
  • HTML
  • XML
  • Plain Text
  • Binary
    • application/octet-stream