-
Notifications
You must be signed in to change notification settings - Fork 44
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 #121 from devlucky/feature/RouterTestServer
Use RouterTestServer in every tests that performs Network requests to…
- Loading branch information
Showing
5 changed files
with
100 additions
and
53 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
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 */ } | ||
} |
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,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() {} | ||
} |
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