Skip to content

blazingcloud/NLTHTTPStubServer

 
 

Repository files navigation

NLTHTTPStubServer

NLTHTTPStubServer is mocking server. launch simple HTTPServer on testcodes.

How to install

CocoaPods

Usage

GHUnit and AFNetworking example

@implementation NLTHTTPStubServerWithAFNetwrokingTest

- (void)setUpClass {
    [NLTHTTPStubServer globalSettings].port = 12345;
    server = [[NLTHTTPStubServer stubServer] retain];
    [server startServer];
}

- (void)tearDownClass {
    
    [server stopServer];
    [server release];
}

- (void)setUp {
    [server clear];
}

- (void)tearDown {
    if(![server isStubEmpty]) {
        GHFail(@"stubs not empty");
    }
}

- (void)testJSONResponse {
    
    [[[server stub] forPath:@"/index.json"] andJSONResponseResource:@"test" ofType:@"json"]; // create stub response
    
    [self prepare];
    NSURL *url = [NSURL URLWithString:@"http://localhost:12345/index.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        GHAssertEqualStrings(@"ok", [JSON objectForKey:@"status"], @"status = ok");
        GHAssertEqualStrings(@"json", [JSON objectForKey:@"format"], @"format = json");
        [self notify:kGHUnitWaitStatusSuccess];
    } failure:nil];
    [operation start];
    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0f];
    
}
@end

setup server

[NLTHTTPStubServer globalSettings].port = 12345;
server = [[NLTHTTPStubServer stubServer] retain];
[server startServer];

stop server

[server stopServer];
[server release];

create simple response

[[[server stub] forPath:@"/api.json"] andJSONResponse:json];

support content-types

  • JSON
  • HTML
  • XML
  • Plain Text
  • Binary
    • application/octet-stream

set status code

[[[server stub] forPath:@"/api.json"] andStatusCode:200];

simulate timeout

[[[server stub] forPath:@"/api.json"] andTimeout];

check query

[[[server stub] forPath:@"api.json"] andCheckURI:^(NSURL *URI) {
    // check URI
}];

About

launch simple HTTPServer on testcodes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 99.1%
  • Ruby 0.9%