Skip to content

Commit

Permalink
Allow passing in custom port when building a request
Browse files Browse the repository at this point in the history
  • Loading branch information
marinofelipe committed May 31, 2020
1 parent c109c53 commit 0f5937d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public final class HTTPRequestBuilder {
return self
}

@discardableResult
public func port(_ port: Int) -> HTTPRequestBuilder {
urlComponents.port = port
return self
}

@discardableResult
public func method(_ method: HTTPRequestMethod) -> HTTPRequestBuilder {
self.method = method
Expand Down
20 changes: 20 additions & 0 deletions Tests/HTTPClientCoreTests/HTTPRequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ final class HTTPRequestBuilderTests: XCTestCase {
XCTAssertEqual(decodedBody, FakeResponseBody(id: 10, description: "Desc"), "The request body has the correct properties when decoded")
}

func testBuildingValidRequestWithCustomPort() {
let builder = HTTPRequestBuilder(scheme: .https, host: "127.0.0.1", headers: ["key": "value"])

let request = try! builder
.path("/path")
.port(8080)
.method(.get)
.queryItems([URLQueryItem(name: "name", value: "value")])
.additionalHeaders(["other": "2"])
.build()

XCTAssertNotNil(request, "It is not nil")
XCTAssertNotNil(request.url, "It is not nil")
XCTAssertEqual(request.url?.absoluteString, "https://127.0.0.1:8080/path?name=value", "It has the correct url string")
XCTAssertEqual(request.allHTTPHeaderFields?.count, 2, "It has the expected headers count")
XCTAssertEqual(request.allHTTPHeaderFields?["key"], "value", "It has the expected value for key")
XCTAssertEqual(request.allHTTPHeaderFields?["other"], "2", "It has the expected value for key")
XCTAssertEqual(request.httpMethod, "GET", "It has the correct http method")
}

// MARK: - Tests - Invalid

func testBuildingRequestWithInvalidPath() {
Expand Down

0 comments on commit 0f5937d

Please sign in to comment.