|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Inertia\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Http; |
| 6 | +use Inertia\Ssr\HttpGateway; |
| 7 | + |
| 8 | +class HttpGatewayTest extends TestCase |
| 9 | +{ |
| 10 | + protected HttpGateway $gateway; |
| 11 | + |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + $this->gateway = new HttpGateway; |
| 17 | + |
| 18 | + Http::preventStrayRequests(); |
| 19 | + } |
| 20 | + |
| 21 | + public function test_it_returns_null_when_ssr_is_disabled() |
| 22 | + { |
| 23 | + config([ |
| 24 | + 'inertia.ssr.enabled' => false, |
| 25 | + 'inertia.ssr.bundle' => __DIR__.'/Stubs/ssr-bundle.js', |
| 26 | + ]); |
| 27 | + |
| 28 | + $this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_it_returns_null_when_no_bundle_file_is_detected() |
| 32 | + { |
| 33 | + config([ |
| 34 | + 'inertia.ssr.enabled' => true, |
| 35 | + 'inertia.ssr.bundle' => null, |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_it_uses_the_configured_http_url_when_the_bundle_file_is_detected() |
| 42 | + { |
| 43 | + config([ |
| 44 | + 'inertia.ssr.enabled' => true, |
| 45 | + 'inertia.ssr.bundle' => __DIR__.'/Stubs/ssr-bundle.js', |
| 46 | + ]); |
| 47 | + |
| 48 | + Http::fake([ |
| 49 | + $this->gateway->getHttpUrl() => Http::response(json_encode([ |
| 50 | + 'head' => ['<title>SSR Test</title>', '<style></style>'], |
| 51 | + 'body' => '<div id="app">SSR Response</div>', |
| 52 | + ])), |
| 53 | + ]); |
| 54 | + |
| 55 | + $this->assertNotNull( |
| 56 | + $response = $this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]) |
| 57 | + ); |
| 58 | + |
| 59 | + $this->assertEquals("<title>SSR Test</title>\n<style></style>", $response->head); |
| 60 | + $this->assertEquals('<div id="app">SSR Response</div>', $response->body); |
| 61 | + } |
| 62 | + |
| 63 | + public function test_it_uses_the_configured_http_url__when_bundle_file_detection_is_disabled() |
| 64 | + { |
| 65 | + config([ |
| 66 | + 'inertia.ssr.enabled' => true, |
| 67 | + 'inertia.ssr.dispatch_without_bundle' => true, |
| 68 | + 'inertia.ssr.bundle' => null, |
| 69 | + ]); |
| 70 | + |
| 71 | + Http::fake([ |
| 72 | + $this->gateway->getHttpUrl() => Http::response(json_encode([ |
| 73 | + 'head' => ['<title>SSR Test</title>', '<style></style>'], |
| 74 | + 'body' => '<div id="app">SSR Response</div>', |
| 75 | + ])), |
| 76 | + ]); |
| 77 | + |
| 78 | + $this->assertNotNull( |
| 79 | + $response = $this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]) |
| 80 | + ); |
| 81 | + |
| 82 | + $this->assertEquals("<title>SSR Test</title>\n<style></style>", $response->head); |
| 83 | + $this->assertEquals('<div id="app">SSR Response</div>', $response->body); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_it_returns_null_when_the_http_request_fails() |
| 87 | + { |
| 88 | + config([ |
| 89 | + 'inertia.ssr.enabled' => true, |
| 90 | + 'inertia.ssr.bundle' => __DIR__.'/Stubs/ssr-bundle.js', |
| 91 | + ]); |
| 92 | + |
| 93 | + Http::fake([ |
| 94 | + $this->gateway->getHttpUrl() => Http::response(null, 500), |
| 95 | + ]); |
| 96 | + |
| 97 | + $this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 98 | + } |
| 99 | + |
| 100 | + public function test_it_returns_null_when_invalid_json_is_returned() |
| 101 | + { |
| 102 | + config([ |
| 103 | + 'inertia.ssr.enabled' => true, |
| 104 | + 'inertia.ssr.bundle' => __DIR__.'/Stubs/ssr-bundle.js', |
| 105 | + ]); |
| 106 | + |
| 107 | + Http::fake([ |
| 108 | + $this->gateway->getHttpUrl() => Http::response('invalid json'), |
| 109 | + ]); |
| 110 | + |
| 111 | + $this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 112 | + } |
| 113 | +} |
0 commit comments