Skip to content

Commit

Permalink
Renaming AFTestURLProtocol -> AFMockURLProtocol
Browse files Browse the repository at this point in the history
Refactoring AFMockURLProtocol to use mocks more directly
  • Loading branch information
mattt committed Jun 1, 2013
1 parent 230afbb commit 118bfee
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
16 changes: 12 additions & 4 deletions Tests/AFTestURLProtocol.h → Tests/AFMockURLProtocol.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AFTestURLProtocol.h
// AFMockURLProtocol.h
//
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
//
Expand All @@ -22,9 +22,17 @@

#import <Foundation/Foundation.h>

@interface AFTestURLProtocol : NSURLProtocol <NSURLAuthenticationChallengeSender>
#import "OCMock.h"

+ (void)matchURL:(NSURL *)URL
withCallback:(id (^)(AFTestURLProtocol *protocol))initializationCallback;
@protocol AFMockURLProtocolProxy <NSObject>
- (id)stub;
- (id)expect;
- (id)reject;
@end

@interface AFMockURLProtocol : NSURLProtocol <NSURLAuthenticationChallengeSender>

+ (void)handleNextRequestForURL:(NSURL *)URL
usingBlock:(void (^)(AFMockURLProtocol <AFMockURLProtocolProxy> * protocol))block;

@end
32 changes: 19 additions & 13 deletions Tests/AFTestURLProtocol.m → Tests/AFMockURLProtocol.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AFTestURLProtocol.m
// AFMockURLProtocol.m
//
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
//
Expand All @@ -20,24 +20,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "AFTestURLProtocol.h"
#import "AFMockURLProtocol.h"

typedef id (^AFTestURLProtocolInitializationCallback)(AFTestURLProtocol *protocol);
typedef void (^AFTestURLProtocolInitializationCallback)(AFMockURLProtocol *protocol);

static NSURL * _matchingURL = nil;
static AFTestURLProtocolInitializationCallback _initializationCallback = nil;
static volatile NSURL * _matchingURL = nil;
static volatile AFTestURLProtocolInitializationCallback _initializationCallback = nil;

@implementation AFTestURLProtocol
@implementation AFMockURLProtocol

+ (void)load {
[NSURLProtocol registerClass:[AFTestURLProtocol class]];
[NSURLProtocol registerClass:[AFMockURLProtocol class]];
}

+ (void)matchURL:(NSURL *)URL
withCallback:(id(^)(AFTestURLProtocol *protocol))callback
+ (void)handleNextRequestForURL:(NSURL *)URL
usingBlock:(void (^)(AFMockURLProtocol <AFMockURLProtocolProxy> * protocol))block;
{
_matchingURL = URL;
_initializationCallback = callback;
_initializationCallback = block;
}

#pragma mark - NSURLProtocol
Expand All @@ -50,7 +50,9 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a
toRequest:(NSURLRequest *)b
{
return NO;
}

Expand All @@ -63,10 +65,14 @@ - (id)initWithRequest:(NSURLRequest *)request
return nil;
}

self = _initializationCallback ? _initializationCallback(self) : self;
if (_initializationCallback) {
self = [OCMockObject partialMockForObject:self];

_initializationCallback(self);
}

_matchingURL = nil;
_initializationCallback = nil;
_matchingURL = nil;

