1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\Tests\Encoding;
use Lcobucci\JWT\Encoding\CannotDecodeContent;
use Lcobucci\JWT\Encoding\CannotEncodeContent;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\SodiumBase64Polyfill;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;
use function assert;
use function base64_decode;
use function is_string;
#[PHPUnit\CoversClass(JoseEncoder::class)]
#[PHPUnit\CoversClass(CannotDecodeContent::class)]
#[PHPUnit\CoversClass(CannotEncodeContent::class)]
#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)]
final class JoseEncoderTest extends TestCase
{
#[PHPUnit\Test]
public function jsonEncodeMustReturnAJSONString(): void
{
$encoder = new JoseEncoder();
self::assertSame('{"test":"test"}', $encoder->jsonEncode(['test' => 'test']));
}
#[PHPUnit\Test]
public function jsonEncodeShouldNotEscapeUnicode(): void
{
$encoder = new JoseEncoder();
self::assertSame('"汉语"', $encoder->jsonEncode('汉语'));
}
#[PHPUnit\Test]
public function jsonEncodeShouldNotEscapeSlashes(): void
{
$encoder = new JoseEncoder();
self::assertSame('"https://google.com"', $encoder->jsonEncode('https://google.com'));
}
#[PHPUnit\Test]
public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccurred(): void
{
$encoder = new JoseEncoder();
$this->expectException(CannotEncodeContent::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Error while encoding to JSON');
$encoder->jsonEncode("\xB1\x31");
}
#[PHPUnit\Test]
public function jsonDecodeMustReturnTheDecodedData(): void
{
$decoder = new JoseEncoder();
self::assertSame(
['test' => ['test' => []]],
$decoder->jsonDecode('{"test":{"test":{}}}'),
);
}
#[PHPUnit\Test]
public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccurred(): void
{
$decoder = new JoseEncoder();
$this->expectException(CannotDecodeContent::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Error while decoding from JSON');
$decoder->jsonDecode('{"test":\'test\'}');
}
#[PHPUnit\Test]
public function base64UrlEncodeMustReturnAUrlSafeBase64(): void
{
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=', true);
assert(is_string($data));
$encoder = new JoseEncoder();
self::assertSame('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo', $encoder->base64UrlEncode($data));
}
#[PHPUnit\Test]
public function base64UrlEncodeMustEncodeBilboMessageProperly(): void
{
/** @link https://tools.ietf.org/html/rfc7520#section-4 */
$message = 'It’s a dangerous business, Frodo, going out your door. You step '
. "onto the road, and if you don't keep your feet, there’s no knowing "
. 'where you might be swept off to.';
$expected = 'SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH'
. 'lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk'
. 'b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm'
. 'UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4';
$encoder = new JoseEncoder();
self::assertSame($expected, $encoder->base64UrlEncode($message));
}
#[PHPUnit\Test]
public function base64UrlDecodeMustRaiseExceptionWhenInvalidBase64CharsAreUsed(): void
{
$decoder = new JoseEncoder();
$this->expectException(CannotDecodeContent::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Error while decoding from Base64Url, invalid base64 characters detected');
$decoder->base64UrlDecode('ááá');
}
#[PHPUnit\Test]
public function base64UrlDecodeMustReturnTheRightData(): void
{
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=', true);
$decoder = new JoseEncoder();
self::assertSame($data, $decoder->base64UrlDecode('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo'));
}
#[PHPUnit\Test]
public function base64UrlDecodeMustDecodeBilboMessageProperly(): void
{
/** @link https://tools.ietf.org/html/rfc7520#section-4 */
$message = 'SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH'
. 'lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk'
. 'b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm'
. 'UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4';
$expected = 'It’s a dangerous business, Frodo, going out your door. You step '
. "onto the road, and if you don't keep your feet, there’s no knowing "
. 'where you might be swept off to.';
$encoder = new JoseEncoder();
self::assertSame($expected, $encoder->base64UrlDecode($message));
}
}
|