Skip to content

Fix leading slash issue in relative URL requests #747

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
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
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