Skip to content

Commit

Permalink
Merge pull request #13 from huizhang-Easyswoole/master
Browse files Browse the repository at this point in the history
Feat: 完善单元测试
  • Loading branch information
kiss291323003 authored Nov 21, 2020
2 parents 2060292 + eba9e93 commit 4b71a75
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions tests/JwtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace EasySwoole\Jwt\Tests;

use EasySwoole\Jwt\Encryption;
use EasySwoole\Jwt\Jwt;
use EasySwoole\Jwt\JwtObject;
use EasySwoole\Jwt\Signature;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -22,11 +24,12 @@ class JwtTest extends TestCase
private $nbf;
private $sub;
private $extData;
private $other;

protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->alg = Jwt::ALG_METHOD_HMACSHA256;
$this->alg = Jwt::ALG_METHOD_HS256;
$this->aud = 'user';
$this->exp = time();
$this->iat = time() + 3600;
Expand All @@ -35,6 +38,7 @@ protected function setUp()
$this->nbf = time() + 60 * 5;
$this->sub = 'auth';
$this->extData = 'extData';
$this->other = 'other';
}

public function testJwt()
Expand Down Expand Up @@ -93,22 +97,68 @@ public function testDecode()
$this->assertTrue($jwtObject->getData() == $this->extData);

$jwtObject = Jwt::getInstance()->publish();
$jwtObject->setExp(time()-3600);
$jwtObject->setExp(time() - 3600);
$status = Jwt::getInstance()->decode($jwtObject->__toString())->getStatus();
$this->assertTrue($status === JwtObject::STATUS_EXPIRED);

$jwtObject = Jwt::getInstance()->publish();
$jwt = $jwtObject->__toString();

// 把签名解释出来,然修改,然后再放回去
$raw = json_decode(base64_decode(urldecode($jwt)), true);
$raw['signature'] = substr_replace($raw['signature'], mt_rand(1000, 9999), -4, 4);
$jwt = urlencode(base64_encode(json_encode($raw)));
$jwt = substr_replace($jwt, mt_rand(1000, 9999), -4, 4);

$status = Jwt::getInstance()->decode($jwt)->getStatus();
$this->assertTrue($status === JwtObject::STATUS_SIGNATURE_ERROR);
}

public function testOtherInfo()
{
// 通过第三方生成的token, 用于验证payload自定义参数
//header: {
// "alg": "HS256",
// "typ": "JWT"
//}
//payload: {
// "exp": "1906893573",
// "other": "other"
//}
//signature: HMACSHA256(
// base64UrlEncode(header) + "." +
// base64UrlEncode(payload),
// easyswoole
// )
$token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxOTA2ODkzNTczIiwib3RoZXIiOiJvdGhlciJ9.eDiVODe_LoARtYU8968tJYtQz3nZkae8y6QZv4QtLT4';
$jwtObject = Jwt::getInstance()->setSecretKey('easyswoole')->decode($token);
$this->assertTrue($jwtObject->getStatus() === JwtObject::STATUS_OK);
$this->assertTrue($jwtObject->other === $this->other);
}

public function testSignature()
{
// https://jwt.io/ 通过第三方生成的签名与es生成的签名做对比
//header: {
// "alg": "HS256",
// "typ": "JWT"
//}
//payload: {
// "exp": "1906893573",
//}
//signature: HMACSHA256(
// base64UrlEncode(header) + "." +
// base64UrlEncode(payload),
// easyswoole
// )
$token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY4OTM1NzN9.ngWBkKeKiOMjRQpi5rvY5xRf0yZzvx_NSfi5msZCRmA';
$token = explode('.', $token);
$signature = (new Signature([
'secretKey' => 'easyswoole',
'header' => $token[0],
'payload' => $token[1],
'alg' => $this->alg
]))->__toString();
$this->assertTrue($signature === $token[2]);
}

/**
* @expectedException \EasySwoole\Jwt\Exception
*/
Expand Down

0 comments on commit 4b71a75

Please sign in to comment.