From 82bdaab654e1931f93c0cef14ca7ba2389d21550 Mon Sep 17 00:00:00 2001 From: Alex Manzella Date: Thu, 22 Sep 2016 23:51:34 +0200 Subject: [PATCH] Use RouterTestServer in every tests that performs Network requests to make sure we don't do real requests by mistacke Hopefully might help to troubleshoot the occasionally failing test --- Kakapo.xcodeproj/project.pbxproj | 26 +++++++++++- Tests/Helpers/FakeURLProtocolClient.swift | 20 +++++++++ Tests/Helpers/RouterTestServer.swift | 48 +++++++++++++++++++++ Tests/JSONAPIErrorTests.swift | 7 ++- Tests/RouterTests.swift | 52 +---------------------- 5 files changed, 100 insertions(+), 53 deletions(-) create mode 100644 Tests/Helpers/FakeURLProtocolClient.swift create mode 100644 Tests/Helpers/RouterTestServer.swift diff --git a/Kakapo.xcodeproj/project.pbxproj b/Kakapo.xcodeproj/project.pbxproj index 1449a62..8ceeb09 100644 --- a/Kakapo.xcodeproj/project.pbxproj +++ b/Kakapo.xcodeproj/project.pbxproj @@ -32,6 +32,12 @@ DE4924261D356A2000604142 /* NSMutableURLRequest+FixCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = DE4924201D356A2000604142 /* NSMutableURLRequest+FixCopy.m */; }; DE4924271D356A2000604142 /* NSMutableURLRequest+FixCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = DE4924201D356A2000604142 /* NSMutableURLRequest+FixCopy.m */; }; DE4924281D356A2000604142 /* NSMutableURLRequest+FixCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = DE4924201D356A2000604142 /* NSMutableURLRequest+FixCopy.m */; }; + DE5652FF1D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */; }; + DE5653001D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */; }; + DE5653011D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */; }; + DE5653031D9488BA00F02188 /* RouterTestServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5653021D9488BA00F02188 /* RouterTestServer.swift */; }; + DE5653041D9488BA00F02188 /* RouterTestServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5653021D9488BA00F02188 /* RouterTestServer.swift */; }; + DE5653051D9488BA00F02188 /* RouterTestServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE5653021D9488BA00F02188 /* RouterTestServer.swift */; }; DE76E1311D0DC62B009721A4 /* Kakapo.h in Headers */ = {isa = PBXBuildFile; fileRef = DE76E0FA1D0DC37E009721A4 /* Kakapo.h */; settings = {ATTRIBUTES = (Public, ); }; }; DE76E1411D0DC640009721A4 /* Kakapo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE76E1371D0DC640009721A4 /* Kakapo.framework */; }; DE76E15D1D0DC65B009721A4 /* Kakapo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE76E1531D0DC65B009721A4 /* Kakapo.framework */; }; @@ -137,6 +143,8 @@ DE3950A91D216C77008DCF43 /* SerializationTransformerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SerializationTransformerTests.swift; sourceTree = ""; }; DE49241F1D356A2000604142 /* NSMutableURLRequest+FixCopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableURLRequest+FixCopy.h"; sourceTree = ""; }; DE4924201D356A2000604142 /* NSMutableURLRequest+FixCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableURLRequest+FixCopy.m"; sourceTree = ""; }; + DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeURLProtocolClient.swift; sourceTree = ""; }; + DE5653021D9488BA00F02188 /* RouterTestServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouterTestServer.swift; sourceTree = ""; }; DE76E0F91D0DC37E009721A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; DE76E0FA1D0DC37E009721A4 /* Kakapo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Kakapo.h; sourceTree = ""; }; DE76E0FE1D0DC38B009721A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -254,6 +262,15 @@ name = Pods; sourceTree = ""; }; + DE5652FD1D94885300F02188 /* Helpers */ = { + isa = PBXGroup; + children = ( + DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */, + DE5653021D9488BA00F02188 /* RouterTestServer.swift */, + ); + path = Helpers; + sourceTree = ""; + }; DE76E08B1D0DBDBA009721A4 = { isa = PBXGroup; children = ( @@ -295,7 +312,7 @@ DE76E0FD1D0DC38B009721A4 /* Tests */ = { isa = PBXGroup; children = ( - DE76E0FE1D0DC38B009721A4 /* Info.plist */, + DE5652FD1D94885300F02188 /* Helpers */, 8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */, DE76E1B11D0DC857009721A4 /* JSONAPITests.swift */, DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */, @@ -306,6 +323,7 @@ DE3950A91D216C77008DCF43 /* SerializationTransformerTests.swift */, DE76E1B61D0DC857009721A4 /* RouteMatcherTests.swift */, DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */, + DE76E0FE1D0DC38B009721A4 /* Info.plist */, ); path = Tests; sourceTree = ""; @@ -863,11 +881,13 @@ DE76E1CB1D0DC857009721A4 /* RouteMatcherTests.swift in Sources */, DE76E1BF1D0DC857009721A4 /* StoreTests.swift in Sources */, DE76E1C81D0DC857009721A4 /* SerializerTests.swift in Sources */, + DE5653041D9488BA00F02188 /* RouterTestServer.swift in Sources */, DE76E1C51D0DC857009721A4 /* RouterTests.swift in Sources */, DE8202801D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */, DE76E1CE1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */, DE76E1C21D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */, DE3950AB1D216C77008DCF43 /* SerializationTransformerTests.swift in Sources */, + DE5653001D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -899,11 +919,13 @@ DE76E1CC1D0DC857009721A4 /* RouteMatcherTests.swift in Sources */, DE76E1C01D0DC857009721A4 /* StoreTests.swift in Sources */, DE76E1C91D0DC857009721A4 /* SerializerTests.swift in Sources */, + DE5653051D9488BA00F02188 /* RouterTestServer.swift in Sources */, DE76E1C61D0DC857009721A4 /* RouterTests.swift in Sources */, DE8202811D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */, DE76E1CF1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */, DE76E1C31D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */, DE3950AC1D216C77008DCF43 /* SerializationTransformerTests.swift in Sources */, + DE5653011D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -935,11 +957,13 @@ DE76E1CA1D0DC857009721A4 /* RouteMatcherTests.swift in Sources */, DE76E1BE1D0DC857009721A4 /* StoreTests.swift in Sources */, DE76E1C71D0DC857009721A4 /* SerializerTests.swift in Sources */, + DE5653031D9488BA00F02188 /* RouterTestServer.swift in Sources */, DE76E1C41D0DC857009721A4 /* RouterTests.swift in Sources */, DE82027F1D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */, DE76E1CD1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */, DE76E1C11D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */, DE3950AA1D216C77008DCF43 /* SerializationTransformerTests.swift in Sources */, + DE5652FF1D94888D00F02188 /* FakeURLProtocolClient.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tests/Helpers/FakeURLProtocolClient.swift b/Tests/Helpers/FakeURLProtocolClient.swift new file mode 100644 index 0000000..bcf46fa --- /dev/null +++ b/Tests/Helpers/FakeURLProtocolClient.swift @@ -0,0 +1,20 @@ +// +// FakeURLProtocolClient.swift +// Kakapo +// +// Created by Alex Manzella on 22/09/16. +// Copyright © 2016 devlucky. All rights reserved. +// + +import Foundation + +final class FakeURLProtocolClient: NSObject, NSURLProtocolClient { + @objc func URLProtocol(`protocol`: NSURLProtocol, wasRedirectedToRequest request: NSURLRequest, redirectResponse: NSURLResponse) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, cachedResponseIsValid cachedResponse: NSCachedURLResponse) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, didReceiveResponse response: NSURLResponse, cacheStoragePolicy policy: NSURLCacheStoragePolicy) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, didLoadData data: NSData) { /* intentionally left empty */ } + @objc func URLProtocolDidFinishLoading(`protocol`: NSURLProtocol) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, didFailWithError error: NSError) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { /* intentionally left empty */ } + @objc func URLProtocol(`protocol`: NSURLProtocol, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { /* intentionally left empty */ } +} diff --git a/Tests/Helpers/RouterTestServer.swift b/Tests/Helpers/RouterTestServer.swift new file mode 100644 index 0000000..fe84b97 --- /dev/null +++ b/Tests/Helpers/RouterTestServer.swift @@ -0,0 +1,48 @@ +// +// RouterTestServer.swift +// Kakapo +// +// Created by Alex Manzella on 22/09/16. +// Copyright © 2016 devlucky. All rights reserved. +// + +import Foundation + +/** + A test server that conforms to NSURLProtocol, in order to intercept outgoing network for unit tests. + As `NSURLProtocol` documentation states: + + "Classes are consulted in the reverse order of their registration. + A similar design governs the process to create the canonical form of a request with canonicalRequestForRequest:." + + Thus, we use this test server to intercept real network calls in tests as a fallback for `Server`. + */ +final class RouterTestServer: NSURLProtocol { + + class func register() { + NSURLProtocol.registerClass(self) + } + + class func disable() { + NSURLProtocol.unregisterClass(self) + } + + override class func canInitWithRequest(request: NSURLRequest) -> Bool { + return true + } + + override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest { + return request + } + + override func startLoading() { + guard let requestURL = request.URL, + client = client else { return } + + let response = NSHTTPURLResponse(URL: requestURL, statusCode: 200, HTTPVersion: "HTTP/1.1", headerFields: nil)! + client.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: .AllowedInMemoryOnly) + client.URLProtocolDidFinishLoading(self) + } + + override func stopLoading() {} +} diff --git a/Tests/JSONAPIErrorTests.swift b/Tests/JSONAPIErrorTests.swift index bfab2fd..74f2a25 100644 --- a/Tests/JSONAPIErrorTests.swift +++ b/Tests/JSONAPIErrorTests.swift @@ -56,10 +56,15 @@ class JSONAPIErrorsSpec: QuickSpec { context("Provides response fields") { + beforeEach { + RouterTestServer.register() + } + afterEach { + RouterTestServer.disable() Router.disableAll() } - + it("should affect the status code of the request") { let router = Router.register("http://www.test123.com") diff --git a/Tests/RouterTests.swift b/Tests/RouterTests.swift index 27671db..32373eb 100644 --- a/Tests/RouterTests.swift +++ b/Tests/RouterTests.swift @@ -15,56 +15,6 @@ import Alamofire @testable import Kakapo -/** - A test server that conforms to NSURLProtocol, in order to intercept outgoing network for unit tests. - As `NSURLProtocol` documentation states: - - "Classes are consulted in the reverse order of their registration. - A similar design governs the process to create the canonical form of a request with canonicalRequestForRequest:." - - Thus, we use this test server to intercept real network calls in tests as a fallback for `Server`. - */ -private final class RouterTestServer: NSURLProtocol { - - class func register() { - NSURLProtocol.registerClass(self) - } - - class func disable() { - NSURLProtocol.unregisterClass(self) - } - - override class func canInitWithRequest(request: NSURLRequest) -> Bool { - return true - } - - override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest { - return request - } - - override func startLoading() { - guard let requestURL = request.URL, - client = client else { return } - - let response = NSHTTPURLResponse(URL: requestURL, statusCode: 200, HTTPVersion: "HTTP/1.1", headerFields: nil)! - client.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: .AllowedInMemoryOnly) - client.URLProtocolDidFinishLoading(self) - } - - override func stopLoading() {} -} - -private final class ProtocolClientTest: NSObject, NSURLProtocolClient { - @objc func URLProtocol(`protocol`: NSURLProtocol, wasRedirectedToRequest request: NSURLRequest, redirectResponse: NSURLResponse) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, cachedResponseIsValid cachedResponse: NSCachedURLResponse) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, didReceiveResponse response: NSURLResponse, cacheStoragePolicy policy: NSURLCacheStoragePolicy) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, didLoadData data: NSData) { /* intentionally left empty */ } - @objc func URLProtocolDidFinishLoading(`protocol`: NSURLProtocol) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, didFailWithError error: NSError) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { /* intentionally left empty */ } - @objc func URLProtocol(`protocol`: NSURLProtocol, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { /* intentionally left empty */ } -} - struct CustomResponse: ResponseFieldsProvider { let statusCode: Int let body: Serializable @@ -173,7 +123,7 @@ class RouterTests: QuickSpec { Therefore we simulate the "stopLoading" and "startLoading" mechanism manually. */ let urlRequest = NSURLRequest(URL: requestURL) - let client = ProtocolClientTest() + let client = FakeURLProtocolClient() let server = Server(request: urlRequest, cachedResponse: nil, client: client) server.stopLoading()