@@ -24,21 +24,38 @@ public function testSign()
24
24
$ this ->assertStringContainsString ('?_hash= ' , $ signer ->sign ('http://example.com/foo ' ));
25
25
$ this ->assertStringContainsString ('?_hash= ' , $ signer ->sign ('http://example.com/foo?foo=bar ' ));
26
26
$ 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 ));
27
33
}
28
34
29
35
public function testCheck ()
30
36
{
31
37
$ signer = new UriSigner ('foobar ' );
32
38
39
+ $ this ->assertFalse ($ signer ->check ('http://example.com/foo ' ));
33
40
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?_hash=foo ' ));
34
41
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo ' ));
35
42
$ this ->assertFalse ($ signer ->check ('http://example.com/foo?foo=bar&_hash=foo&bar=foo ' ));
36
43
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
+
37
49
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo ' )));
38
50
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar ' )));
39
51
$ this ->assertTrue ($ signer ->check ($ signer ->sign ('http://example.com/foo?foo=bar&0=integer ' )));
40
52
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
+
41
57
$ 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 ));
42
59
}
43
60
44
61
public function testCheckWithDifferentArgSeparator ()
@@ -51,6 +68,12 @@ public function testCheckWithDifferentArgSeparator()
51
68
$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
52
69
);
53
70
$ 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 ' ))));
54
77
}
55
78
56
79
public function testCheckWithRequest ()
@@ -60,17 +83,27 @@ public function testCheckWithRequest()
60
83
$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo ' ))));
61
84
$ this ->assertTrue ($ signer ->checkRequest (Request::create ($ signer ->sign ('http://example.com/foo?foo=bar ' ))));
62
85
$ 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 ' )))));
63
90
}
64
91
65
92
public function testCheckWithDifferentParameter ()
66
93
{
67
- $ signer = new UriSigner ('foobar ' , 'qux ' );
94
+ $ signer = new UriSigner ('foobar ' , 'qux ' , ' abc ' );
68
95
69
96
$ this ->assertSame (
70
97
'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D ' ,
71
98
$ signer ->sign ('http://example.com/foo?foo=bar&baz=bay ' )
72
99
);
73
100
$ 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 ' ))));
74
107
}
75
108
76
109
public function testSignerWorksWithFragments ()
@@ -81,6 +114,79 @@ public function testSignerWorksWithFragments()
81
114
'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar ' ,
82
115
$ signer ->sign ('http://example.com/foo?bar=foo&foo=bar#foobar ' )
83
116
);
117
+
84
118
$ 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 ));
85
191
}
86
192
}
0 commit comments