|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -class JWTTest extends PHPUnit_Framework_TestCase { |
4 |
| - function testEncodeDecode() { |
5 |
| - $msg = JWT::encode('abc', 'my_key'); |
6 |
| - $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc'); |
7 |
| - } |
| 3 | +class JWTTest extends PHPUnit_Framework_TestCase |
| 4 | +{ |
| 5 | + public function testEncodeDecode() |
| 6 | + { |
| 7 | + $msg = JWT::encode('abc', 'my_key'); |
| 8 | + $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc'); |
| 9 | + } |
8 | 10 |
|
9 |
| - function testDecodeFromPython() { |
10 |
| - $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; |
11 |
| - $this->assertEquals( |
12 |
| - JWT::decode($msg, 'my_key'), |
13 |
| - '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123' |
14 |
| - ); |
15 |
| - } |
| 11 | + public function testDecodeFromPython() |
| 12 | + { |
| 13 | + $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; |
| 14 | + $this->assertEquals( |
| 15 | + JWT::decode($msg, 'my_key'), |
| 16 | + '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123' |
| 17 | + ); |
| 18 | + } |
16 | 19 |
|
17 |
| - function testUrlSafeCharacters() { |
18 |
| - $encoded = JWT::encode('f?', 'a'); |
19 |
| - $this->assertEquals('f?', JWT::decode($encoded, 'a')); |
20 |
| - } |
| 20 | + public function testUrlSafeCharacters() |
| 21 | + { |
| 22 | + $encoded = JWT::encode('f?', 'a'); |
| 23 | + $this->assertEquals('f?', JWT::decode($encoded, 'a')); |
| 24 | + } |
21 | 25 |
|
22 |
| - function testMalformedUtf8StringsFail() { |
23 |
| - $this->setExpectedException('DomainException'); |
24 |
| - JWT::encode(pack('c', 128), 'a'); |
25 |
| - } |
| 26 | + public function testMalformedUtf8StringsFail() |
| 27 | + { |
| 28 | + $this->setExpectedException('DomainException'); |
| 29 | + JWT::encode(pack('c', 128), 'a'); |
| 30 | + } |
26 | 31 |
|
27 |
| - function testMalformedJsonThrowsException() { |
28 |
| - $this->setExpectedException('DomainException'); |
29 |
| - JWT::jsonDecode('this is not valid JSON string'); |
30 |
| - } |
| 32 | + public function testMalformedJsonThrowsException() |
| 33 | + { |
| 34 | + $this->setExpectedException('DomainException'); |
| 35 | + JWT::jsonDecode('this is not valid JSON string'); |
| 36 | + } |
31 | 37 |
|
32 |
| - function testExpiredToken() { |
33 |
| - $this->setExpectedException('UnexpectedValueException'); |
34 |
| - $payload = array( |
35 |
| - "message" => "abc", |
36 |
| - "exp" => time() - 20); // time in the past |
37 |
| - $encoded = JWT::encode($payload, 'my_key'); |
38 |
| - JWT::decode($encoded); |
39 |
| - } |
| 38 | + public function testExpiredToken() |
| 39 | + { |
| 40 | + $this->setExpectedException('UnexpectedValueException'); |
| 41 | + $payload = array( |
| 42 | + "message" => "abc", |
| 43 | + "exp" => time() - 20); // time in the past |
| 44 | + $encoded = JWT::encode($payload, 'my_key'); |
| 45 | + JWT::decode($encoded); |
| 46 | + } |
40 | 47 |
|
41 |
| - function testValidToken() { |
42 |
| - $payload = array( |
43 |
| - "message" => "abc", |
44 |
| - "exp" => time() + 20); // time in the future |
45 |
| - $encoded = JWT::encode($payload, 'my_key'); |
46 |
| - $decoded = JWT::decode($encoded, 'my_key'); |
47 |
| - $this->assertEquals($decoded->message, 'abc'); |
48 |
| - } |
| 48 | + public function testValidToken() |
| 49 | + { |
| 50 | + $payload = array( |
| 51 | + "message" => "abc", |
| 52 | + "exp" => time() + 20); // time in the future |
| 53 | + $encoded = JWT::encode($payload, 'my_key'); |
| 54 | + $decoded = JWT::decode($encoded, 'my_key'); |
| 55 | + $this->assertEquals($decoded->message, 'abc'); |
| 56 | + } |
49 | 57 |
|
50 |
| - function testInvalidToken() { |
51 |
| - $payload = array( |
52 |
| - "message" => "abc", |
53 |
| - "exp" => time() + 20); // time in the future |
54 |
| - $encoded = JWT::encode($payload, 'my_key'); |
55 |
| - $this->setExpectedException('UnexpectedValueException'); |
56 |
| - $decoded = JWT::decode($encoded, 'my_key2'); |
57 |
| - } |
| 58 | + public function testInvalidToken() |
| 59 | + { |
| 60 | + $payload = array( |
| 61 | + "message" => "abc", |
| 62 | + "exp" => time() + 20); // time in the future |
| 63 | + $encoded = JWT::encode($payload, 'my_key'); |
| 64 | + $this->setExpectedException('UnexpectedValueException'); |
| 65 | + $decoded = JWT::decode($encoded, 'my_key2'); |
| 66 | + } |
58 | 67 |
|
59 |
| - function testRSEncodeDecode() { |
60 |
| - $privKey = openssl_pkey_new(array('digest_alg' => 'sha256', |
61 |
| - 'private_key_bits' => 1024, |
62 |
| - 'private_key_type' => OPENSSL_KEYTYPE_RSA)); |
63 |
| - $msg = JWT::encode('abc', $privKey, 'RS256'); |
64 |
| - $pubKey = openssl_pkey_get_details($privKey); |
65 |
| - $pubKey = $pubKey['key']; |
66 |
| - $decoded = JWT::decode($msg, $pubKey, true); |
67 |
| - $this->assertEquals($decoded, 'abc'); |
68 |
| - } |
69 |
| - |
70 |
| - function testKIDChooser() { |
71 |
| - $keys = array('1' => 'my_key', '2' => 'my_key2'); |
72 |
| - $msg = JWT::encode('abc', $keys['1'], 'HS256', '1'); |
73 |
| - $decoded = JWT::decode($msg, $keys, true); |
74 |
| - $this->assertEquals($decoded, 'abc'); |
75 |
| - } |
| 68 | + public function testRSEncodeDecode() |
| 69 | + { |
| 70 | + $privKey = openssl_pkey_new(array('digest_alg' => 'sha256', |
| 71 | + 'private_key_bits' => 1024, |
| 72 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA)); |
| 73 | + $msg = JWT::encode('abc', $privKey, 'RS256'); |
| 74 | + $pubKey = openssl_pkey_get_details($privKey); |
| 75 | + $pubKey = $pubKey['key']; |
| 76 | + $decoded = JWT::decode($msg, $pubKey, true); |
| 77 | + $this->assertEquals($decoded, 'abc'); |
| 78 | + } |
76 | 79 |
|
| 80 | + public function testKIDChooser() |
| 81 | + { |
| 82 | + $keys = array('1' => 'my_key', '2' => 'my_key2'); |
| 83 | + $msg = JWT::encode('abc', $keys['1'], 'HS256', '1'); |
| 84 | + $decoded = JWT::decode($msg, $keys, true); |
| 85 | + $this->assertEquals($decoded, 'abc'); |
| 86 | + } |
77 | 87 | }
|
78 |
| - |
79 |
| -?> |
|
0 commit comments