Skip to content

Commit

Permalink
Merge pull request #7 from Kyle-Ye/bugfix/kyle/url
Browse files Browse the repository at this point in the history
Fix extra empty path issue with HttpUrl.init?(url:)
  • Loading branch information
tib authored May 25, 2023
2 parents ae927ad + 4eb4a9c commit 8bcff49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 10 additions & 11 deletions Sources/SwiftHttp/HttpUrl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,19 @@ extension HttpUrl {
///
/// Returns `nil` if a `HttpUrl` cannot be formed with the `URL` (for example, if the string contains characters that are illegal in a URL, or is an empty string).
public init?(url: URL) {
guard
let components = URLComponents(
url: url,
resolvingAgainstBaseURL: true
)
else { return nil }
var path = components.path.trimmingCharacters(
in: CharacterSet(charactersIn: "/")
).components(separatedBy: "/")
guard let components = URLComponents(
url: url,
resolvingAgainstBaseURL: true
) else { return nil }

var path = components.path
.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
.components(separatedBy: "/")
.filter { !$0.isEmpty }
let resource: String?
if path.last?.contains(".") == true {
resource = path.removeLast()
}
else {
} else {
resource = nil
}
self.init(
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftHttpTests/HttpUrlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ final class HttpUrlTests: XCTestCase {
"https://jsonplaceholder.typicode.com/todos?foo=bar"
)
}

func testURLInitPathIssue() throws {
let url = URL(string: "https://jsonplaceholder.typicode.com")!
let baseUrl = try XCTUnwrap(HttpUrl(url: url))
XCTAssertEqual(baseUrl.path, [])
}
}

0 comments on commit 8bcff49

Please sign in to comment.