Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 0184352

Browse files
Add unit tests.
1 parent 14e3fd7 commit 0184352

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed

‎tests/Base64HeaderExtractorTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/*
3+
* (c) Minh Vuong <vuongxuongminh@gmail.com>
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this source code in the file LICENSE.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Istio\JWTPayloadExtractor\Tests;
12+
13+
use Istio\JWTPayloadExtractor\Base64HeaderExtractor;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
class Base64HeaderExtractorTest extends TestCase
18+
{
19+
use RequestDataProviderTrait;
20+
21+
public function testInitWithBlankIssuer(): void
22+
{
23+
$this->expectException(\LogicException::class);
24+
$this->expectExceptionMessageMatches('~can not be blank!~');
25+
26+
new Base64HeaderExtractor('', 'Authorization');
27+
}
28+
29+
/**
30+
* @dataProvider invalidRequests
31+
*/
32+
public function testExtractFromInvalidRequests(Request $inHeader)
33+
{
34+
$extractor = new Base64HeaderExtractor('valid', 'Authorization');
35+
$payloadFromHeader = $extractor->extract($inHeader);
36+
37+
$this->assertNull($payloadFromHeader);
38+
}
39+
40+
/**
41+
* @dataProvider validRequests
42+
*/
43+
public function testExtractFromValidRequests(Request $inHeader)
44+
{
45+
$extractor = new Base64HeaderExtractor('valid', 'Authorization');
46+
$payloadFromHeader = $extractor->extract($inHeader);
47+
48+
$this->assertIsArray($payloadFromHeader);
49+
$this->assertSame('valid', $payloadFromHeader['iss']);
50+
}
51+
52+
protected function getValidToken(): string
53+
{
54+
return base64_encode(json_encode(['iss' => 'valid']));
55+
}
56+
57+
protected function getInvalidToken(): string
58+
{
59+
return base64_encode(json_encode(['iss' => 'invalid']));
60+
}
61+
}

‎tests/CompositeExtractorTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/*
3+
* (c) Minh Vuong <vuongxuongminh@gmail.com>
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this source code in the file LICENSE.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Istio\JWTPayloadExtractor\Tests;
12+
13+
use Istio\JWTPayloadExtractor\ExtractorFactory;
14+
use Istio\JWTPayloadExtractor\ExtractorInterface;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
class CompositeExtractorTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider validRequests
22+
*/
23+
public function testExtractFromValidRequestsWithEmptyExtractors(Request $request): void
24+
{
25+
$extractor = ExtractorFactory::fromExtractors();
26+
$payload = $extractor->extract($request);
27+
28+
$this->assertNull($payload);
29+
}
30+
31+
/**
32+
* @dataProvider validRequests
33+
*/
34+
public function testExtractFromValidRequests(Request $request): void
35+
{
36+
$extractor = $this->getExtractor();
37+
$payload = $extractor->extract($request);
38+
39+
$this->assertIsArray($payload);
40+
$this->assertSame('valid', $payload['iss']);
41+
}
42+
43+
/**
44+
* @dataProvider invalidRequests
45+
*/
46+
public function testExtractFromInvalidRequests(Request $request): void
47+
{
48+
$extractor = $this->getExtractor();
49+
$payload = $extractor->extract($request);
50+
51+
$this->assertNull($payload);
52+
}
53+
54+
public function validRequests(): array
55+
{
56+
return [
57+
[Request::create('', server: ['HTTP_X_JWT_PAYLOAD' => $this->getValidBase64Payload()])],
58+
[Request::create('', server: ['HTTP_AUTHORIZATION' => $this->getValidOriginToken()])],
59+
[Request::create('', parameters: ['token' => $this->getValidOriginToken()])],
60+
];
61+
}
62+
63+
public function invalidRequests(): array
64+
{
65+
return [
66+
[Request::create('')],
67+
[Request::create('', server: ['HTTP_X_JWT_PAYLOAD' => ''])],
68+
[Request::create('', server: ['HTTP_AUTHORIZATION' => ''])],
69+
[Request::create('', parameters: ['token' => ''])],
70+
[Request::create('', server: ['HTTP_X_JWT_PAYLOAD' => $this->getValidOriginToken()])],
71+
[Request::create('', server: ['HTTP_AUTHORIZATION' => $this->getValidBase64Payload()])],
72+
[Request::create('', server: ['HTTP_X_JWT_PAYLOAD' => $this->getInvalidBase64Payload()])],
73+
[Request::create('', server: ['HTTP_AUTHORIZATION' => $this->getInvalidOriginToken()])],
74+
[Request::create('', parameters: ['token' => $this->getInvalidOriginToken()])],
75+
];
76+
}
77+
78+
private function getExtractor(): ExtractorInterface
79+
{
80+
return ExtractorFactory::fromExtractors(
81+
ExtractorFactory::fromBase64Header('valid', 'X-JWT-Payload'),
82+
ExtractorFactory::fromOriginTokenHeader('valid', 'Authorization'),
83+
ExtractorFactory::fromOriginTokenQueryParam('valid', 'token'),
84+
);
85+
}
86+
87+
private function getValidOriginToken(): string
88+
{
89+
return sprintf('header.%s.signature', base64_encode(json_encode(['iss' => 'valid'])));
90+
}
91+
92+
private function getInvalidOriginToken(): string
93+
{
94+
return sprintf('header.%s.signature', base64_encode(json_encode(['iss' => 'invalid'])));
95+
}
96+
97+
private function getValidBase64Payload(): string
98+
{
99+
return base64_encode(json_encode(['iss' => 'valid']));
100+
}
101+
102+
private function getInvalidBase64Payload(): string
103+
{
104+
return base64_encode(json_encode(['iss' => 'invalid']));
105+
}
106+
}

