summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChris Raynor <chris@firebase.com>2014-11-14 14:32:03 -0800
committerChris Raynor <chris@firebase.com>2014-11-14 14:32:03 -0800
commitc20a3cb3faf81ec9716449b6c07c27b14f52bc75 (patch)
tree7c2e8f613cd50c57740cc5a317df3254fcdf8bd4 /tests
parent0cc1ae69c2040c6afd4c0bfc30fae727ccfb016f (diff)
parent301e6bc0f183c71e16748310d1a96cfa94ddc3fb (diff)
downloadphp-jwt-c20a3cb3faf81ec9716449b6c07c27b14f52bc75.zip
php-jwt-c20a3cb3faf81ec9716449b6c07c27b14f52bc75.tar.gz
php-jwt-c20a3cb3faf81ec9716449b6c07c27b14f52bc75.tar.bz2
Merge pull request #23 from firebase/integration
This closes #22 and #21
Diffstat (limited to 'tests')
-rw-r--r--tests/JWTTest.php161
-rw-r--r--tests/autoload.php.dist2
2 files changed, 96 insertions, 67 deletions
diff --git a/tests/JWTTest.php b/tests/JWTTest.php
index 2149862..5a76ed4 100644
--- a/tests/JWTTest.php
+++ b/tests/JWTTest.php
@@ -1,79 +1,108 @@
<?php
-class JWTTest extends PHPUnit_Framework_TestCase {
- function testEncodeDecode() {
- $msg = JWT::encode('abc', 'my_key');
- $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc');
- }
+class JWTTest extends PHPUnit_Framework_TestCase
+{
+ public function testEncodeDecode()
+ {
+ $msg = JWT::encode('abc', 'my_key');
+ $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc');
+ }
- function testDecodeFromPython() {
- $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
- $this->assertEquals(
- JWT::decode($msg, 'my_key'),
- '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
- );
- }
+ public function testDecodeFromPython()
+ {
+ $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
+ $this->assertEquals(
+ JWT::decode($msg, 'my_key'),
+ '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
+ );
+ }
- function testUrlSafeCharacters() {
- $encoded = JWT::encode('f?', 'a');
- $this->assertEquals('f?', JWT::decode($encoded, 'a'));
- }
+ public function testUrlSafeCharacters()
+ {
+ $encoded = JWT::encode('f?', 'a');
+ $this->assertEquals('f?', JWT::decode($encoded, 'a'));
+ }
- function testMalformedUtf8StringsFail() {
- $this->setExpectedException('DomainException');
- JWT::encode(pack('c', 128), 'a');
- }
+ public function testMalformedUtf8StringsFail()
+ {
+ $this->setExpectedException('DomainException');
+ JWT::encode(pack('c', 128), 'a');
+ }
- function testMalformedJsonThrowsException() {
- $this->setExpectedException('DomainException');
- JWT::jsonDecode('this is not valid JSON string');
- }
+ public function testMalformedJsonThrowsException()
+ {
+ $this->setExpectedException('DomainException');
+ JWT::jsonDecode('this is not valid JSON string');
+ }
- function testExpiredToken() {
- $this->setExpectedException('UnexpectedValueException');
- $payload = array(
- "message" => "abc",
- "exp" => time() - 20); // time in the past
- $encoded = JWT::encode($payload, 'my_key');
- JWT::decode($encoded);
- }
+ public function testExpiredToken()
+ {
+ $this->setExpectedException('ExpiredException');
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() - 20); // time in the past
+ $encoded = JWT::encode($payload, 'my_key');
+ JWT::decode($encoded, 'my_key');
+ }
- function testValidToken() {
- $payload = array(
- "message" => "abc",
- "exp" => time() + 20); // time in the future
- $encoded = JWT::encode($payload, 'my_key');
- $decoded = JWT::decode($encoded, 'my_key');
- $this->assertEquals($decoded->message, 'abc');
- }
+ public function testBeforeValidToken()
+ {
+ $this->setExpectedException('BeforeValidException');
+ $payload = array(
+ "message" => "abc",
+ "nbf" => time() + 20); // time in the future
+ $encoded = JWT::encode($payload, 'my_key');
+ JWT::decode($encoded, 'my_key');
+ }
- function testInvalidToken() {
- $payload = array(
- "message" => "abc",
- "exp" => time() + 20); // time in the future
- $encoded = JWT::encode($payload, 'my_key');
- $this->setExpectedException('UnexpectedValueException');
- $decoded = JWT::decode($encoded, 'my_key2');
- }
+ public function testValidToken()
+ {
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() + 20); // time in the future
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key');
+ $this->assertEquals($decoded->message, 'abc');
+ }
- function testRSEncodeDecode() {
- $privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
- 'private_key_bits' => 1024,
- 'private_key_type' => OPENSSL_KEYTYPE_RSA));
- $msg = JWT::encode('abc', $privKey, 'RS256');
- $pubKey = openssl_pkey_get_details($privKey);
- $pubKey = $pubKey['key'];
- $decoded = JWT::decode($msg, $pubKey, true);
- $this->assertEquals($decoded, 'abc');
- }
+ public function testValidTokenWithNbf()
+ {
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() + 20, // time in the future
+ "nbf" => time() - 20);
+ $encoded = JWT::encode($payload, 'my_key');
+ $decoded = JWT::decode($encoded, 'my_key');
+ $this->assertEquals($decoded->message, 'abc');
+ }
- function testKIDChooser() {
- $keys = array('1' => 'my_key', '2' => 'my_key2');
- $msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
- $decoded = JWT::decode($msg, $keys, true);
- $this->assertEquals($decoded, 'abc');
- }
+ public function testInvalidToken()
+ {
+ $payload = array(
+ "message" => "abc",
+ "exp" => time() + 20); // time in the future
+ $encoded = JWT::encode($payload, 'my_key');
+ $this->setExpectedException('SignatureInvalidException');
+ $decoded = JWT::decode($encoded, 'my_key2');
+ }
-}
+ public function testRSEncodeDecode()
+ {
+ $privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
+ 'private_key_bits' => 1024,
+ 'private_key_type' => OPENSSL_KEYTYPE_RSA));
+ $msg = JWT::encode('abc', $privKey, 'RS256');
+ $pubKey = openssl_pkey_get_details($privKey);
+ $pubKey = $pubKey['key'];
+ $decoded = JWT::decode($msg, $pubKey, true);
+ $this->assertEquals($decoded, 'abc');
+ }
-?>
+ public function testKIDChooser()
+ {
+ $keys = array('1' => 'my_key', '2' => 'my_key2');
+ $msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
+ $decoded = JWT::decode($msg, $keys, true);
+ $this->assertEquals($decoded, 'abc');
+ }
+}
diff --git a/tests/autoload.php.dist b/tests/autoload.php.dist
index 4533624..2e4310a 100644
--- a/tests/autoload.php.dist
+++ b/tests/autoload.php.dist
@@ -14,4 +14,4 @@ php composer.phar install
Visit http://getcomposer.org/ for more information.
');
-} \ No newline at end of file
+}