Skip to content

Commit

Permalink
Add error response when no match is found
Browse files Browse the repository at this point in the history
Without this, any request that doesn’t match a stub will just timeout and slow down test failures.
  • Loading branch information
davbeck committed Jan 24, 2018
1 parent a8a413d commit de1dd2f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Hippolyte/HTTPStubURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import Foundation

final class HTTPStubURLProtocol: URLProtocol {

struct NoMatchError: Swift.Error, CustomStringConvertible {
let request: URLRequest

var description: String {
return "No matching stub found for \(request)"
}
}

override class func canInit(with request: URLRequest) -> Bool {
guard let scheme = request.url?.scheme else { return false }
return ["http", "https"].contains(scheme)
Expand All @@ -20,7 +28,10 @@ final class HTTPStubURLProtocol: URLProtocol {
}

override func startLoading() {
guard let stubbedResponse = try? Hippolyte.shared.response(for: request), let url = request.url else { return }
guard let stubbedResponse = try? Hippolyte.shared.response(for: request), let url = request.url else {
client?.urlProtocol(self, didFailWithError: NoMatchError(request: request))
return
}

let cookieStorage = HTTPCookieStorage.shared
cookieStorage.setCookies(HTTPCookie.cookies(withResponseHeaderFields: stubbedResponse.headers, for: url),
Expand Down

0 comments on commit de1dd2f

Please sign in to comment.