‎tests/ExtractorFactoryTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/*
3+
* (c) Minh Vuong <vuongxuongminh@gmail.com>
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this source code in the file LICENSE.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Istio\JWTPayloadExtractor\Tests;
12+
13+
use Istio\JWTPayloadExtractor\Base64HeaderExtractor;
14+
use Istio\JWTPayloadExtractor\CompositeExtractor;
15+
use Istio\JWTPayloadExtractor\ExtractorFactory;
16+
use Istio\JWTPayloadExtractor\OriginTokenExtractor;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ExtractorFactoryTest extends TestCase
20+
{
21+
public function testMatchInstanceOf(): void
22+
{
23+
$this->assertInstanceOf(
24+
OriginTokenExtractor::class,
25+
ExtractorFactory::fromOriginTokenHeader('valid', 'Authorization')
26+
);
27+
28+
$this->assertInstanceOf(
29+
OriginTokenExtractor::class,
30+
ExtractorFactory::fromOriginTokenQueryParam('valid', 'token')
31+
);
32+
33+
$this->assertInstanceOf(
34+
Base64HeaderExtractor::class,
35+
ExtractorFactory::fromBase64Header('valid', 'Authorization')
36+
);
37+
38+
$this->assertInstanceOf(
39+
CompositeExtractor::class,
40+
ExtractorFactory::fromExtractors()
41+
);
42+
}
43+
}

‎tests/OriginTokenExtractorTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/*
3+
* (c) Minh Vuong <vuongxuongminh@gmail.com>
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this source code in the file LICENSE.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Istio\JWTPayloadExtractor\Tests;
12+
13+
use Istio\JWTPayloadExtractor\OriginTokenExtractor;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
class OriginTokenExtractorTest extends TestCase
18+
{
19+
use RequestDataProviderTrait;
20+
21+
public function testInitWithBlankIssuer(): void
22+
{
23+
$this->expectException(\LogicException::class);
24+
$this->expectExceptionMessageMatches('~can not be blank!~');
25+
26+
new OriginTokenExtractor('', 'headers', 'Authorization');
27+
}
28+
29+
public function testInitWithInvalidRequestBag(): void
30+
{
31+
$this->expectException(\LogicException::class);
32+
$this->expectExceptionMessageMatches('~`headers` or `query`~');
33+
34+
new OriginTokenExtractor('valid', 'invalid bag', 'Authorization');
35+
}
36+
37+
/**
38+
* @dataProvider invalidRequests
39+
*/
40+
public function testExtractFromInvalidRequests(Request $inHeader, Request $inQueryParam): void
41+
{
42+
[$payloadFromHeader, $payloadFromQueryParam] = $this->extractRequests($inHeader, $inQueryParam);
43+
44+
$this->assertNull($payloadFromHeader);
45+
$this->assertNull($payloadFromQueryParam);
46+
}
47+
48+
/**
49+
* @dataProvider validRequests
50+
*/
51+
public function testExtractFromValidRequests(Request $inHeader, Request $inQueryParam)
52+
{
53+
[$payloadFromHeader, $payloadFromQueryParam] = $this->extractRequests($inHeader, $inQueryParam);
54+
55+
$this->assertIsArray($payloadFromHeader);
56+
$this->assertSame('valid', $payloadFromHeader['iss']);
57+
$this->assertIsArray($payloadFromQueryParam);
58+
$this->assertSame('valid', $payloadFromQueryParam['iss']);
59+
}
60+
61+
private function extractRequests(Request $inHeader, Request $inQueryParam): array
62+
{
63+
$headerExtractor = new OriginTokenExtractor('valid', 'headers', 'Authorization');
64+
$queryParamExtractor = new OriginTokenExtractor('valid', 'query', 'token');
65+
66+
return [$headerExtractor->extract($inHeader), $queryParamExtractor->extract($inQueryParam)];
67+
}
68+
69+
protected function getValidToken(): string
70+
{
71+
return sprintf('header.%s.signature', base64_encode(json_encode(['iss' => 'valid'])));
72+
}
73+
74+
protected function getInvalidToken(): string
75+
{
76+
return sprintf('header.%s.signature', base64_encode(json_encode(['iss' => 'invalid'])));
77+
}
78+
}

