From 118bfee3a49ff74a057cbdcd2810f0903ba2451c Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sat, 1 Jun 2013 16:44:30 -0700 Subject: [PATCH] Renaming AFTestURLProtocol -> AFMockURLProtocol Refactoring AFMockURLProtocol to use mocks more directly --- ...FTestURLProtocol.h => AFMockURLProtocol.h} | 16 +++++++--- ...FTestURLProtocol.m => AFMockURLProtocol.m} | 32 +++++++++++-------- .../project.pbxproj | 18 +++++------ Tests/AFURLConnectionOperationTests.m | 19 +++++------ 4 files changed, 48 insertions(+), 37 deletions(-) rename Tests/{AFTestURLProtocol.h => AFMockURLProtocol.h} (77%) rename Tests/{AFTestURLProtocol.m => AFMockURLProtocol.m} (77%) diff --git a/Tests/AFTestURLProtocol.h b/Tests/AFMockURLProtocol.h similarity index 77% rename from Tests/AFTestURLProtocol.h rename to Tests/AFMockURLProtocol.h index 5eda046f66..ddd4d68ddf 100644 --- a/Tests/AFTestURLProtocol.h +++ b/Tests/AFMockURLProtocol.h @@ -1,4 +1,4 @@ -// AFTestURLProtocol.h +// AFMockURLProtocol.h // // Copyright (c) 2013 AFNetworking (http://afnetworking.com) // @@ -22,9 +22,17 @@ #import -@interface AFTestURLProtocol : NSURLProtocol +#import "OCMock.h" -+ (void)matchURL:(NSURL *)URL - withCallback:(id (^)(AFTestURLProtocol *protocol))initializationCallback; +@protocol AFMockURLProtocolProxy +- (id)stub; +- (id)expect; +- (id)reject; +@end + +@interface AFMockURLProtocol : NSURLProtocol + ++ (void)handleNextRequestForURL:(NSURL *)URL + usingBlock:(void (^)(AFMockURLProtocol * protocol))block; @end diff --git a/Tests/AFTestURLProtocol.m b/Tests/AFMockURLProtocol.m similarity index 77% rename from Tests/AFTestURLProtocol.m rename to Tests/AFMockURLProtocol.m index 6d20dceb39..f475af18ff 100644 --- a/Tests/AFTestURLProtocol.m +++ b/Tests/AFMockURLProtocol.m @@ -1,4 +1,4 @@ -// AFTestURLProtocol.m +// AFMockURLProtocol.m // // Copyright (c) 2013 AFNetworking (http://afnetworking.com) // @@ -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 * protocol))block; { _matchingURL = URL; - _initializationCallback = callback; + _initializationCallback = block; } #pragma mark - NSURLProtocol @@ -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; } @@ -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; } diff --git a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj index f4c273d0d9..0d7a428a50 100644 --- a/Tests/AFNetworking Tests.xcodeproj/project.pbxproj +++ b/Tests/AFNetworking Tests.xcodeproj/project.pbxproj @@ -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 */; }; @@ -87,8 +87,8 @@ A70F4A95175A529400386DF5 /* root_certificate.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = root_certificate.cer; path = Resources/root_certificate.cer; sourceTree = ""; }; A70F4A9C175A726B00386DF5 /* ca.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = ca.cer; path = Resources/ca.cer; sourceTree = ""; }; A70F4A9D175A726B00386DF5 /* derived.cert */ = {isa = PBXFileReference; lastKnownFileType = file; name = derived.cert; path = Resources/derived.cert; sourceTree = ""; }; - A7DC62A417592E4200EBEC2F /* AFTestURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFTestURLProtocol.h; sourceTree = ""; }; - A7DC62A517592E4200EBEC2F /* AFTestURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFTestURLProtocol.m; sourceTree = ""; }; + A7DC62A417592E4200EBEC2F /* AFMockURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFMockURLProtocol.h; sourceTree = ""; }; + A7DC62A517592E4200EBEC2F /* AFMockURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFMockURLProtocol.m; sourceTree = ""; }; A7DC62A817592E4800EBEC2F /* AFURLConnectionOperationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLConnectionOperationTests.m; sourceTree = ""; }; F8C6F281174D2C6200B154D5 /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = Icon.png; path = ../Example/Icon.png; sourceTree = ""; }; /* End PBXFileReference section */ @@ -128,7 +128,6 @@ 25801549173EB4B40026AA6E /* Pods-ios.xcconfig */, 2580154A173EB4B40026AA6E /* Pods-osx.xcconfig */, 25801548173EB3B00026AA6E /* Tests */, - A70F4A91175A4E0000386DF5 /* Certificates */, 2544EC37173BE382004117E8 /* AFNetworking */, 25A753091747FC7E00F04F2F /* Resources */, 2544EC34173BE382004117E8 /* Frameworks */, @@ -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 */, @@ -220,6 +219,7 @@ 25A753091747FC7E00F04F2F /* Resources */ = { isa = PBXGroup; children = ( + A70F4A91175A4E0000386DF5 /* Certificates */, F8C6F281174D2C6200B154D5 /* Icon.png */, ); name = Resources; @@ -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; @@ -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; diff --git a/Tests/AFURLConnectionOperationTests.m b/Tests/AFURLConnectionOperationTests.m index 44cc410dba..7982789902 100644 --- a/Tests/AFURLConnectionOperationTests.m +++ b/Tests/AFURLConnectionOperationTests.m @@ -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; @@ -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 * 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]; @@ -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) { @@ -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) { @@ -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) { @@ -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) {