|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | +#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) |
| 10 | +import Foundation |
| 11 | +import XCTest |
| 12 | +#else |
| 13 | +import SwiftFoundation |
| 14 | +import SwiftXCTest |
| 15 | +#endif |
| 16 | + |
| 17 | +class TestNSURLProtocol : LoopbackServerTest { |
| 18 | + |
| 19 | + static var allTests: [(String, (TestNSURLProtocol) -> () throws -> Void)] { |
| 20 | + return [ |
| 21 | + ("test_interceptResponse", test_interceptResponse), |
| 22 | + ("test_interceptRequest", test_interceptRequest), |
| 23 | + ("test_multipleCustomProtocols", test_multipleCustomProtocols), |
| 24 | + ("test_customProtocolResponseWithDelegate", test_customProtocolResponseWithDelegate), |
| 25 | + ("test_customProtocolSetDataInResponseWithDelegate", test_customProtocolSetDataInResponseWithDelegate), |
| 26 | + ] |
| 27 | + } |
| 28 | + |
| 29 | + func test_interceptResponse() { |
| 30 | + let urlString = "http://127.0.0.1:\(TestNSURLProtocol.serverPort)/USA" |
| 31 | + let url = URL(string: urlString)! |
| 32 | + let config = URLSessionConfiguration.default |
| 33 | + config.protocolClasses = [CustomProtocol.self] |
| 34 | + config.timeoutIntervalForRequest = 8 |
| 35 | + let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil) |
| 36 | + let expect = expectation(description: "GET \(urlString): with a custom protocol") |
| 37 | + let task = session.dataTask(with: url) { data, response, error in |
| 38 | + defer { expect.fulfill() } |
| 39 | + if let e = error as? URLError { |
| 40 | + XCTAssertEqual(e.code, .timedOut, "Unexpected error code") |
| 41 | + return |
| 42 | + } |
| 43 | + let httpResponse = response as! HTTPURLResponse? |
| 44 | + XCTAssertEqual(429, httpResponse!.statusCode, "HTTP response code is not 429") |
| 45 | + } |
| 46 | + task.resume() |
| 47 | + waitForExpectations(timeout: 12) |
| 48 | + } |
| 49 | + |
| 50 | + func test_interceptRequest() { |
| 51 | + let urlString = "ssh://127.0.0.1:\(TestNSURLProtocol.serverPort)/USA" |
| 52 | + let url = URL(string: urlString)! |
| 53 | + let config = URLSessionConfiguration.default |
| 54 | + config.protocolClasses = [InterceptableRequest.self] |
| 55 | + config.timeoutIntervalForRequest = 8 |
| 56 | + let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil) |
| 57 | + let expect = expectation(description: "GET \(urlString): with a custom protocol") |
| 58 | + let task = session.dataTask(with: url) { data, response, error in |
| 59 | + defer { expect.fulfill() } |
| 60 | + if let e = error as? URLError { |
| 61 | + XCTAssertEqual(e.code, .timedOut, "Unexpected error code") |
| 62 | + return |
| 63 | + } |
| 64 | + let httpResponse = response as! HTTPURLResponse? |
| 65 | + let responseURL = URL(string: "http://google.com") |
| 66 | + XCTAssertEqual(responseURL, httpResponse?.url, "Unexpected url") |
| 67 | + XCTAssertEqual(200, httpResponse!.statusCode, "HTTP response code is not 200") |
| 68 | + } |
| 69 | + task.resume() |
| 70 | + waitForExpectations(timeout: 12) |
| 71 | + } |
| 72 | + |
| 73 | + func test_multipleCustomProtocols() { |
| 74 | + let urlString = "http://127.0.0.1:\(TestNSURLProtocol.serverPort)/Nepal" |
| 75 | + let url = URL(string: urlString)! |
| 76 | + let config = URLSessionConfiguration.default |
| 77 | + config.protocolClasses = [InterceptableRequest.self, CustomProtocol.self] |
| 78 | + let expect = expectation(description: "GET \(urlString): with a custom protocol") |
| 79 | + let session = URLSession(configuration: config) |
| 80 | + let task = session.dataTask(with: url) { data, response, error in |
| 81 | + defer { expect.fulfill() } |
| 82 | + if let e = error as? URLError { |
| 83 | + XCTAssertEqual(e.code, .timedOut, "Unexpected error code") |
| 84 | + return |
| 85 | + } |
| 86 | + let httpResponse = response as! HTTPURLResponse |
| 87 | + print(httpResponse.statusCode) |
| 88 | + XCTAssertEqual(429, httpResponse.statusCode, "Status code is not 429") |
| 89 | + } |
| 90 | + task.resume() |
| 91 | + waitForExpectations(timeout: 12) |
| 92 | + } |
| 93 | + |
| 94 | + func test_customProtocolResponseWithDelegate() { |
| 95 | + let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru" |
| 96 | + let url = URL(string: urlString)! |
| 97 | + let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self]) |
| 98 | + d.responseReceivedExpectation = expectation(description: "GET \(urlString): response received") |
| 99 | + d.run(with: url) |
| 100 | + waitForExpectations(timeout: 12) |
| 101 | + } |
| 102 | + |
| 103 | + func test_customProtocolSetDataInResponseWithDelegate() { |
| 104 | + let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal" |
| 105 | + let url = URL(string: urlString)! |
| 106 | + let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self]) |
| 107 | + d.run(with: url) |
| 108 | + waitForExpectations(timeout: 12) |
| 109 | + if !d.error { |
| 110 | + XCTAssertEqual(d.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result") |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class InterceptableRequest : URLProtocol { |
| 116 | + |
| 117 | + override class func canInit(with request: URLRequest) -> Bool { |
| 118 | + return request.url?.scheme == "ssh" |
| 119 | + } |
| 120 | + |
| 121 | + override class func canonicalRequest(for request: URLRequest) -> URLRequest { |
| 122 | + return request |
| 123 | + } |
| 124 | + |
| 125 | + override func startLoading() { |
| 126 | + let urlString = "http://google.com" |
| 127 | + let url = URL(string: urlString)! |
| 128 | + let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: [:]) |
| 129 | + self.client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed) |
| 130 | + self.client?.urlProtocolDidFinishLoading(self) |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + override func stopLoading() { |
| 135 | + return |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +class CustomProtocol : URLProtocol { |
| 140 | + |
| 141 | + override class func canInit(with request: URLRequest) -> Bool { |
| 142 | + return true |
| 143 | + } |
| 144 | + |
| 145 | + func sendResponse(statusCode: Int, headers: [String: String] = [:], data: Data) { |
| 146 | + let response = HTTPURLResponse(url: self.request.url!, statusCode: statusCode, httpVersion: "HTTP/1.1", headerFields: headers) |
| 147 | + let capital = "Kathmandu" |
| 148 | + let data = capital.data(using: String.Encoding.utf8) |
| 149 | + self.client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed) |
| 150 | + self.client?.urlProtocol(self, didLoad: data!) |
| 151 | + self.client?.urlProtocolDidFinishLoading(self) |
| 152 | + } |
| 153 | + |
| 154 | + override class func canonicalRequest(for request: URLRequest) -> URLRequest { |
| 155 | + return request |
| 156 | + } |
| 157 | + |
| 158 | + override func startLoading() { |
| 159 | + sendResponse(statusCode: 429, data: Data()) |
| 160 | + } |
| 161 | + |
| 162 | + override func stopLoading() { |
| 163 | + return |
| 164 | + } |
| 165 | +} |
0 commit comments