8000 [Bugfix] Ensure response has matched media type for content type · startupengine/laravel-json-api@df1ae94 · GitHub
[go: up one dir, main page]

Skip to content

Commit df1ae94

Browse files
committed
[Bugfix] Ensure response has matched media type for content type
Apis were not passing their codec matcher to the responses factory which meant that the matched media type was not being set on responses.
1 parent 84a96e5 commit df1ae94

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Api/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function response(
318318
return $this->factory->createResponses(
319319
$this->getSchemas(),
320320
$this->getErrors(),
321-
null,
321+
$this->getCodecMatcher(),
322322
$parameters,
323323
$extensions ?: $this->getSupportedExtensions(),
324324
(string) $this->getUrl()

tests/Integration/ContentNegotiationTest.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ protected function setUp()
2222

2323
public function testOkWithoutBody()
2424
{
25-
$this->getJsonApi('/api/v1/posts')->assertStatusCode(200);
25+
$this->getJsonApi('/api/v1/posts')
26+
->assertStatus(200)
27+
->assertHeader('Content-Type', 'application/vnd.api+json');
2628
}
2729

2830
public function testOkWithBody()
@@ -71,9 +73,54 @@ public function testUnsupportedMediaType()
7173
])->assertStatus(415);
7274
}
7375

76+
/**
77+
* Can request an alternative media-type that is in our configuration.
78+
* Note that the Symfony response automatically appends the charset to the
79+
* content-type header if it starts with `text/`.
80+
*/
81+
public function testAcceptable()
82+
{
83+
$this->get('/api/v1/posts', ['Accept' => 'text/plain'])
84+
->assertStatus(200)
85+
->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
86+
}
87+
88+
/**
89+
* If we request a content type that is not in our codec configuration, we
90+
* expect a 406 response.
91+
*/
7492
public function testNotAcceptable()
7593
{
76-
$this->get('/api/v1/posts', ['Accept' => 'text/html'])->assertStatus(406);
94+
$this->get('/api/v1/posts', ['Accept' => 'application/json'])->assertStatus(406);
95+
}
96+
97+
/**
98+
* The codec configuration can be changed.
99+
*/
100+
public function testCanChangeMediaType1()
101+
{
102+
app('config')->set('json-api-default.codecs', [
103+
'encoders' => ['application/json'],
104+
'decoders' => ['application/json'],
105+
]);
106+
107+
$this->get('/api/v1/posts', ['Accept' => 'application/json'])
108+
->assertStatus(200)
109+
->assertHeader('Content-Type', 'application/json');
110+
}
111+
112+
/**
113+
* Not including the JSON API media type in our configuration results in a 406 response
114+
*/
115+
public function testCanChangeMediaType2()
116+
{
117+
app('config')->set('json-api-default.codecs', [
118+
'encoders' => ['application/json'],
119+
'decoders' => ['application/json'],
120+
]);
121+
122+
$this->get('/api/v1/posts', ['Accept' => 'application/vnd.api+json'])
123+
->assertStatus(406);
77124
}
78125

79126
/**

0 commit comments

Comments
 (0)
0