Skip to content

Commit

Permalink
Fix redirect handling
Browse files Browse the repository at this point in the history
According to the docs for URLSessionTaskDelegate, redirects should return the original response body, not an error if the redirect was rejected.

https://developer.apple.com/documentation/foundation/urlsessiontaskdelegate/1411626-urlsession#
  • Loading branch information
davbeck committed Mar 23, 2018
1 parent de1dd2f commit bf3562d
Showing 1 changed file with 8 additions and 10 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

0 comments on commit bf3562d

Please sign in to comment.