@@ -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