|
2 | 2 |
|
3 | 3 | namespace Tobyz\Tests\JsonApiServer\specification; |
4 | 4 |
|
| 5 | +use Psr\Http\Message\ResponseInterface; |
5 | 6 | use Tobyz\JsonApiServer\Endpoint\Show; |
| 7 | +use Tobyz\JsonApiServer\Extension\Extension; |
6 | 8 | use Tobyz\JsonApiServer\Exception\NotAcceptableException; |
| 9 | +use Tobyz\JsonApiServer\Exception\ResourceNotFoundException; |
7 | 10 | use Tobyz\JsonApiServer\Exception\UnsupportedMediaTypeException; |
8 | 11 | use Tobyz\JsonApiServer\JsonApi; |
9 | 12 | use Tobyz\Tests\JsonApiServer\AbstractTestCase; |
@@ -196,4 +199,62 @@ public function test_profiles_from_lines_with_unsupported_extensions_are_ignored |
196 | 199 | // Should only get the profile from the second (valid) Accept line |
197 | 200 | $this->assertEquals(['https://example.com/valid-profile'], $capturedProfiles); |
198 | 201 | } |
| 202 | + |
| 203 | + public function test_extensions_can_activate_on_get_requests_without_content_type() |
| 204 | + { |
| 205 | + $this->api = new JsonApi(); |
| 206 | + $this->api->extension($this->extensionDemo()); |
| 207 | + |
| 208 | + $response = $this->api->handle( |
| 209 | + $this->buildRequest('GET', '/extension-demo')->withHeader( |
| 210 | + 'Accept', |
| 211 | + 'application/vnd.api+json; ext="https://example.com/extensions/demo"', |
| 212 | + ), |
| 213 | + ); |
| 214 | + |
| 215 | + $this->assertEquals(200, $response->getStatusCode()); |
| 216 | + $this->assertEquals( |
| 217 | + 'application/vnd.api+json; ext=https://example.com/extensions/demo', |
| 218 | + $response->getHeaderLine('Content-Type'), |
| 219 | + ); |
| 220 | + $this->assertJsonApiDocumentSubset(['meta' => ['activated' => true]], (string) $response->getBody()); |
| 221 | + } |
| 222 | + |
| 223 | + public function test_extensions_with_content_type_must_be_requested_in_both_headers() |
| 224 | + { |
| 225 | + $this->api = new JsonApi(); |
| 226 | + $this->api->extension($this->extensionDemo()); |
| 227 | + |
| 228 | + $this->expectException(ResourceNotFoundException::class); |
| 229 | + |
| 230 | + $this->api->handle( |
| 231 | + $this->buildRequest('POST', '/extension-demo') |
| 232 | + ->withHeader( |
| 233 | + 'Accept', |
| 234 | + 'application/vnd.api+json; ext="https://example.com/extensions/demo"', |
| 235 | + ) |
| 236 | + ->withHeader('Content-Type', 'application/vnd.api+json') |
| 237 | + ->withParsedBody(['data' => ['type' => 'users']]), |
| 238 | + ); |
| 239 | + } |
| 240 | + |
| 241 | + private function extensionDemo(): Extension |
| 242 | + { |
| 243 | + return new class extends Extension |
| 244 | + { |
| 245 | + public function uri(): string |
| 246 | + { |
| 247 | + return 'https://example.com/extensions/demo'; |
| 248 | + } |
| 249 | + |
| 250 | + public function handle(\Tobyz\JsonApiServer\Context $context): ?ResponseInterface |
| 251 | + { |
| 252 | + if ($context->path() !== 'extension-demo') { |
| 253 | + return null; |
| 254 | + } |
| 255 | + |
| 256 | + return $context->createResponse(['meta' => ['activated' => true]]); |
| 257 | + } |
| 258 | + }; |
| 259 | + } |
199 | 260 | } |
0 commit comments