Skip to content

Commit 591ddf1

Browse files
authored
Merge pull request #2 from Pattio/query-items-support
Add query items configuration to HTTPRequest
2 parents 6d93d1e + 0028256 commit 591ddf1

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Sources/Modules/Core/HTTPRequest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ extension HTTPRequest {
113113
get { urlComponents.path }
114114
set { urlComponents.path = newValue }
115115
}
116+
117+
/// The query items component of the URL.
118+
public var queryItems: [URLQueryItem] {
119+
get { urlComponents.queryItems ?? [] }
120+
set { urlComponents.queryItems = newValue }
121+
}
116122
}
117123

118124
fileprivate struct AnyHTTPRequestOption: @unchecked Sendable {

Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Edvinas Byla on 14/05/2025.
66
//
77

8+
import Foundation
89
import Testing
910
import SimpleHTTP
1011

@@ -47,6 +48,53 @@ struct HTTPRequestTests {
4748
request[option: ToggleOption.self] = true
4849
#expect(request[option: ToggleOption.self] == true)
4950
}
51+
52+
@Test("Single query item composes into URL")
53+
func testSingleQueryItemURL() throws {
54+
var request = HTTPRequest(
55+
method: .get,
56+
scheme: "https",
57+
host: "example.com",
58+
path: "/foo"
59+
)
60+
61+
request.queryItems = [
62+
URLQueryItem(name: "since", value: "15")
63+
]
64+
65+
let url = try #require(request.url)
66+
#expect(url.absoluteString == "https://example.com/foo?since=15")
67+
}
68+
69+
@Test("Query items reflect in composed URL")
70+
func testQueryItemsComposition() throws {
71+
var request = HTTPRequest(
72+
method: .get,
73+
scheme: "https",
74+
host: "example.com",
75+
path: "/foo"
76+
)
77+
78+
request.queryItems = [
79+
URLQueryItem(name: "since", value: "15"),
80+
URLQueryItem(name: "limit", value: "100")
81+
]
82+
83+
let url = try #require(request.url)
84+
#expect(url.scheme == "https")
85+
#expect(url.host == "example.com")
86+
#expect(url.path == "/foo")
87+
88+
let components = try #require(
89+
URLComponents(
90+
url: url,
91+
resolvingAgainstBaseURL: false
92+
)
93+
)
94+
let items = components.queryItems ?? []
95+
#expect(items.contains(URLQueryItem(name: "since", value: "15")))
96+
#expect(items.contains(URLQueryItem(name: "limit", value: "100")))
97+
}
5098
}
5199

52100
fileprivate enum ToggleOption: HTTPRequestOption {

0 commit comments

Comments
 (0)