Skip to content

Commit

Permalink
Merge pull request #74 from immobiliare/bug/70-fix-for-stubber-ignore…
Browse files Browse the repository at this point in the history
…QueryParameters

#70: HTTP Stubbing: URL matcher incorrectly strips port when using "ignore query parameters" option
  • Loading branch information
malcommac authored Nov 27, 2022
2 parents 6759f24 + c7d05e2 commit 51697c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ extension URL {
/// "https://www.apple.com/v1/test?param=test"
/// would be "https://www.apple.com/v1/test"
public var baseString: String? {
guard let scheme = scheme, let host = host else { return nil }
guard let scheme = scheme, let host = fullHost else { return nil }
return scheme + "://" + host + path
}

Expand All @@ -286,6 +286,16 @@ extension URL {
}
}

// MARK: - Public Properties

/// Return the complete host along with port (if available).
///
/// NOTE: the original `host` method strip the port.
public var fullHost: String? {
guard let host = host else { return nil }
return host + (port != nil ? ":\(port!)" : "")
}

}

// MARK: - FileManager
Expand Down
18 changes: 18 additions & 0 deletions Tests/RealHTTPTests/Requests+Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ import XCTest
import Combine
@testable import RealHTTP

class StubberTests: XCTestCase {

/// Test stubber along with port specified.
public func test_matchStubberWithExplicitPort() async throws {
let stub = HTTPStubRequest()
.match(URL: URL(string: "http://localhost:3001/some/path")!)
.stub(for: .get) { _, _ in
let response = HTTPStubResponse()
response.statusCode = .ok
return response
}

let matches = stub.matchers[0].matches(request: URLRequest(url: URL(string: "http://localhost:3001/some/path")!), for: stub)
XCTAssertTrue(matches, "Failed to match host with port")
}

}

class RequestsTests: XCTestCase {

private var observerBag = Set<AnyCancellable>()
Expand Down

0 comments on commit 51697c0

Please sign in to comment.