Skip to content

Instantly share code, notes, and snippets.

@alvarezloaiciga
Created October 10, 2013 20:27
Show Gist options
  • Save alvarezloaiciga/6925055 to your computer and use it in GitHub Desktop.
Save alvarezloaiciga/6925055 to your computer and use it in GitHub Desktop.
AFNetworking + OHHTTP Issues
// APTHttpClient
@implementation APTHttpClient
static APTHttpClient *_sharedClient = nil;
static dispatch_once_t onceToken;
+ (instancetype)sharedInstance
{
dispatch_once(&onceToken, ^{
if (!_sharedClient) {
_sharedClient = [[APTHttpClient alloc] initWithBaseURL:[NSURL URLWithString:APRProductionServer]
sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
});
return _sharedClient;
}
@end
// Test file
@implementation APRHttpClientTests {
APTHttpClient *_sut;
}
- (void)setUp
{
[super setUp];
_sut = [APTHttpClient sharedInstance];
}
- (void)tearDown
{
[OHHTTPStubs removeAllStubs];
[super tearDown];
}
- (void)testCanMockARequestToTheServer
{
static const NSTimeInterval kRequestTime = 1.0;
static const NSTimeInterval kResponseTime = 1.0;
NSDictionary *expectedResponseDict = @{@"Success" : @"Yes"};
NSData* expectedResponse = [NSJSONSerialization dataWithJSONObject:expectedResponseDict options:0 error:NULL];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:expectedResponse statusCode:200 headers:@{@"Content-Type" : @"application/json"}]
requestTime:kRequestTime responseTime:kResponseTime];
}];
__block __strong id response = nil;
[_sut GET:@"http://localhost:3333"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
response = responseObject; // keep strong reference
[self notifyAsyncOperationDone];
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
XCTFail(@"Unexpected network failure");
[self notifyAsyncOperationDone];
}];
[self waitForAsyncOperationWithTimeout:100];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment