Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 91 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| JavascriptCdnProxyTest | |
0.00% |
0 / 91 |
|
0.00% |
0 / 7 |
72 | |
0.00% |
0 / 1 |
| test_it_reads_a_fake_package_file_before_requesting_the_cdn | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| test_it_proxies_valid_scoped_package_paths_to_jsdelivr | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| test_it_rejects_traversal_paths_without_requesting_the_cdn | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| test_it_caches_responses_for_versioned_packages | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| test_it_caches_latest_packages_for_a_short_time | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| test_it_caches_a_rewritten_redirect_for_latest_packages | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| test_it_rejects_redirects_to_other_hosts | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Tests\JavascriptCdn; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 5 | use Apie\JavascriptCdn\JavascriptCdnProxy; |
| 6 | use Apie\JavascriptCdn\ValueObjects\JavascriptCdnPath; |
| 7 | use PHPUnit\Framework\TestCase; |
| 8 | use Symfony\Component\Cache\Adapter\ArrayAdapter; |
| 9 | use Symfony\Component\HttpClient\MockHttpClient; |
| 10 | use Symfony\Component\HttpClient\Response\MockResponse; |
| 11 | |
| 12 | class JavascriptCdnProxyTest extends TestCase |
| 13 | { |
| 14 | public function test_it_reads_a_fake_package_file_before_requesting_the_cdn(): void |
| 15 | { |
| 16 | $directory = sys_get_temp_dir() . '/apie-javascript-cdn-' . uniqid('', true); |
| 17 | $filePath = $directory . '/apie-stacktrace/0.1.6/dist/apie-stacktrace/apie-stacktrace.esm.js'; |
| 18 | mkdir(dirname($filePath), 0777, true); |
| 19 | file_put_contents($filePath, 'export const local = true;'); |
| 20 | $client = new MockHttpClient(static function (): never { |
| 21 | self::fail('A local fake package should not trigger a CDN request.'); |
| 22 | }); |
| 23 | |
| 24 | try { |
| 25 | $proxy = new JavascriptCdnProxy(new ArrayAdapter(), $client, $directory); |
| 26 | $response = $proxy->getResponse(new JavascriptCdnPath('apie-stacktrace@0.1.6/dist/apie-stacktrace/apie-stacktrace.esm.js')); |
| 27 | |
| 28 | self::assertContains($response->getHeaderLine('Content-Type'), ['text/javascript', 'application/javascript']); |
| 29 | self::assertSame('export const local = true;', (string) $response->getBody()); |
| 30 | } finally { |
| 31 | unlink($filePath); |
| 32 | $path = dirname($filePath); |
| 33 | while ($path !== $directory) { |
| 34 | rmdir($path); |
| 35 | $path = dirname($path); |
| 36 | } |
| 37 | rmdir($directory); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function test_it_proxies_valid_scoped_package_paths_to_jsdelivr(): void |
| 42 | { |
| 43 | $requestedUrl = null; |
| 44 | $client = new MockHttpClient(static function (string $method, string $url) use (&$requestedUrl): MockResponse { |
| 45 | $requestedUrl = $url; |
| 46 | |
| 47 | return new MockResponse('remote asset', [ |
| 48 | 'http_code' => 200, |
| 49 | 'response_headers' => ['content-type: text/javascript'], |
| 50 | ]); |
| 51 | }); |
| 52 | $proxy = new JavascriptCdnProxy(new ArrayAdapter(), $client); |
| 53 | |
| 54 | $response = $proxy->getResponse(new JavascriptCdnPath('@ionic/core@7.0.0/dist/ionic/ionic.esm.js')); |
| 55 | |
| 56 | self::assertSame( |
| 57 | 'https://cdn.jsdelivr.net/npm/@ionic/core@7.0.0/dist/ionic/ionic.esm.js', |
| 58 | $requestedUrl |
| 59 | ); |
| 60 | self::assertSame('remote asset', (string) $response->getBody()); |
| 61 | self::assertSame('public, max-age=63072000, immutable', $response->getHeaderLine('Cache-Control')); |
| 62 | self::assertSame('text/javascript', $response->getHeaderLine('Content-Type')); |
| 63 | } |
| 64 | |
| 65 | public function test_it_rejects_traversal_paths_without_requesting_the_cdn(): void |
| 66 | { |
| 67 | $client = new MockHttpClient(static function (): never { |
| 68 | self::fail('Invalid package paths must not be sent to the CDN.'); |
| 69 | }); |
| 70 | $this->expectException(InvalidStringForValueObjectException::class); |
| 71 | |
| 72 | new JavascriptCdnPath('package@1.0.0/../../secret.js'); |
| 73 | } |
| 74 | |
| 75 | public function test_it_caches_responses_for_versioned_packages(): void |
| 76 | { |
| 77 | $requestCount = 0; |
| 78 | $client = new MockHttpClient(static function () use (&$requestCount): MockResponse { |
| 79 | $requestCount++; |
| 80 | |
| 81 | return new MockResponse('cached asset', [ |
| 82 | 'http_code' => 200, |
| 83 | 'response_headers' => ['content-type: text/javascript'], |
| 84 | ]); |
| 85 | }); |
| 86 | $proxy = new JavascriptCdnProxy(new ArrayAdapter(), $client); |
| 87 | |
| 88 | $firstResponse = $proxy->getResponse(new JavascriptCdnPath('package@1.2.3/dist/index.js')); |
| 89 | $secondResponse = $proxy->getResponse(new JavascriptCdnPath('package@1.2.3/dist/index.js')); |
| 90 | |
| 91 | self::assertSame(1, $requestCount); |
| 92 | self::assertSame('cached asset', (string) $firstResponse->getBody()); |
| 93 | self::assertSame('cached asset', (string) $secondResponse->getBody()); |
| 94 | self::assertSame('text/javascript', $secondResponse->getHeaderLine('Content-Type')); |
| 95 | } |
| 96 | |
| 97 | public function test_it_caches_latest_packages_for_a_short_time(): void |
| 98 | { |
| 99 | $requestCount = 0; |
| 100 | $client = new MockHttpClient(static function () use (&$requestCount): MockResponse { |
| 101 | $requestCount++; |
| 102 | |
| 103 | return new MockResponse('latest asset ' . $requestCount, [ |
| 104 | 'http_code' => 200, |
| 105 | 'response_headers' => ['content-type: text/javascript'], |
| 106 | ]); |
| 107 | }); |
| 108 | $cache = new ArrayAdapter(); |
| 109 | $proxy = new JavascriptCdnProxy($cache, $client); |
| 110 | |
| 111 | $firstResponse = $proxy->getResponse(new JavascriptCdnPath('package@latest/dist/index.js')); |
| 112 | $secondResponse = $proxy->getResponse(new JavascriptCdnPath('package@latest/dist/index.js')); |
| 113 | |
| 114 | self::assertSame(1, $requestCount); |
| 115 | $expiry = $cache->getItem('apie_javascript_cdn.' . hash('sha256', 'package@latest/dist/index.js'))->getMetadata()['expiry']; |
| 116 | self::assertEqualsWithDelta(time() + 600, $expiry, 5); |
| 117 | self::assertSame('latest asset 1', (string) $firstResponse->getBody()); |
| 118 | self::assertSame('latest asset 1', (string) $secondResponse->getBody()); |
| 119 | } |
| 120 | |
| 121 | public function test_it_caches_a_rewritten_redirect_for_latest_packages(): void |
| 122 | { |
| 123 | $requestCount = 0; |
| 124 | $client = new MockHttpClient(static function () use (&$requestCount): MockResponse { |
| 125 | $requestCount++; |
| 126 | |
| 127 | return new MockResponse('', [ |
| 128 | 'http_code' => 302, |
| 129 | 'response_headers' => ['location: https://cdn.jsdelivr.net/npm/package@1.2.3/dist/index.js'], |
| 130 | ]); |
| 131 | }); |
| 132 | $proxy = new JavascriptCdnProxy(new ArrayAdapter(), $client); |
| 133 | |
| 134 | $proxy->getResponse(new JavascriptCdnPath('package@latest/dist/index.js')); |
| 135 | $response = $proxy->getResponse(new JavascriptCdnPath('package@latest/dist/index.js')); |
| 136 | |
| 137 | self::assertSame(1, $requestCount); |
| 138 | self::assertSame(302, $response->getStatusCode()); |
| 139 | self::assertSame('/npm/package@1.2.3/dist/index.js', $response->getHeaderLine('Location')); |
| 140 | } |
| 141 | |
| 142 | public function test_it_rejects_redirects_to_other_hosts(): void |
| 143 | { |
| 144 | $client = new MockHttpClient(new MockResponse('', [ |
| 145 | 'http_code' => 302, |
| 146 | 'response_headers' => ['location: https://evil.example/x.js'], |
| 147 | ])); |
| 148 | $proxy = new JavascriptCdnProxy(new ArrayAdapter(), $client); |
| 149 | |
| 150 | self::assertSame(502, $proxy->getResponse(new JavascriptCdnPath('package@latest/a.js'))->getStatusCode()); |
| 151 | } |
| 152 | } |