Skip to content

Commit

Permalink
Merge pull request #121 from devlucky/feature/RouterTestServer
Browse files Browse the repository at this point in the history
Use RouterTestServer in every tests that performs Network requests to…
  • Loading branch information
MP0w authored Sep 23, 2016
2 parents 384a633 + 82bdaab commit 56b02e2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 53 deletions.
26 changes: 25 additions & 1 deletion Kakapo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -137,6 +143,8 @@
DE3950A91D216C77008DCF43 /* SerializationTransformerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SerializationTransformerTests.swift; sourceTree = "<group>"; };
DE49241F1D356A2000604142 /* NSMutableURLRequest+FixCopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableURLRequest+FixCopy.h"; sourceTree = "<group>"; };
DE4924201D356A2000604142 /* NSMutableURLRequest+FixCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableURLRequest+FixCopy.m"; sourceTree = "<group>"; };
DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeURLProtocolClient.swift; sourceTree = "<group>"; };
DE5653021D9488BA00F02188 /* RouterTestServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouterTestServer.swift; sourceTree = "<group>"; };
DE76E0F91D0DC37E009721A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DE76E0FA1D0DC37E009721A4 /* Kakapo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Kakapo.h; sourceTree = "<group>"; };
DE76E0FE1D0DC38B009721A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,6 +262,15 @@
name = Pods;
sourceTree = "<group>";
};
DE5652FD1D94885300F02188 /* Helpers */ = {
isa = PBXGroup;
children = (
DE5652FE1D94888D00F02188 /* FakeURLProtocolClient.swift */,
DE5653021D9488BA00F02188 /* RouterTestServer.swift */,
);
path = Helpers;
sourceTree = "<group>";
};
DE76E08B1D0DBDBA009721A4 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -295,7 +312,7 @@
DE76E0FD1D0DC38B009721A4 /* Tests */ = {
isa = PBXGroup;
children = (
DE76E0FE1D0DC38B009721A4 /* Info.plist */,
DE5652FD1D94885300F02188 /* Helpers */,
8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */,
DE76E1B11D0DC857009721A4 /* JSONAPITests.swift */,
DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */,
Expand All @@ -306,6 +323,7 @@
DE3950A91D216C77008DCF43 /* SerializationTransformerTests.swift */,
DE76E1B61D0DC857009721A4 /* RouteMatcherTests.swift */,
DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */,
DE76E0FE1D0DC38B009721A4 /* Info.plist */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
20 changes: 20 additions & 0 deletions Tests/Helpers/FakeURLProtocolClient.swift
Original file line number Diff line number Diff line change
@@ -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 */ }
}
48 changes: 48 additions & 0 deletions Tests/Helpers/RouterTestServer.swift
Original file line number Diff line number Diff line change
@@ -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() {}
}
7 changes: 6 additions & 1 deletion Tests/JSONAPIErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
52 changes: 1 addition & 51 deletions Tests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 56b02e2

Please sign in to comment.