Skip to content
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
6 changes: 6 additions & 0 deletions Sources/Modules/Core/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ extension HTTPRequest {
get { urlComponents.path }
set { urlComponents.path = newValue }
}

/// The query items component of the URL.
public var queryItems: [URLQueryItem] {
get { urlComponents.queryItems ?? [] }
set { urlComponents.queryItems = newValue }
}
}

fileprivate struct AnyHTTPRequestOption: @unchecked Sendable {
Expand Down
48 changes: 48 additions & 0 deletions Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Edvinas Byla on 14/05/2025.
//

import Foundation
import Testing
import SimpleHTTP

Expand Down Expand Up @@ -47,6 +48,53 @@ struct HTTPRequestTests {
request[option: ToggleOption.self] = true
#expect(request[option: ToggleOption.self] == true)
}

@Test("Single query item composes into URL")
func testSingleQueryItemURL() throws {
var request = HTTPRequest(
method: .get,
scheme: "https",
host: "example.com",
path: "/foo"
)

request.queryItems = [
URLQueryItem(name: "since", value: "15")
]

let url = try #require(request.url)
#expect(url.absoluteString == "https://example.com/foo?since=15")
}

@Test("Query items reflect in composed URL")
func testQueryItemsComposition() throws {
var request = HTTPRequest(
method: .get,
scheme: "https",
host: "example.com",
path: "/foo"
)

request.queryItems = [
URLQueryItem(name: "since", value: "15"),
URLQueryItem(name: "limit", value: "100")
]

let url = try #require(request.url)
#expect(url.scheme == "https")
#expect(url.host == "example.com")
#expect(url.path == "/foo")

let components = try #require(
URLComponents(
url: url,
resolvingAgainstBaseURL: false
)
)
let items = components.queryItems ?? []
#expect(items.contains(URLQueryItem(name: "since", value: "15")))
#expect(items.contains(URLQueryItem(name: "limit", value: "100")))
}
}

fileprivate enum ToggleOption: HTTPRequestOption {
Expand Down