I'm trying to use OHHTTPStubs in my XCTest class,
This is how I configured OHTTPStubs
in my test file.
//
// Tests Configuration
//
- (void)setUp
{
[super setUp];
_bundle = [NSBundle bundleForClass:[self class]];
[self configureHTTPStubs];
[self installHTTPStubs];
}
- (void)configureHTTPStubs
{
[OHHTTPStubs onStubActivation:^(NSURLRequest *request, id<OHHTTPStubsDescriptor> stub) {
NSLog(@"[OHHTTPStubs] Request to %@ has been stubbed with %@", request.URL, stub.name);
}];
}
- (void)installHTTPStubs
{
HIAPIRequests *requester = [[HIAPIOperator sharedOperator] requester];
[OHHTTPStubs setEnabled:YES forSessionConfiguration:requester.session.configuration];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.path isEqualToString:@"/image_upload"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"image_upload_ws_response.json", nil)
statusCode:201
headers:@{@"Content-Type":@"text/json"}] responseTime:OHHTTPStubsDownloadSpeed3G];
}].name = @"Image Upload OK";
}
//
// In my Requester class this is how I setup the NSURLSession configuration
//
- (void)configureURLSession
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config];
}
And this is how I'm performing a request
- (void)uploadImage:(UIImage *)image
completionBlock:(operationCompletionBlock)completionBlock
progressBlock:(operationProgressBlock)progressBlock
{
NSData *imageData = UIImageJPEGRepresentation(image, 0.80);
NSURLRequest *request = [NSURLRequest requestWithURLString:@"/image_upload"];
NSURLSessionUploadTask *uploadTask = [_session uploadTaskWithRequest:request
fromData:imageData
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
completionBlock(data, error);
}];
[_progressTable setObject:progressBlock forKey:uploadTask];
[uploadTask resume];
}
In the completionHandler
callback I'm basically getting a no domain found error (error NSURLError * domain: @"NSURLErrorDomain" - code: -1003 0x08a70740
) , @"A server with the specified hostname could not be found."
I'm completely sure that I'm querying the correct URL (the one I stubbed with OHHTTPStubs
) in my test.
What could be going on here? Bug maybe?