12
12
namespace Symfony \Component \HttpFoundation \Tests ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \HttpFoundation \Exception \LogicException ;
15
16
use Symfony \Component \HttpFoundation \Request ;
16
17
use Symfony \Component \HttpFoundation \UriSigner ;
17
18
@@ -24,21 +25,38 @@ public function testSign()
24
25
$ this ->assertStringContainsString ('?_hash= ' , $ signer ->sign ('http://example.com/foo ' ));
25
26
$ this ->assertStringContainsString ('?_hash= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' ));
26
27
$ this ->assertStringContainsString ('&foo= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' ));
28
+
29
+ $ this ->assertStringContainsString ('?_expiration= ' , $ signer ->sign ('http://example.com/foo ' , 1 ));
30
+ $ this ->assertStringContainsString ('&_hash= ' , $ signer ->sign ('http://example.com/foo ' , 1 ));
31
+ $ this ->assertStringContainsString ('?_expiration= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' , 1 ));
32
+ $ this ->assertStringContainsString ('&_hash= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' , 1 ));
33
+ $ this ->assertStringContainsString ('&foo= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' , 1 ));
27
34
}
28
35
29
36
public function testCheck ()
30
37
{
31
38
$ signer = new UriSigner ('foobar ' );
32
39
40
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo ' ));
33
41
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?_hash=foo ' ));
34
42
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo ' ));
35
43
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo&bar=foo ' ));
36
44
45
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800 ' ));
46
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800?_hash=foo ' ));
47
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo ' ));
48
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo?_expiration=4070908800&foo=bar&_hash=foo&bar=foo ' ));
49
+
37
50
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo ' )));
38
51
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' )));
39
52
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' )));
40
53
54
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
55
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
56
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
57
+
41
58
$ this ->assertSame ($ signer ->sign ('http://example.com/foo?foo=bar&bar=foo ' ), $ signer ->sign ('http://example.com/foo?bar=foo&foo=bar ' ));
59
+ $ this ->assertSame ($ signer ->sign ('http://example.com/foo?foo=bar&bar=foo ' , 1 ), $ signer ->sign ('http://example.com/foo?bar=foo&foo=bar ' , 1 ));
42
60
}
43
61
44
62
public function testCheckWithDifferentArgSeparator ()
@@ -51,6 +69,12 @@ public function testCheckWithDifferentArgSeparator()
51
69
$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
52
70
);
53
71
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )));
72
+
73
+ $ this ->assertSame (
74
+ 'http://example.com/foo?_expiration=4070908800&_hash=xfui5FoP0vbD9Cp7pI0tHnqR1Fmj2UARqkIUw7SZVfQ%3D&baz=bay&foo=bar ' ,
75
+ $ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
76
+ );
77
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
54
78
}
55
79
56
80
public function testCheckWithRequest ()
@@ -60,17 +84,27 @@ public function testCheckWithRequest()
60
84
$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo ' ))));
61
85
$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar ' ))));
62
86
$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' ))));
87
+
88
+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
89
+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
90
+ $ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' )))));
63
91
}
64
92
65
93
public function testCheckWithDifferentParameter ()
66
94
{
67
- $ signer = new UriSigner ('foobar ' , 'qux ' );
95
+ $ signer = new UriSigner ('foobar ' , 'qux ' , ' abc ' );
68
96
69
97
$ this ->assertSame (
70
98
'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D ' ,
71
99
$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
72
100
);
73
101
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )));
102
+
103
+ $ this ->assertSame (
104
+ 'http://example.com/foo?abc=4070908800&baz=bay&foo=bar&qux=hdhUhBVPpzKJdz5ZjC%2FkLvtOYdGKOvKVOczmmMIZK0A%3D ' ,
105
+ $ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
106
+ );
107
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
74
108
}
75
109
76
110
public function testSignerWorksWithFragments ()
@@ -81,6 +115,79 @@ public function testSignerWorksWithFragments()
81
115
'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar ' ,
82
116
$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' )
83
117
);
118
+
84
119
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' )));
120
+
121
+ $ this ->assertSame (
122
+ 'http://example.com/foo?_expiration=4070908800&_hash=qHl626U5d7LMsVtBxPt9GNzysdSxyOQ1fHA59Y1ib0Y%3D&bar=foo&foo=bar#foobar ' ,
123
+ $ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))
124
+ );
125
+
126
+ $ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ))));
127
+ }
128
+
129
+ public function testSignWithUriExpiration ()
130
+ {
131
+ $ signer = new UriSigner ('foobar ' );
132
+
133
+ $ 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 ));
134
+ }
135
+
136
+ public function testSignWithoutExpirationAndWithReservedHashParameter ()
137
+ {
138
+ $ signer = new UriSigner ('foobar ' );
139
+
140
+ $ this ->expectException (LogicException::class);
141
+
142
+ $ signer ->sign ('http://example.com/foo?_hash=bar ' );
143
+ }
144
+
145
+ public function testSignWithoutExpirationAndWithReservedParameter ()
146
+ {
147
+ $ signer = new UriSigner ('foobar ' );
148
+
149
+ $ this ->expectException (LogicException::class);
150
+
151
+ $ signer ->sign ('http://example.com/foo?_expiration=4070908800 ' );
152
+ }
153
+
154
+ public function testSignWithExpirationAndWithReservedHashParameter ()
155
+ {
156
+ $ signer = new UriSigner ('foobar ' );
157
+
158
+ $ this ->expectException (LogicException::class);
159
+
160
+ $ signer ->sign ('http://example.com/foo?_hash=bar ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ));
161
+ }
162
+
163
+ public function testSignWithExpirationAndWithReservedParameter ()
164
+ {
165
+ $ signer = new UriSigner ('foobar ' );
166
+
167
+ $ this ->expectException (LogicException::class);
168
+
169
+ $ signer ->sign ('http://example.com/foo?_expiration=4070908800 ' , new \DateTimeImmutable ('2099-01-01 00:00:00 ' ));
170
+ }
171
+
172
+ public function testCheckWithUriExpiration ()
173
+ {
174
+ $ signer = new UriSigner ('foobar ' );
175
+
176
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo ' , new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
177
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' , new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
178
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' , new \DateTimeImmutable ('2000-01-01 00:00:00 ' ))));
179
+
180
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo ' , 1577836800 ))); // 2000-01-01
181
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' , 1577836800 ))); // 2000-01-01
182
+ $ this ->assertFalse ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' , 1577836800 ))); // 2000-01-01
183
+
184
+ $ relativeUriFromNow1 = $ signer ->sign ('http://example.com/foo ' , new \DateInterval ('PT3S ' ));
185
+ $ relativeUriFromNow2 = $ signer ->sign ('http://example.com/foo?foo=bar ' , new \DateInterval ('PT3S ' ));
186
+ $ relativeUriFromNow3 = $ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' , new \DateInterval ('PT3S ' ));
187
+ sleep (10 );
188
+
189
+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow1 ));
190
+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow2 ));
191
+ $ this ->assertFalse ($ signer ->check ($ relativeUriFromNow3 ));
85
192
}
86
193
}
0 commit comments