-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAcceptHeaderTest.php
More file actions
291 lines (248 loc) · 8.79 KB
/
AcceptHeaderTest.php
File metadata and controls
291 lines (248 loc) · 8.79 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace LaravelJsonApi\Exceptions\Tests\Integration;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use LaravelJsonApi\Exceptions\ExceptionParser;
use Symfony\Component\HttpKernel\Exception\HttpException;
class AcceptHeaderTest extends TestCase
{
/**
* @var array
*/
private array $jsonApi;
/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
Route::middleware('api')->get('/test', function () {
throw new HttpException(
418,
'I think I might be a teapot.',
);
});
$this->jsonApi = [
'errors' => [
[
'title' => "I'm a teapot",
'detail' => 'I think I might be a teapot.',
'status' => '418',
]
],
'jsonapi' => [
'version' => '1.0',
],
];
}
/**
* @return void
*/
protected function tearDown(): void
{
parent::tearDown();
Handler::$testRenderer = null;
}
/**
* @inheritDoc
*/
protected function resolveApplicationExceptionHandler($app)
{
$app->singleton(ExceptionHandler::class, Handler::class);
}
public function testJsonApi(): void
{
$this->get('/test', ['Accept' => 'application/vnd.api+json'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
/**
* We expect the standard Laravel error JSON response here.
*/
public function testJson(): void
{
$expected = [
'message' => 'I think I might be a teapot.',
];
$this->get('/test', ['Accept' => 'application/json'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/json')
->assertExactJson($expected);
}
/**
* Allow a developer to set the exception parser to render JSON:API even if it is
* a JSON request.
*
* @see https://github.com/cloudcreativity/laravel-json-api/issues/582
*/
public function testAcceptsJson(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsJson()
->renderable();
$this->get('/test', ['Accept' => 'application/json'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
/**
* Test the `acceptsAll()` helper method, which ensures exceptions
* are always rendered as JSON:API.
*/
public function testAcceptsAll(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsAll()
->renderable();
$this->get('/test', ['Accept' => '*/*'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
/**
* Test the `acceptsMiddleware()` helper method. This should render JSON:API
* errors if any of the provided middleware matches.
*/
public function testAcceptsMiddlewareMatches(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsMiddleware('foo', 'api')
->renderable();
$this->get('/test', ['Accept' => '*/*'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
public function testAcceptsMiddlewareDoesNotMatch(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsMiddleware('foo', 'bar')
->renderable();
$response = $this->get('/test', ['Accept' => '*/*']);
$response
->assertStatus(418)
->assertSee('teapot');
$this->assertSame('text/html; charset=utf-8', strtolower($response->headers->get('Content-Type')));
}
/**
* @see https://github.com/laravel-json-api/laravel/issues/204
*/
public function testAcceptsMiddlewareWhenRouteNotFound(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsMiddleware('foo', 'api')
->renderable();
$response = $this->get('/blah', ['Accept' => '*/*']);
$response->assertStatus(404)->assertSee('Not Found');
$this->assertSame('text/html; charset=utf-8', strtolower($response->headers->get('Content-Type')));
}
public function testAcceptsMiddlewareWhenRouteNotFoundWithJsonApiMediaType(): void
{
$expected = [
'errors' => [
[
// @TODO this detail has been added at some point in Laravel 9.
// 'detail' => 'The route blah could not be found.',
'title' => 'Not Found',
'status' => '404',
]
],
'jsonapi' => [
'version' => '1.0',
],
];
Handler::$testRenderer = ExceptionParser::make()
->acceptsMiddleware('foo', 'api')
->renderable();
$this->get('/blah', ['Accept' => 'application/vnd.api+json'])
->assertStatus(404)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertJson($expected); // @TODO put back to `assertExactJson` when min version is Laravel 9
}
/**
* Allow a developer to use their own callback for whether JSON:API should be rendered.
*
* @see https://github.com/cloudcreativity/laravel-json-api/issues/582
*/
public function testAcceptTrue(): void
{
Handler::$testRenderer = ExceptionParser::make()
->accept(fn(\Throwable $ex, Request $request) => true)
->renderable();
$this->get('/test', ['Accept' => '*/*'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
public function testAcceptFalse(): void
{
Handler::$testRenderer = ExceptionParser::make()
->accept(fn(\Throwable $ex, Request $request) => false)
->renderable();
$response = $this->get('/test', ['Accept' => '*/*']);
$response->assertStatus(418)
->assertSee('teapot');
$this->assertSame('text/html; charset=utf-8', strtolower($response->headers->get('Content-Type')));
}
public function testAcceptFalseWithJsonApiAcceptHeader(): void
{
Handler::$testRenderer = ExceptionParser::make()
->accept(fn(\Throwable $ex, $request) => false)
->renderable();
$this->get('/test', ['Accept' => 'application/vnd.api+json'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
/**
* We should be able to accept multiple accept callbacks. For example,
* if we wanted to render JSON:API errors if the client accepts JSON
* or if a particular middleware matches.
*/
public function testMultipleAcceptCallbacks1(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsJson()
->acceptsMiddleware('foo', 'bar')
->renderable();
$this->get('/test', ['Accept' => 'application/json'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
/**
* We should be able to accept multiple accept callbacks. For example,
* if we wanted to render JSON:API errors if the client accepts JSON
* or if a particular middleware matches.
*/
public function testMultipleAcceptCallbacks2(): void
{
Handler::$testRenderer = ExceptionParser::make()
->acceptsMiddleware('api')
->acceptsJson()
->renderable();
$this->get('/test', ['Accept' => '*/*'])
->assertStatus(418)
->assertHeader('Content-Type', 'application/vnd.api+json')
->assertExactJson($this->jsonApi);
}
public function testHtml(): void
{
$response = $this->get('/test', ['Accept' => '*/*']);
$response
->assertStatus(418)
->assertSee('teapot');
$this->assertSame('text/html; charset=utf-8', strtolower($response->headers->get('Content-Type')));
}
}