Skip to content

Request.url should return nil for situations where it is not applicable #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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