|
5 | 5 | // Created by Edvinas Byla on 14/05/2025. |
6 | 6 | // |
7 | 7 |
|
| 8 | +import Foundation |
8 | 9 | import Testing |
9 | 10 | import SimpleHTTP |
10 | 11 |
|
@@ -47,6 +48,53 @@ struct HTTPRequestTests { |
47 | 48 | request[option: ToggleOption.self] = true |
48 | 49 | #expect(request[option: ToggleOption.self] == true) |
49 | 50 | } |
| 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 | + } |
50 | 98 | } |
51 | 99 |
|
52 | 100 | fileprivate enum ToggleOption: HTTPRequestOption { |
|
0 commit comments