return self;
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/AFNetworking Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
A70F4A9F175A726B00386DF5 /* ca.cer in Resources */ = {isa = PBXBuildFile; fileRef = A70F4A9C175A726B00386DF5 /* ca.cer */; };
A70F4AA0175A726B00386DF5 /* derived.cert in Resources */ = {isa = PBXBuildFile; fileRef = A70F4A9D175A726B00386DF5 /* derived.cert */; };
A70F4AA1175A726B00386DF5 /* derived.cert in Resources */ = {isa = PBXBuildFile; fileRef = A70F4A9D175A726B00386DF5 /* derived.cert */; };
A7DC62A617592E4200EBEC2F /* AFTestURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */; };
A7DC62A717592E4200EBEC2F /* AFTestURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */; };
A7DC62A617592E4200EBEC2F /* AFMockURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFMockURLProtocol.m */; };
A7DC62A717592E4200EBEC2F /* AFMockURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A517592E4200EBEC2F /* AFMockURLProtocol.m */; };
A7DC62A917592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */; };
A7DC62AA17592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */; };
AC11A74923B64A3096ACADFC /* libPods-osx.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 96A923755B00464187DEDBAF /* libPods-osx.a */; };
Expand Down Expand Up @@ -87,8 +87,8 @@
A70F4A95175A529400386DF5 /* root_certificate.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = root_certificate.cer; path = Resources/root_certificate.cer; sourceTree = "<group>"; };
A70F4A9C175A726B00386DF5 /* ca.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = ca.cer; path = Resources/ca.cer; sourceTree = "<group>"; };
A70F4A9D175A726B00386DF5 /* derived.cert */ = {isa = PBXFileReference; lastKnownFileType = file; name = derived.cert; path = Resources/derived.cert; sourceTree = "<group>"; };
A7DC62A417592E4200EBEC2F /* AFTestURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFTestURLProtocol.h; sourceTree = "<group>"; };
A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFTestURLProtocol.m; sourceTree = "<group>"; };
A7DC62A417592E4200EBEC2F /* AFMockURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFMockURLProtocol.h; sourceTree = "<group>"; };
A7DC62A517592E4200EBEC2F /* AFMockURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFMockURLProtocol.m; sourceTree = "<group>"; };
A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLConnectionOperationTests.m; sourceTree = "<group>"; };
F8C6F281174D2C6200B154D5 /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Icon.png; path = ../Example/Icon.png; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -128,7 +128,6 @@
25801549173EB4B40026AA6E /* Pods-ios.xcconfig */,
2580154A173EB4B40026AA6E /* Pods-osx.xcconfig */,
25801548173EB3B00026AA6E /* Tests */,
A70F4A91175A4E0000386DF5 /* Certificates */,
2544EC37173BE382004117E8 /* AFNetworking */,
25A753091747FC7E00F04F2F /* Resources */,
2544EC34173BE382004117E8 /* Frameworks */,
Expand Down Expand Up @@ -207,8 +206,8 @@
children = (
2580153E173EB3A70026AA6E /* AFNetworkingTests.h */,
2580153F173EB3A70026AA6E /* AFNetworkingTests.m */,
A7DC62A417592E4200EBEC2F /* AFTestURLProtocol.h */,
A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */,
A7DC62A417592E4200EBEC2F /* AFMockURLProtocol.h */,
A7DC62A517592E4200EBEC2F /* AFMockURLProtocol.m */,
A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */,
2580153B173EB3A70026AA6E /* AFHTTPRequestOperationTests.m */,
29A9CE2017456336002360C8 /* AFJSONRequestOperationTests.m */,
Expand All @@ -220,6 +219,7 @@
25A753091747FC7E00F04F2F /* Resources */ = {
isa = PBXGroup;
children = (
A70F4A91175A4E0000386DF5 /* Certificates */,
F8C6F281174D2C6200B154D5 /* Icon.png */,
);
name = Resources;
Expand Down Expand Up @@ -364,7 +364,7 @@
25801542173EB3A70026AA6E /* AFHTTPRequestOperationTests.m in Sources */,
25801546173EB3A70026AA6E /* AFNetworkingTests.m in Sources */,
29A9CE2117456336002360C8 /* AFJSONRequestOperationTests.m in Sources */,
A7DC62A617592E4200EBEC2F /* AFTestURLProtocol.m in Sources */,
A7DC62A617592E4200EBEC2F /* AFMockURLProtocol.m in Sources */,
A7DC62A917592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -377,7 +377,7 @@
25801543173EB3A70026AA6E /* AFHTTPRequestOperationTests.m in Sources */,
25801547173EB3A70026AA6E /* AFNetworkingTests.m in Sources */,
29A9CE2217456336002360C8 /* AFJSONRequestOperationTests.m in Sources */,
A7DC62A717592E4200EBEC2F /* AFTestURLProtocol.m in Sources */,
A7DC62A717592E4200EBEC2F /* AFMockURLProtocol.m in Sources */,
A7DC62AA17592E4800EBEC2F /* AFURLConnectionOperationTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
19 changes: 8 additions & 11 deletions Tests/AFURLConnectionOperationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#import "AFNetworkingTests.h"
#import "AFURLConnectionOperation.h"
#import "AFTestURLProtocol.h"
#import "AFMockURLProtocol.h"

@interface AFURLConnectionOperationTests : SenTestCase
@property (readwrite, nonatomic, strong) NSURL *baseURL;
Expand All @@ -46,20 +46,17 @@ - (void)testThatAFURLConnectionOperationInvokesWillSendRequestForAuthenticationC
willSendRequestForAuthenticationChallengeBlockInvoked = YES;
}];

[AFTestURLProtocol matchURL:request.URL withCallback:^id(AFTestURLProtocol *protocol) {
id mockedProtocol = [OCMockObject partialMockForObject:protocol];
[AFMockURLProtocol handleNextRequestForURL:request.URL usingBlock:^(AFMockURLProtocol <AFMockURLProtocolProxy> * protocol) {

void(^startOperation)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
__unsafe_unretained AFTestURLProtocol *protocol = nil;
__unsafe_unretained AFMockURLProtocol *protocol = nil;
[invocation getArgument:&protocol atIndex:0];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:request.URL.host port:request.URL.port.integerValue protocol:request.URL.scheme realm:nil authenticationMethod:NSURLAuthenticationMethodDefault];
NSURLAuthenticationChallenge *authenticationChallenge = [[NSURLAuthenticationChallenge alloc] initWithProtectionSpace:protectionSpace proposedCredential:nil previousFailureCount:0 failureResponse:nil error:nil sender:protocol];
[protocol.client URLProtocol:protocol didReceiveAuthenticationChallenge:authenticationChallenge];
};
[[[mockedProtocol stub] andDo:startOperation] startLoading];

return mockedProtocol;
[[[protocol stub] andDo:startOperation] startLoading];
}];

[operation start];
Expand Down Expand Up @@ -101,7 +98,7 @@ - (void)testThatAFURLConnectionOperationTrustsPinnedCertificates {
[invocation setReturnValue:(void *)&trust];
}] serverTrust];

AFTestURLProtocol *protocol = [[AFTestURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
AFMockURLProtocol *protocol = [[AFMockURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
id mockedProtocol = [OCMockObject partialMockForObject:protocol];

void(^useCredential)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
Expand Down Expand Up @@ -157,7 +154,7 @@ - (void)testThatAFURLConnectionOperationTrustsPinnedPublicKeys {
[invocation setReturnValue:(void *)&trust];
}] serverTrust];

AFTestURLProtocol *protocol = [[AFTestURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
AFMockURLProtocol *protocol = [[AFMockURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
id mockedProtocol = [OCMockObject partialMockForObject:protocol];

void(^useCredential)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
Expand Down Expand Up @@ -219,7 +216,7 @@ - (void)testThatAFURLConnectionOperationTrustsPublicKeysOfDerivedCertificates {
[invocation setReturnValue:(void *)&trust];
}] serverTrust];

AFTestURLProtocol *protocol = [[AFTestURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
AFMockURLProtocol *protocol = [[AFMockURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
id mockedProtocol = [OCMockObject partialMockForObject:protocol];

void(^useCredential)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
Expand Down Expand Up @@ -282,7 +279,7 @@ - (void)testThatAFURLConnectionOperationTrustsDerivedCertificates {
[invocation setReturnValue:(void *)&trust];
}] serverTrust];

AFTestURLProtocol *protocol = [[AFTestURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
AFMockURLProtocol *protocol = [[AFMockURLProtocol alloc] initWithRequest:request cachedResponse:nil client:nil];
id mockedProtocol = [OCMockObject partialMockForObject:protocol];

void(^useCredential)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
Expand Down

0 comments on commit 118bfee

Please sign in to comment.