Skip to content

Commit

Permalink
#70: Added fullHost property for URL, fixed match with port and added…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
malcommac committed Nov 27, 2022
1 parent 6759f24 commit 611e282
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 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 611e282

Please sign in to comment.