Skip to content

Commit a9f846e

Browse files
authored
Fix A Trailing '?' When Passing Empty Query Items (#12)
2 parents 8a9f6ee + 42f69d8 commit a9f846e

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

Sources/MicroClient/Extensions/URLComponents.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ extension URL {
1717
resolvingAgainstBaseURL: false
1818
)
1919

20-
components?.queryItems = networkRequest.queryItems
20+
let queryItems = networkRequest
21+
.queryItems
2122
.filter { $0.value != nil }
2223

24+
if !queryItems.isEmpty {
25+
components?.queryItems = queryItems
26+
}
27+
2328
return try unwrap(
2429
value: components?.url,
2530
error: NetworkClientError.malformedURL

Tests/MicroClientTests/NetworkRequestQueryItemsTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,72 @@ struct NetworkRequestQueryItemsTests {
5050
"It should preserve nil values in query items"
5151
)
5252
}
53+
54+
@Test("It should not add a trailing '?' when there are no query items")
55+
func noTrailingQuestionMark() throws {
56+
let configuration = NetworkClientMother.makeNetworkConfiguration()
57+
58+
let request = NetworkRequest<VoidRequest, VoidResponse>(
59+
path: "/v1/endpoint",
60+
method: .get
61+
)
62+
63+
let url = try URL.makeURL(
64+
configuration: configuration,
65+
networkRequest: request
66+
)
67+
68+
#expect(
69+
url.absoluteString == "https://api.example.com/v1/endpoint",
70+
"It should not have a trailing '?'"
71+
)
72+
}
73+
74+
@Test("It should add a trailing '?' when an empty queryItems is provided")
75+
func trailingQuestionMarkWithEmptyQueryItems() throws {
76+
let configuration = NetworkClientMother.makeNetworkConfiguration()
77+
78+
let request = NetworkRequest<VoidRequest, VoidResponse>(
79+
path: "/v1/endpoint",
80+
method: .get,
81+
queryItems: []
82+
)
83+
84+
let url = try URL.makeURL(
85+
configuration: configuration,
86+
networkRequest: request
87+
)
88+
89+
#expect(
90+
url.absoluteString == "https://api.example.com/v1/endpoint",
91+
"It should not have a trailing '?'"
92+
)
93+
}
94+
95+
@Test("It should add '?' and query items when query items are present")
96+
func addQuestionMarkWithQueryItems() throws {
97+
let configuration = NetworkClientMother.makeNetworkConfiguration()
98+
99+
let queryItems = [
100+
URLQueryItem(name: "page", value: "1"),
101+
URLQueryItem(name: "limit", value: "10"),
102+
URLQueryItem(name: "sort", value: "name")
103+
]
104+
105+
let request = NetworkRequest<VoidRequest, VoidResponse>(
106+
path: "/v1/endpoint",
107+
method: .get,
108+
queryItems: queryItems
109+
)
110+
111+
let url = try URL.makeURL(
112+
configuration: configuration,
113+
networkRequest: request
114+
)
115+
116+
#expect(
117+
url.absoluteString == "https://api.example.com/v1/endpoint?page=1&limit=10&sort=name",
118+
"It should add the '?' and query items"
119+
)
120+
}
53121
}

0 commit comments

Comments
 (0)