diff --git a/Sources/Modules/Core/HTTPRequest.swift b/Sources/Modules/Core/HTTPRequest.swift index 6fade7e..79f4caf 100644 --- a/Sources/Modules/Core/HTTPRequest.swift +++ b/Sources/Modules/Core/HTTPRequest.swift @@ -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 { diff --git a/Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift b/Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift index 53a8957..e9f2fc3 100644 --- a/Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift +++ b/Tests/SimpleHTTPTests/Core/HTTPRequestTests.swift @@ -5,6 +5,7 @@ // Created by Edvinas Byla on 14/05/2025. // +import Foundation import Testing import SimpleHTTP @@ -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 {