‎tests/RequestDataProviderTrait.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* (c) Minh Vuong <vuongxuongminh@gmail.com>
4+
*
5+
* This source file is subject to the MIT license that is bundled
6+
* with this source code in the file LICENSE.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Istio\JWTPayloadExtractor\Tests;
12+
13+
use Symfony\Component\HttpFoundation\Request;
14+
15+
trait RequestDataProviderTrait
16+
{
17+
public function invalidRequests(): array
18+
{
19+
return [
20+
[
21+
Request::create(''),
22+
Request::create(''),
23+
],
24+
[
25+
Request::create('', server: ['HTTP_INVALID_NAME' => $this->getValidToken()]),
26+
Request::create('', parameters: ['invalid_name' => $this->getValidToken()]),
27+
],
28+
[
29+
Request::create('', server: ['HTTP_AUTHORIZATION' => '']),
30+
Request::create('', parameters: ['token' => '']),
31+
],
32+
[
33+
Request::create('', server: ['HTTP_AUTHORIZATION' => 'invalid header']),
34+
Request::create('', parameters: ['token' => 'invalid param']),
35+
],
36+
[
37+
Request::create('', server: ['HTTP_AUTHORIZATION' => $this->getInvalidToken()]),
38+
Request::create('', parameters: ['token' => $this->getInvalidToken()]),
39+
],
40+
];
41+
}
42+
43+
public function validRequests(): array
44+
{
45+
return [
46+
[
47+
Request::create('', server: ['HTTP_AUTHORIZATION' => $this->getValidToken()]),
48+
Request::create('', parameters: ['token' => $this->getValidToken()]),
49+
],
50+
];
51+
}
52+
53+
abstract protected function getValidToken(): string;
54+
55+
abstract protected function getInvalidToken(): string;
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /