22
33namespace Tests ;
44
5+ use CodeIgniter \Config \Factories ;
56use CodeIgniter \Config \Services ;
67use CodeIgniter \HTTP \IncomingRequest ;
8+ use CodeIgniter \HTTP \SiteURIFactory ;
79use CodeIgniter \HTTP \URI ;
810use CodeIgniter \HTTP \UserAgent ;
911use CodeIgniter \I18n \Time ;
12+ use CodeIgniter \Superglobals ;
1013use CodeIgniter \Test \CIUnitTestCase ;
1114use Config \App ;
1215use Michalsn \CodeIgniterSignedUrl \Config \SignedUrl as SignedUrlConfig ;
1821 */
1922final class SignedUrlTest extends CIUnitTestCase
2023{
24+ private App $ config ;
25+
2126 protected function setUp (): void
2227 {
2328 parent ::setUp ();
2429
2530 Services::reset (true );
2631
32+ $ this ->config = new App ();
33+ $ this ->config ->baseURL = 'http://example.com/ ' ;
34+ $ this ->config ->indexPage = '' ;
35+
36+ $ _SERVER ['HTTP_HOST ' ] = 'example.com ' ;
37+ $ _SERVER ['REQUEST_URI ' ] = '/ ' ;
38+ $ _SERVER ['SCRIPT_NAME ' ] = '' ;
39+
2740 config ('Encryption ' )->key = hex2bin ('6ece79d55cd04503600bd97520a0138a067690112fbfb44c704b0c626a7c62a2 ' );
2841 }
2942
43+ private function createRequest (?App $ config = null , $ body = null , ?string $ path = null )
44+ {
45+ $ config ??= new App ();
46+
47+ $ factory = new SiteURIFactory ($ config , new Superglobals ());
48+ $ uri = $ factory ->createFromGlobals ();
49+
50+ if ($ path !== null ) {
51+ $ uri ->setPath ($ path );
52+ }
53+
54+ $ request = new IncomingRequest ($ config , $ uri , $ body , new UserAgent ());
55+
56+ Factories::injectMock ('config ' , 'App ' , $ config );
57+
58+ return $ request ;
59+ }
60+
3061 public function testIncorrectAlgorithm (): void
3162 {
3263 $ this ->expectException (SignedUrlException::class);
@@ -98,7 +129,7 @@ public function testSignWithNoExpirationInConfig(): void
98129 $ signedUrl = new SignedUrl ($ config );
99130 $ url = $ signedUrl ->sign ($ uri );
100131
101- $ expectedUrl .= '&signature=T3Y2OoBY2KvUbkTTBpPqjXFgs0k ' ;
132+ $ expectedUrl .= '&signature=ongZW4ttfJMqN757mwNXp5kx_3snwQhaDyI6JiV-5FM ' ;
102133
103134 $ this ->assertSame ($ expectedUrl , $ url );
104135 }
@@ -114,7 +145,7 @@ public function testSignWithIncludedAlgorithm(): void
114145 $ signedUrl = new SignedUrl ($ config );
115146 $ url = $ signedUrl ->sign ($ uri );
116147
117- $ expectedUrl .= '&algorithm=sha1 &signature=fBY7AIRdMqyhRwknzK3lPusRoWw ' ;
148+ $ expectedUrl .= '&algorithm=sha256 &signature=IldvSUQVJqTc8Gq47i0pEvuUYNjK_oRX1PAw-ZaXyM4 ' ;
118149
119150 $ this ->assertSame ($ expectedUrl , $ url );
120151 }
@@ -131,7 +162,7 @@ public function testSignWithExpirationFromConfig(): void
131162 $ signedUrl = new SignedUrl ($ config );
132163 $ url = $ signedUrl ->sign ($ uri );
133164
134- $ expectedUrl .= '&expires=1671980361&signature=ILQnUh4hW3O9qEM541lZFgexlB4 ' ;
165+ $ expectedUrl .= '&expires=1671980361&signature=qohLh7fvypmDF9vktdJ6DBXH6fiKyBezNQblosN2sbA ' ;
135166
136167 $ this ->assertSame ($ expectedUrl , $ url );
137168 }
@@ -148,20 +179,20 @@ public function testSignWithOverwritenExpirationFromConfig(): void
148179 $ signedUrl = new SignedUrl ($ config );
149180 $ url = $ signedUrl ->setExpiration (SECOND * 20 )->sign ($ uri );
150181
151- $ expectedUrl .= '&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI ' ;
182+ $ expectedUrl .= '&expires=1671980371&signature=IzHjHhkTOOBPTayZnk8f_ut0H4-3q0YrDb11slKPWWE ' ;
152183
153184 $ this ->assertSame ($ expectedUrl , $ url );
154185 }
155186
156187 public function testVerifyWithIndexPage (): void
157188 {
158- $ path = '/ index.php/path?query=string&signature=q0qKGOtgw3F153F1W3HZ0hUwxGc ' ;
159- $ url = 'https://example.com ' . $ path ;
189+ $ this -> config -> indexPage = 'index.php ' ;
190+ $ _SERVER [ ' SCRIPT_NAME ' ] = '/index.php ' ;
160191
161- $ _SERVER ['REQUEST_URI ' ] = $ path ;
192+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string&signature=joVnKjlHYIeuLtyUW5SnQ-US2FPkWkykZnSmf2D_RZY ' ;
193+
194+ $ request = $ this ->createRequest ($ this ->config );
162195
163- $ uri = new URI ($ url );
164- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
165196 $ config = new SignedUrlConfig ();
166197 $ signedUrl = new SignedUrl ($ config );
167198
@@ -172,13 +203,10 @@ public function testVerifyWithIndexPage(): void
172203
173204 public function testVerifyWithoutExpiration (): void
174205 {
175- $ path = '/path?query=string&signature=9IOk6sKK9VmpboZXQCFa-Xv2BEE ' ;
176- $ url = 'https://example.com ' . $ path ;
206+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string&signature=iBEmAoQ9cPafZ3N05b9jEMj906Nd5nmSsJV7rKzFZSY ' ;
177207
178- $ _SERVER [ ' REQUEST_URI ' ] = $ path ;
208+ $ request = $ this -> createRequest ( $ this -> config ) ;
179209
180- $ uri = new URI ($ url );
181- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
182210 $ config = new SignedUrlConfig ();
183211 $ signedUrl = new SignedUrl ($ config );
184212
@@ -189,13 +217,9 @@ public function testVerifyWithoutExpiration(): void
189217
190218 public function testVerifyWithExpiration (): void
191219 {
192- $ path = '/path?query=string&expires=1671980371&signature=VQ1Nu3FAYcKKO3FrdmjFLk6PxNQ ' ;
193- $ url = 'https://example.com ' . $ path ;
194-
195- $ _SERVER ['REQUEST_URI ' ] = $ path ;
220+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string&expires=1671980371&signature=9GNwvgcsK7jJUPpXe3MK5xFbE0rb5ZBHIjKc1qqWSgU ' ;
196221
197- $ uri = new URI ($ url );
198- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
222+ $ request = $ this ->createRequest ($ this ->config );
199223
200224 $ config = new SignedUrlConfig ();
201225 $ signedUrl = new SignedUrl ($ config );
@@ -210,13 +234,9 @@ public function testVerifyThrowExceptionForMissingSignature(): void
210234 $ this ->expectException (SignedUrlException::class);
211235 $ this ->expectExceptionMessage ('This URL have to be signed. ' );
212236
213- $ path = '/path?query=string ' ;
214- $ url = 'https://example.com ' . $ path ;
237+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string ' ;
215238
216- $ _SERVER ['REQUEST_URI ' ] = $ path ;
217-
218- $ uri = new URI ($ url );
219- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
239+ $ request = $ this ->createRequest ($ this ->config );
220240
221241 $ config = new SignedUrlConfig ();
222242 $ signedUrl = new SignedUrl ($ config );
@@ -228,13 +248,9 @@ public function testVerifyThrowExceptionForInvalidAlgorithm(): void
228248 $ this ->expectException (SignedUrlException::class);
229249 $ this ->expectExceptionMessage ('Algorithm is invalid or not supported. ' );
230250
231- $ path = '/path?query=string&algorithm=fake&signature=fake ' ;
232- $ url = 'https://example.com ' . $ path ;
233-
234- $ _SERVER ['REQUEST_URI ' ] = $ path ;
251+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string&algorithm=fake&signature=fake ' ;
235252
236- $ uri = new URI ($ url );
237- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
253+ $ request = $ this ->createRequest ($ this ->config );
238254
239255 $ config = new SignedUrlConfig ();
240256 $ signedUrl = new SignedUrl ($ config );
@@ -246,13 +262,9 @@ public function testVerifyThrowExceptionForUrlNotValid(): void
246262 $ this ->expectException (SignedUrlException::class);
247263 $ this ->expectExceptionMessage ('URL is not valid. ' );
248264
249- $ path = '/path?query=string123&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI ' ;
250- $ url = 'https://example.com ' . $ path ;
265+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string123&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI ' ;
251266
252- $ _SERVER ['REQUEST_URI ' ] = $ path ;
253-
254- $ uri = new URI ($ url );
255- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
267+ $ request = $ this ->createRequest ($ this ->config );
256268
257269 $ config = new SignedUrlConfig ();
258270 $ signedUrl = new SignedUrl ($ config );
@@ -264,13 +276,9 @@ public function testVerifyThrowExceptionForExpiredUrl(): void
264276 $ this ->expectException (SignedUrlException::class);
265277 $ this ->expectExceptionMessage ('This URL has expired. ' );
266278
267- $ path = '/path?query=string&expires=1671980371&signature=VQ1Nu3FAYcKKO3FrdmjFLk6PxNQ ' ;
268- $ url = 'https://example.com ' . $ path ;
269-
270- $ _SERVER ['REQUEST_URI ' ] = $ path ;
279+ $ _SERVER ['REQUEST_URI ' ] = '/path?query=string&expires=1671980371&signature=9GNwvgcsK7jJUPpXe3MK5xFbE0rb5ZBHIjKc1qqWSgU ' ;
271280
272- $ uri = new URI ($ url );
273- $ request = new IncomingRequest (new App (), $ uri , null , new UserAgent ());
281+ $ request = $ this ->createRequest ($ this ->config );
274282
275283 Time::setTestNow ('2022-12-25 15:59:11 ' , 'UTC ' );
276284
0 commit comments