Skip to content

Commit

Permalink
Merge pull request #6 from davbeck/feature/redirect
Browse files Browse the repository at this point in the history
Add support for redirect responses
  • Loading branch information
JanGorman authored Mar 23, 2018
2 parents 18c5dd1 + 4cac206 commit 403cf4c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
18 changes: 8 additions & 10 deletions Hippolyte/HTTPStubURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ final class HTTPStubURLProtocol: URLProtocol {
let response = HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: nil,
headerFields: stubbedResponse.headers)

if statusCode < 300 || statusCode > 399 || statusCode == 304 || statusCode == 305 {
let body = stubbedResponse.body
client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: body!)
client?.urlProtocolDidFinishLoading(self)
} else {
if 300...399 ~= statusCode && (statusCode != 304 || statusCode != 305) {
guard let location = stubbedResponse.headers["Location"], let url = URL(string: location),
let cookies = cookieStorage.cookies(for: url) else { return }

var redirect = URLRequest(url: url)
redirect.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookies)

client?.urlProtocol(self, wasRedirectedTo: redirect, redirectResponse: response!)
let error = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
client?.urlProtocol(self, didFailWithError: error)

client?.urlProtocol(self, wasRedirectedTo: redirect, redirectResponse: response!)
}

let body = stubbedResponse.body
client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: body!)
client?.urlProtocolDidFinishLoading(self)
}
}

Expand Down
40 changes: 39 additions & 1 deletion HippolyteTests/HippolyteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,51 @@ class HippolyteTests: XCTestCase {
Hippolyte.shared.start()

let expectation = self.expectation(description: "Stubs network call")
let task = URLSession.shared.dataTask(with: url) { data, _, _ in
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: nil)
let task = session.dataTask(with: url) { data, _, _ in
XCTAssertEqual(data, body)
expectation.fulfill()
}
task.resume()

wait(for: [expectation], timeout: 1)
}

func testItStubsRedirectNetworkCall() {
let url = URL(string: "http://www.apple.com")!
var stub = StubRequest(method: .GET, url: url)
var response = StubResponse()
response = StubResponse(statusCode: 301)
response.headers = ["Location": "https://example.com/not/here"]
let body = "Hippolyte Redirect".data(using: .utf8)!
response.body = body
stub.response = response
Hippolyte.shared.add(stubbedRequest: stub)

Hippolyte.shared.start()

let expectation = self.expectation(description: "Stubs network redirect call")
let delegate = BlockRedirectDelegate()
let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)
let task = session.dataTask(with: url) { data, _, _ in
XCTAssertEqual(data, body)
expectation.fulfill()
}
task.resume()
wait(for: [expectation], timeout: 1)

XCTAssertEqual(delegate.redirectCallCount, 1)
}

}

class BlockRedirectDelegate: NSObject, URLSessionDelegate, URLSessionTaskDelegate {
var redirectCallCount: Int = 0

func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
redirectCallCount += 1

// disables redirect responses
completionHandler(nil)
}
}

0 comments on commit 403cf4c

Please sign in to comment.