Skip to content

Commit 299105a

Browse files
authored
chore: add tests for latest fixes (#512)
1 parent 3936842 commit 299105a

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

tests/JWTTest.php

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public function testValidToken()
7676
$this->assertSame($decoded->message, 'abc');
7777
}
7878

79+
/**
80+
* @runInSeparateProcess
81+
*/
7982
public function testValidTokenWithLeeway()
8083
{
8184
JWT::$leeway = 60;
@@ -86,9 +89,11 @@ public function testValidTokenWithLeeway()
8689
$encoded = JWT::encode($payload, 'my_key', 'HS256');
8790
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
8891
$this->assertSame($decoded->message, 'abc');
89-
JWT::$leeway = 0;
9092
}
9193

94+
/**
95+
* @runInSeparateProcess
96+
*/
9297
public function testExpiredTokenWithLeeway()
9398
{
9499
JWT::$leeway = 60;
@@ -100,7 +105,6 @@ public function testExpiredTokenWithLeeway()
100105
$encoded = JWT::encode($payload, 'my_key', 'HS256');
101106
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
102107
$this->assertSame($decoded->message, 'abc');
103-
JWT::$leeway = 0;
104108
}
105109

106110
public function testValidTokenWithNbf()
@@ -116,6 +120,9 @@ public function testValidTokenWithNbf()
116120
$this->assertSame($decoded->message, 'abc');
117121
}
118122

123+
/**
124+
* @runInSeparateProcess
125+
*/
119126
public function testValidTokenWithNbfLeeway()
120127
{
121128
JWT::$leeway = 60;
@@ -126,9 +133,11 @@ public function testValidTokenWithNbfLeeway()
126133
$encoded = JWT::encode($payload, 'my_key', 'HS256');
127134
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
128135
$this->assertSame($decoded->message, 'abc');
129-
JWT::$leeway = 0;
130136
}
131137

138+
/**
139+
* @runInSeparateProcess
140+
*/
132141
public function testInvalidTokenWithNbfLeeway()
133142
{
134143
JWT::$leeway = 60;
@@ -139,9 +148,45 @@ public function testInvalidTokenWithNbfLeeway()
139148
$encoded = JWT::encode($payload, 'my_key', 'HS256');
140149
$this->expectException(BeforeValidException::class);
141150
JWT::decode($encoded, new Key('my_key', 'HS256'));
142-
JWT::$leeway = 0;
143151
}
144152

153+
public function testValidTokenWithNbfIgnoresIat()
154+
{
155+
$payload = [
156+
'message' => 'abc',
157+
'nbf' => time() - 20, // time in the future
158+
'iat' => time() + 20, // time in the past
159+
];
160+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
161+
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
162+
$this->assertEquals('abc', $decoded->message);
163+
}
164+
165+
public function testValidTokenWithNbfMicrotime()
166+
{
167+
$payload = [
168+
'message' => 'abc',
169+
'nbf' => microtime(true), // use microtime
170+
];
171+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
172+
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
173+
$this->assertEquals('abc', $decoded->message);
174+
}
175+
176+
public function testInvalidTokenWithNbfMicrotime()
177+
{
178+
$this->expectException(BeforeValidException::class);
179+
$payload = [
180+
'message' => 'abc',
181+
'nbf' => microtime(true) + 20, // use microtime in the future
182+
];
183+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
184+
JWT::decode($encoded, new Key('my_key', 'HS256'));
185+
}
186+
187+
/**
188+
* @runInSeparateProcess
189+
*/
145190
public function testValidTokenWithIatLeeway()
146191
{
147192
JWT::$leeway = 60;
@@ -152,9 +197,11 @@ public function testValidTokenWithIatLeeway()
152197
$encoded = JWT::encode($payload, 'my_key', 'HS256');
153198
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
154199
$this->assertSame($decoded->message, 'abc');
155-
JWT::$leeway = 0;
156200
}
157201

202+
/**
203+
* @runInSeparateProcess
204+
*/
158205
public function testInvalidTokenWithIatLeeway()
159206
{
160207
JWT::$leeway = 60;
@@ -165,7 +212,28 @@ public function testInvalidTokenWithIatLeeway()
165212
$encoded = JWT::encode($payload, 'my_key', 'HS256');
166213
$this->expectException(BeforeValidException::class);
167214
JWT::decode($encoded, new Key('my_key', 'HS256'));
168-
JWT::$leeway = 0;
215+
}
216+
217+
public function testValidTokenWithIatMicrotime()
218+
{
219+
$payload = [
220+
'message' => 'abc',
221+
'iat' => microtime(true), // use microtime
222+
];
223+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
224+
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
225+
$this->assertEquals('abc', $decoded->message);
226+
}
227+
228+
public function testInvalidTokenWithIatMicrotime()
229+
{
230+
$this->expectException(BeforeValidException::class);
231+
$payload = [
232+
'message' => 'abc',
233+
'iat' => microtime(true) + 20, // use microtime in the future
234+
];
235+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
236+
JWT::decode($encoded, new Key('my_key', 'HS256'));
169237
}
170238

171239
public function testInvalidToken()

0 commit comments

Comments
 (0)