Skip to content

Commit

Permalink
Add leading slash in relative URL requests if necessary (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan-25 authored Jul 9, 2024
1 parent 07536f6 commit 54d1006
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ extension URL {
if self.path.isEmpty {
return "/"
}
return URLComponents(url: self, resolvingAgainstBaseURL: false)?.percentEncodedPath ?? self.path
return URLComponents(url: self, resolvingAgainstBaseURL: true)?.percentEncodedPath ?? self.path
}

var uri: String {
Expand Down
19 changes: 19 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ class HTTPClientInternalTests: XCTestCase {
XCTAssertEqual(request12.url.uri, "/some%2Fpathsegment1/pathsegment2")
}

func testURIOfRelativeURLRequest() throws {
let requestNoLeadingSlash = try Request(
url: URL(
string: "percent%2Fencoded/hello",
relativeTo: URL(string: "http://127.0.0.1")!
)!
)

let requestWithLeadingSlash = try Request(
url: URL(
string: "/percent%2Fencoded/hello",
relativeTo: URL(string: "http://127.0.0.1")!
)!
)

XCTAssertEqual(requestNoLeadingSlash.url.uri, "/percent%2Fencoded/hello")
XCTAssertEqual(requestWithLeadingSlash.url.uri, "/percent%2Fencoded/hello")
}

func testChannelAndDelegateOnDifferentEventLoops() throws {
class Delegate: HTTPClientResponseDelegate {
typealias Response = ([Message], [Message])
Expand Down
14 changes: 14 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,20 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
XCTAssertEqual(.ok, response.status)
}

func testLeadingSlashRelativeURL() throws {
let noLeadingSlashURL = URL(string: "percent%2Fencoded/hello", relativeTo: URL(string: self.defaultHTTPBinURLPrefix)!)!
let withLeadingSlashURL = URL(string: "/percent%2Fencoded/hello", relativeTo: URL(string: self.defaultHTTPBinURLPrefix)!)!

let noLeadingSlashURLRequest = try HTTPClient.Request(url: noLeadingSlashURL, method: .GET)
let withLeadingSlashURLRequest = try HTTPClient.Request(url: withLeadingSlashURL, method: .GET)

let noLeadingSlashURLResponse = try self.defaultClient.execute(request: noLeadingSlashURLRequest).wait()
let withLeadingSlashURLResponse = try self.defaultClient.execute(request: withLeadingSlashURLRequest).wait()

XCTAssertEqual(noLeadingSlashURLResponse.status, .ok)
XCTAssertEqual(withLeadingSlashURLResponse.status, .ok)
}

func testMultipleContentLengthHeaders() throws {
let body = ByteBuffer(string: "hello world!")

Expand Down

0 comments on commit 54d1006

Please sign in to comment.