-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from luisobo/nsurlsession-hook
[WIP] Hook for NSURLSession
- Loading branch information
Showing
8 changed files
with
141 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// LSNSURLSessionHook.h | ||
// Nocilla | ||
// | ||
// Created by Luis Solano Bonet on 08/01/14. | ||
// Copyright (c) 2014 Luis Solano Bonet. All rights reserved. | ||
// | ||
|
||
#import <Nocilla/Nocilla.h> | ||
|
||
#import "LSHTTPClientHook.h" | ||
|
||
@interface LSNSURLSessionHook : LSHTTPClientHook | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// LSNSURLSessionHook.m | ||
// Nocilla | ||
// | ||
// Created by Luis Solano Bonet on 08/01/14. | ||
// Copyright (c) 2014 Luis Solano Bonet. All rights reserved. | ||
// | ||
|
||
#import "LSNSURLSessionHook.h" | ||
#import "LSHTTPStubURLProtocol.h" | ||
#import <objc/runtime.h> | ||
|
||
@implementation LSNSURLSessionHook | ||
|
||
- (void)load { | ||
[self swizzleSelector:@selector(protocolClasses) fromClass:NSClassFromString(@"__NSCFURLSessionConfiguration") toClass:[self class]]; | ||
} | ||
|
||
- (void)unload { | ||
[self swizzleSelector:@selector(protocolClasses) fromClass:NSClassFromString(@"__NSCFURLSessionConfiguration") toClass:[self class]]; | ||
} | ||
|
||
- (void)swizzleSelector:(SEL)selector fromClass:(Class)original toClass:(Class)stub { | ||
|
||
Method originalMethod = class_getInstanceMethod(original, selector); | ||
Method stubMethod = class_getInstanceMethod(stub, selector); | ||
if (!originalMethod || !stubMethod) { | ||
[NSException raise:NSInternalInconsistencyException format:@"Couldn't load NSURLSession hook."]; | ||
} | ||
method_exchangeImplementations(originalMethod, stubMethod); | ||
} | ||
|
||
- (NSArray *)protocolClasses { | ||
return @[[LSHTTPStubURLProtocol class]]; | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
NocillaTests/Hooks/NSURLSession/NSURLSessionStubbingSpec.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// NSURLSessionStubbingSpec.m | ||
// Nocilla | ||
// | ||
// Created by Luis Solano Bonet on 08/01/14. | ||
// Copyright 2014 Luis Solano Bonet. All rights reserved. | ||
// | ||
|
||
#import "Kiwi.h" | ||
#import "Nocilla.h" | ||
#import "LSHTTPStubURLProtocol.h" | ||
|
||
SPEC_BEGIN(NSURLSessionStubbingSpec) | ||
|
||
beforeEach(^{ | ||
[[LSNocilla sharedInstance] start]; | ||
}); | ||
afterEach(^{ | ||
[[LSNocilla sharedInstance] stop]; | ||
[[LSNocilla sharedInstance] clearStubs]; | ||
}); | ||
|
||
it(@"stubs NSURLSessionDataTask", ^{ | ||
stubRequest(@"GET", @"http://example.com") | ||
.andReturn(200) | ||
.withBody(@"this is a counter example."); | ||
__block BOOL done = NO; | ||
__block NSData *capturedData; | ||
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | ||
|
||
NSURL *url = [NSURL URLWithString:@"http://example.com"]; | ||
NSURLSession *urlsession = [NSURLSession sessionWithConfiguration:configuration]; | ||
NSURLSessionDataTask *datatask = [urlsession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | ||
done = YES; | ||
capturedData = data; | ||
}]; | ||
[datatask resume]; | ||
|
||
|
||
[[expectFutureValue(theValue(done)) shouldEventually] beYes]; | ||
[[[[NSString alloc] initWithData:capturedData encoding:NSUTF8StringEncoding] should] equal:@"this is a counter example."]; | ||
}); | ||
|
||
SPEC_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters