Skip to content

Commit

Permalink
Request.url should return nil for situations where it is not applicab…
Browse files Browse the repository at this point in the history
…le (#84)

Regular CONNECT requests and OPTIONS * requests do not have URLs
  • Loading branch information
guoye-zhang authored Jan 29, 2025
1 parent 74191f0 commit 40ca463
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/HTTPTypesFoundation/HTTPRequest+URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ extension HTTPRequest {
/// fields.
public var url: URL? {
get {
if (self.method == .connect && self.extendedConnectProtocol == nil)
|| (self.method == .options && self.path == "*")
{
return nil
}
if let schemeField = self.pseudoHeaderFields.scheme,
let authorityField = self.pseudoHeaderFields.authority,
let pathField = self.pseudoHeaderFields.path
Expand Down
25 changes: 25 additions & 0 deletions Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ final class HTTPTypesFoundationTests: XCTestCase {
XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/")
}

func testNilRequestURL() {
let request1 = HTTPRequest(
method: .connect,
scheme: "https",
authority: "www.example.com:443",
path: "www.example.com:443"
)
XCTAssertNil(request1.url)

var request2 = HTTPRequest(
method: .connect,
scheme: "https",
authority: "www.example.com",
path: "/"
)
request2.extendedConnectProtocol = "websocket"
XCTAssertEqual(request2.url?.absoluteString, "https://www.example.com/")

let request3 = HTTPRequest(method: .options, scheme: "https", authority: "www.example.com", path: "*")
XCTAssertNil(request3.url)

let request4 = HTTPRequest(method: .options, scheme: "https", authority: "www.example.com", path: "/")
XCTAssertEqual(request4.url?.absoluteString, "https://www.example.com/")
}

func testRequestToFoundation() throws {
let request = HTTPRequest(
method: .get,
Expand Down

0 comments on commit 40ca463

Please sign in to comment.