@@ -24,21 +24,38 @@ public function testSign()
2424$ this ->assertStringContainsString ('?_hash= ' ,$ signer ->sign ('http://example.com/foo ' ));
2525$ this ->assertStringContainsString ('?_hash= ' ,$ signer ->sign ('http://example.com/foo?foo=bar ' ));
2626$ this ->assertStringContainsString ('&foo= ' ,$ signer ->sign ('http://example.com/foo?foo=bar ' ));
27+
28+ $ this ->assertStringContainsString ('?_expiration= ' ,$ signer ->sign ('http://example.com/foo ' ,1 ));
29+ $ this ->assertStringContainsString ('&_hash= ' ,$ signer ->sign ('http://example.com/foo ' ,1 ));
30+ $ this ->assertStringContainsString ('?_expiration= ' ,$ signer ->sign ('http://example.com/foo?foo=bar ' ,1 ));
31+ $ this ->assertStringContainsString ('&_hash= ' ,$ signer ->sign ('http://example.com/foo?foo=bar ' ,1 ));
32+ $ this ->assertStringContainsString ('&foo= ' ,$ signer ->sign ('http://example.com/foo?foo=bar ' ,1 ));
2733 }
2834
2935public function testCheck ()
3036 {
3137$ signer =new UriSigner ('foobar ' );
3238
39+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo ' ));
3340$ this ->assertFalse ($ signer ->check ('http://example.com/foo?_hash=foo ' ));
3441$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo ' ));
3542$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo&bar=foo ' ));
3643
44+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800 ' ));
45+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800?_hash=foo ' ));
46+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo ' ));
47+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo&bar=foo ' ));
48+
3749$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo ' )));
3850$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' )));
3951$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' )));
4052
53+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
54+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
55+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
56+
4157$ this ->assertSame ($ signer ->sign ('http://example.com/foo?foo=bar&bar=foo ' ),$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar ' ));
58+ $ this ->assertSame ($ signer ->sign ('http://example.com/foo?foo=bar&bar=foo ' ,1 ),$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar ' ,1 ));
4259 }
4360
4461public function testCheckWithDifferentArgSeparator ()
@@ -51,6 +68,12 @@ public function testCheckWithDifferentArgSeparator()
5168$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
5269 );
5370$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )));
71+
72+ $ this ->assertSame (
73+ 'http://example.com/foo?_expiration=4070908800&_hash=xfui5FoP0vbD9Cp7pI0tHnqR1Fmj2UARqkIUw7SZVfQ%3D&baz=bay&foo=bar ' ,
74+ $ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
75+ );
76+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
5477 }
5578
5679public function testCheckWithRequest ()
@@ -60,17 +83,27 @@ public function testCheckWithRequest()
6083$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo ' ))));
6184$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar ' ))));
6285$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ))));
86+
87+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
88+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
89+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
6390 }
6491
6592public function testCheckWithDifferentParameter ()
6693 {
67- $ signer =new UriSigner ('foobar ' ,'qux ' );
94+ $ signer =new UriSigner ('foobar ' ,'qux ' , ' abc ' );
6895
6996$ this ->assertSame (
7097'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D ' ,
7198$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
7299 );
73100$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )));
101+
102+ $ this ->assertSame (
103+ 'http://example.com/foo?abc=4070908800&baz=bay&foo=bar&qux=hdhUhBVPpzKJdz5ZjC%2FkLvtOYdGKOvKVOczmmMIZK0A%3D ' ,
104+ $ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
105+ );
106+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
74107 }
75108
76109public function testSignerWorksWithFragments ()
@@ -81,6 +114,79 @@ public function testSignerWorksWithFragments()
81114'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar ' ,
82115$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' )
83116 );
117+
84118$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' )));
119+
120+ $ this ->assertSame (
121+ 'http://example.com/foo?_expiration=4070908800&_hash=qHl626U5d7LMsVtBxPt9GNzysdSxyOQ1fHA59Y1ib0Y%3D&bar=foo&foo=bar#foobar ' ,
122+ $ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
123+ );
124+
125+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
126+ }
127+
128+ public function testSignWithUriExpiration ()
129+ {
130+ $ signer =new UriSigner ('foobar ' );
131+
132+ $ this ->assertSame ($ signer ->sign ('http://example.com/foo?foo=bar&bar=foo ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' )),$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar ' ,4070908800 ));
133+ }
134+
135+ public function testSignWithoutExpirationAndWithReservedHashParameter ()
136+ {
137+ $ signer =new UriSigner ('foobar ' );
138+
139+ $ this ->expectException (\LogicException::class);
140+
141+ $ signer ->sign ('http://example.com/foo?_hash=bar ' );
142+ }
143+
144+ public function testSignWithoutExpirationAndWithReservedParameter ()
145+ {
146+ $ signer =new UriSigner ('foobar ' );
147+
148+ $ this ->expectException (\LogicException::class);
149+
150+ $ signer ->sign ('http://example.com/foo?_expiration=4070908800 ' );
151+ }
152+
153+ public function testSignWithExpirationAndWithReservedHashParameter ()
154+ {
155+ $ signer =new UriSigner ('foobar ' );
156+
157+ $ this ->expectException (\LogicException::class);
158+
159+ $ signer ->sign ('http://example.com/foo?_hash=bar ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ));
160+ }
161+
162+ public function testSignWithExpirationAndWithReservedParameter ()
163+ {
164+ $ signer =new UriSigner ('foobar ' );
165+
166+ $ this ->expectException (\LogicException::class);
167+
168+ $ signer ->sign ('http://example.com/foo?_expiration=4070908800 ' ,new \DateTimeImmutable ('2099-01-01 00:00:00 ' ));
169+ }
170+
171+ public function testCheckWithUriExpiration ()
172+ {
173+ $ signer =new UriSigner ('foobar ' );
174+
175+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo ' ,new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
176+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' ,new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
177+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ,new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
178+
179+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo ' ,1577836800 )));// 2000-01-01
180+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' ,1577836800 )));// 2000-01-01
181+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ,1577836800 )));// 2000-01-01
182+
183+ $ relativeUriFromNow1 =$ signer ->sign ('http://example.com/foo ' ,new \DateInterval ('PT3S ' ));
184+ $ relativeUriFromNow2 =$ signer ->sign ('http://example.com/foo?foo=bar ' ,new \DateInterval ('PT3S ' ));
185+ $ relativeUriFromNow3 =$ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ,new \DateInterval ('PT3S ' ));
186+ sleep (10 );
187+
188+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow1 ));
189+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow2 ));
190+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow3 ));
85191 }
86192}