NLTHTTPStubServer is mocking server. launch simple HTTPServer on testcodes.
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
[NLTHTTPStubServer globalSettings].port = 12345;
server = [[NLTHTTPStubServer stubServer] retain];
[server startServer];
[server stopServer];
[server release];
[[[server stub] forPath:@"/api.json"] andJSONResponse:json];
- JSON
- HTML
- XML
- Plain Text
- Binary
- application/octet-stream
[[[server stub] forPath:@"/api.json"] andStatusCode:200];
[[[server stub] forPath:@"/api.json"] andTimeout];
[[[server stub] forPath:@"api.json"] andCheckURI:^(NSURL *URI) {
// check URI
}];