Skip to content

Commit 000370c

Browse files
author
Lander Vanderstraeten
committed
Unsupported media type
1 parent fff4f25 commit 000370c

3 files changed

Lines changed: 113 additions & 81 deletions

File tree

README.md

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
[![Installs](https://img.shields.io/packagist/dt/phpro/api-problem.svg)](https://packagist.org/packages/phpro/api-problem/stats)
33
[![Packagist](https://img.shields.io/packagist/v/phpro/api-problem.svg)](https://packagist.org/packages/phpro/api-problem)
44

5-
65
# Api Problem
76

87
This package provides a [RFC7807](https://tools.ietf.org/html/rfc7807) Problem details implementation.
@@ -13,7 +12,6 @@ Since handling the exceptions is up to the framework, here is a list of known fr
1312

1413
- **Symfony** `^4.1`: [ApiProblemBundle](https://www.github.com/phpro/api-problem-bundle)
1514

16-
1715
## Installation
1816

1917
```sh
@@ -34,22 +32,26 @@ throw new ApiProblemException(
3432

3533
### Built-in problems
3634

37-
- [ExceptionApiProblem](#exceptionapiproblem)
38-
- [ForbiddenProblem](#forbiddenproblem)
39-
- [HttpApiProblem](#httpapiproblem)
40-
- [NotFoundProblem](#notfoundproblem)
41-
- [UnauthorizedProblem](#unauthorizedproblem)
42-
- [ValidationApiProblem](#validationapiproblem)
43-
- [BadRequestProblem](#badrequestproblem)
44-
- [ConflictProblem](#conflictproblem)
45-
- [NotModifiedProblem](#notmodifiedproblem)
46-
- [MethodNotAllowedProblem](#methodnotallowedproblem)
47-
- [PreconditionFailedProblem](#preconditionfailedproblem)
48-
- [UnsupportedMediaTypeProblem](#unsupportedmediatypeproblem)
49-
- [IAmATeapotProblem](#iamateapotproblem)
50-
- [UnprocessableEntityProblem](#unprocessableentityproblem)
51-
- [LockedProblem](#lockedproblem)
52-
- [PreconditionRequiredProblem](#preconditionrequiredproblem)
35+
- General problems
36+
- [ExceptionApiProblem](#exceptionapiproblem)
37+
- [HttpApiProblem](#httpapiproblem)
38+
39+
- Symfony integration problems
40+
- [ValidationApiProblem](#validationapiproblem)
41+
42+
- Http problems
43+
- 400 [BadRequestProblem](#badrequestproblem)
44+
- 401 [UnauthorizedProblem](#unauthorizedproblem)
45+
- 403 [ForbiddenProblem](#forbiddenproblem)
46+
- 404 [NotFoundProblem](#notfoundproblem)
47+
- 405 [MethodNotAllowedProblem](#methodnotallowedproblem)
48+
- 409 [ConflictProblem](#conflictproblem)
49+
- 412 [PreconditionFailedProblem](#preconditionfailedproblem)
50+
- 415 [UnsupportedMediaTypeProblem](#unsupportedmediatypeproblem)
51+
- 418 [IAmATeapotProblem](#iamateapotproblem)
52+
- 422 [UnprocessableEntityProblem](#unprocessableentityproblem)
53+
- 423 [LockedProblem](#lockedproblem)
54+
- 428 [PreconditionRequiredProblem](#preconditionrequiredproblem)
5355

5456
#### ExceptionApiProblem
5557

@@ -98,23 +100,6 @@ new ExceptionApiProblem(new \Exception('message', 500));
98100
}
99101
````
100102

101-
#### ForbiddenProblem
102-
103-
```php
104-
use Phpro\ApiProblem\Http\ForbiddenProblem;
105-
106-
new ForbiddenProblem('Not authorized to access gold.');
107-
```
108-
109-
```json
110-
{
111-
"status": 403,
112-
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
113-
"title": "Forbidden",
114-
"detail": "Not authorized to access gold."
115-
}
116-
````
117-
118103
#### HttpApiProblem
119104

120105
```php
@@ -132,40 +117,6 @@ new HttpApiProblem(404, ['detail' => 'The book could not be found.']);
132117
}
133118
````
134119

135-
#### NotFoundProblem
136-
137-
```php
138-
use Phpro\ApiProblem\Http\NotFoundProblem;
139-
140-
new NotFoundProblem('The book with ID 20 could not be found.');
141-
```
142-
143-
```json
144-
{
145-
"status": 404,
146-
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
147-
"title": "Not found",
148-
"detail": "The book with ID 20 could not be found."
149-
}
150-
````
151-
152-
#### UnauthorizedProblem
153-
154-
```php
155-
use Phpro\ApiProblem\Http\UnauthorizedProblem;
156-
157-
new UnauthorizedProblem('You are not authorized to access X.');
158-
```
159-
160-
```json
161-
{
162-
"status": 401,
163-
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
164-
"title": "Unauthorized",
165-
"detail": "You are not authenticated. Please login."
166-
}
167-
````
168-
169120
#### ValidationApiProblem
170121

171122
```sh
@@ -215,36 +166,54 @@ new BadRequestProblem('Bad request. Bad!.');
215166
}
216167
````
217168

218-
#### ConflictProblem
169+
#### UnauthorizedProblem
219170

220171
```php
221-
use Phpro\ApiProblem\Http\ConflictProblem;
222-
new ConflictProblem('Duplicated key for book with ID 20.');
172+
use Phpro\ApiProblem\Http\UnauthorizedProblem;
173+
174+
new UnauthorizedProblem('You are not authorized to access X.');
223175
```
224176

225177
```json
226178
{
227-
"status": 409,
179+
"status": 401,
228180
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
229-
"title": "Conflict",
230-
"detail": "Duplicated key for book with ID 20."
181+
"title": "Unauthorized",
182+
"detail": "You are not authenticated. Please login."
183+
}
184+
````
185+
186+
#### ForbiddenProblem
187+
188+
```php
189+
use Phpro\ApiProblem\Http\ForbiddenProblem;
190+
191+
new ForbiddenProblem('Not authorized to access gold.');
192+
```
193+
194+
```json
195+
{
196+
"status": 403,
197+
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
198+
"title": "Forbidden",
199+
"detail": "Not authorized to access gold."
231200
}
232201
````
233202

234-
#### NotModifiedProblem
203+
#### NotFoundProblem
235204

236205
```php
237-
use Phpro\ApiProblem\Http\NotModifiedProblem;
206+
use Phpro\ApiProblem\Http\NotFoundProblem;
238207

239-
new NotModifiedProblem('Nothing has changed!.');
208+
new NotFoundProblem('The book with ID 20 could not be found.');
240209
```
241210

242211
```json
243212
{
244-
"status": 304,
213+
"status": 404,
245214
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
246-
"title": "Not Modified",
247-
"detail": "Nothing has changed!"
215+
"title": "Not found",
216+
"detail": "The book with ID 20 could not be found."
248217
}
249218
````
250219

@@ -265,6 +234,22 @@ new MethodNotAllowedProblem('Only POST and GET allowed.');
265234
}
266235
````
267236

237+
#### ConflictProblem
238+
239+
```php
240+
use Phpro\ApiProblem\Http\ConflictProblem;
241+
new ConflictProblem('Duplicated key for book with ID 20.');
242+
```
243+
244+
```json
245+
{
246+
"status": 409,
247+
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
248+
"title": "Conflict",
249+
"detail": "Duplicated key for book with ID 20."
250+
}
251+
````
252+
268253
#### PreconditionFailedProblem
269254

270255
```php

spec/Http/UnsupportedMediaTypeProblemSpec.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,32 @@ public function it_can_parse_to_array(): void
3434
'detail' => 'unsupported media type',
3535
]);
3636
}
37+
38+
public function it_should_be_a_media_type_with_allowed_content_encodings(): void
39+
{
40+
$allowedEncodings = ['gzip', 'identity'];
41+
$currentEncoding = ['compress', 'none'];
42+
43+
$this->beConstructedThrough('invalidContentEncoding', [$allowedEncodings, $currentEncoding]);
44+
$this->toArray()->shouldBe([
45+
'status' => 415,
46+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
47+
'title' => HttpApiProblem::getTitleForStatusCode(415),
48+
'detail' => 'compress and none not allowed. Should be: gzip or identity',
49+
]);
50+
}
51+
52+
public function it_should_be_a_media_type_with_allowed_content_types(): void
53+
{
54+
$allowedEncodings = ['text/html', 'multipart/form-data'];
55+
$currentEncoding = 'application/json';
56+
57+
$this->beConstructedThrough('invalidContentType', [$allowedEncodings, $currentEncoding]);
58+
$this->toArray()->shouldBe([
59+
'status' => 415,
60+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
61+
'title' => HttpApiProblem::getTitleForStatusCode(415),
62+
'detail' => 'application/json not allowed. Should be: text/html or multipart/form-data',
63+
]);
64+
}
3765
}

src/Http/UnsupportedMediaTypeProblem.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,23 @@ public function __construct(string $detail)
1212
'detail' => $detail,
1313
]);
1414
}
15+
16+
public static function invalidContentEncoding(array $allowedEncodings, array $providedEncodings): self
17+
{
18+
$allowedEncodings[] = implode(' or ', array_splice($allowedEncodings, -2));
19+
$providedEncodings[] = implode(' and ', array_splice($providedEncodings, -2));
20+
21+
$detail = implode(', ', $providedEncodings).' not allowed. Should be: '.implode(', ', $allowedEncodings);
22+
23+
return new self($detail);
24+
}
25+
26+
public static function invalidContentType(array $allowedTypes, string $providedType): self
27+
{
28+
$allowedTypes[] = implode(' or ', array_splice($allowedTypes, -2));
29+
30+
$detail = $providedType.' not allowed. Should be: '.implode(', ', $allowedTypes);
31+
32+
return new self($detail);
33+
}
1534
}

0 commit comments

Comments
 (0)