Skip to content

Commit ed1ebac

Browse files
committed
[HttpClient] Add ResponseStatusCodeTrait that allows to check for common HTTP status codes
1 parent f8e605c commit ed1ebac

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Add `ResponseStatusCodeTrait` that allows to check for common HTTP status codes
8+
49
7.3
510
---
611

src/Symfony/Component/HttpClient/Response/CommonResponseTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
trait CommonResponseTrait
2828
{
29+
use ResponseStatusCodeTrait;
30+
2931
/**
3032
* @var callable|null A callback that tells whether we're waiting for response headers
3133
*/
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Response;
13+
14+
/**
15+
* Checking HTTP response status codes explicitly.
16+
*
17+
* @author Jimmy Martin <jimmy.martin952@gmail.com>
18+
*/
19+
trait ResponseStatusCodeTrait
20+
{
21+
public function isOk(): bool
22+
{
23+
return 200 === $this->getHttpCode();
24+
}
25+
26+
public function isCreated(): bool
27+
{
28+
return 201 === $this->getHttpCode();
29+
}
30+
31+
public function isAccepted(): bool
32+
{
33+
return 202 === $this->getHttpCode();
34+
}
35+
36+
public function isNoContent(int $statusCode = 204): bool
37+
{
38+
return $statusCode === $this->getHttpCode() && '' === $this->getContent(false);
39+
}
40+
41+
public function isMovedPermanently(): bool
42+
{
43+
return 301 === $this->getHttpCode();
44+
}
45+
46+
public function isFound(): bool
47+
{
48+
return 302 === $this->getHttpCode();
49+
}
50+
51+
public function isNotModified(): bool
52+
{
53+
return 304 === $this->getHttpCode();
54+
}
55+
56+
public function isBadRequest(): bool
57+
{
58+
return 400 === $this->getHttpCode();
59+
}
60+
61+
public function isUnauthorized(): bool
62+
{
63+
return 401 === $this->getHttpCode();
64+
}
65+
66+
public function isPaymentRequired(): bool
67+
{
68+
return 402 === $this->getHttpCode();
69+
}
70+
71+
public function isForbidden(): bool
72+
{
73+
return 403 === $this->getHttpCode();
74+
}
75+
76+
public function isNotFound(): bool
77+
{
78+
return 404 === $this->getHttpCode();
79+
}
80+
81+
public function isMethodNotAllowed(): bool
82+
{
83+
return 405 === $this->getHttpCode();
84+
}
85+
86+
public function isNotAcceptable(): bool
87+
{
88+
return 406 === $this->getHttpCode();
89+
}
90+
91+
public function isRequestTimeout(): bool
92+
{
93+
return 408 === $this->getHttpCode();
94+
}
95+
96+
public function isConflict(): bool
97+
{
98+
return 409 === $this->getHttpCode();
99+
}
100+
101+
public function isGone(): bool
102+
{
103+
return 410 === $this->getHttpCode();
104+
}
105+
106+
public function isUnprocessableEntity(): bool
107+
{
108+
return 422 === $this->getHttpCode();
109+
}
110+
111+
public function isTooManyRequests(): bool
112+
{
113+
return 429 === $this->getHttpCode();
114+
}
115+
116+
private function getHttpCode(): mixed
117+
{
118+
return $this->getInfo('http_code');
119+
}
120+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests\Response;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
16+
17+
class ResponseStatusCodeTraitTest extends TestCase
18+
{
19+
public function testIsOk()
20+
{
21+
$response = self::createResponse(200);
22+
$this->assertTrue($response->isOk());
23+
}
24+
25+
public function testIsCreated()
26+
{
27+
$response = self::createResponse(201);
28+
$this->assertTrue($response->isCreated());
29+
}
30+
31+
public function testIsAccepted()
32+
{
33+
$response = self::createResponse(202);
34+
$this->assertTrue($response->isAccepted());
35+
}
36+
37+
public function testIsNoContent()
38+
{
39+
$response = self::createEmptyContentResponse();
40+
$this->assertTrue($response->isNoContent());
41+
42+
$response = self::createEmptyContentResponse(204, json_encode(['foo' => 'bar', 'bar' => 'baz']));
43+
$this->assertFalse($response->isNoContent());
44+
45+
$fakeStatusCode = 999;
46+
$response = self::createEmptyContentResponse($fakeStatusCode);
47+
$this->assertTrue($response->isNoContent($fakeStatusCode));
48+
49+
$response = self::createEmptyContentResponse($fakeStatusCode);
50+
// we don't pass any status code, so the default status code (204) is used
51+
$this->assertFalse($response->isNoContent());
52+
}
53+
54+
public function testIsMovedPermanently()
55+
{
56+
$response = self::createResponse(301);
57+
$this->assertTrue($response->isMovedPermanently());
58+
}
59+
60+
public function testIsFound()
61+
{
62+
$response = self::createResponse(302);
63+
$this->assertTrue($response->isFound());
64+
}
65+
66+
public function testIsNotModified()
67+
{
68+
$response = self::createResponse(304);
69+
$this->assertTrue($response->isNotModified());
70+
}
71+
72+
public function testIsBadRequest()
73+
{
74+
$response = self::createResponse(400);
75+
$this->assertTrue($response->isBadRequest());
76+
}
77+
78+
public function testIsUnauthorized()
79+
{
80+
$response = self::createResponse(401);
81+
$this->assertTrue($response->isUnauthorized());
82+
}
83+
84+
public function testIsPaymentRequired()
85+
{
86+
$response = self::createResponse(402);
87+
$this->assertTrue($response->isPaymentRequired());
88+
}
89+
90+
public function testIsForbidden()
91+
{
92+
$response = self::createResponse(403);
93+
$this->assertTrue($response->isForbidden());
94+
}
95+
96+
public function testIsNotFound()
97+
{
98+
$response = self::createResponse(404);
99+
$this->assertTrue($response->isNotFound());
100+
}
101+
102+
public function testIsMethodNotAllowed()
103+
{
104+
$response = self::createResponse(405);
105+
$this->assertTrue($response->isMethodNotAllowed());
106+
}
107+
108+
public function testIsNotAcceptable()
109+
{
110+
$response = self::createResponse(406);
111+
$this->assertTrue($response->isNotAcceptable());
112+
}
113+
114+
public function testIsRequestTimeout()
115+
{
116+
$response = self::createResponse(408);
117+
$this->assertTrue($response->isRequestTimeout());
118+
}
119+
120+
public function testIsConflict()
121+
{
122+
$response = self::createResponse(409);
123+
$this->assertTrue($response->isConflict());
124+
}
125+
126+
public function testIsGone()
127+
{
128+
$response = self::createResponse(410);
129+
$this->assertTrue($response->isGone());
130+
}
131+
132+
public function testIsUnprocessableEntity()
133+
{
134+
$response = self::createResponse(422);
135+
$this->assertTrue($response->isUnprocessableEntity());
136+
}
137+
138+
public function testIsTooManyRequests()
139+
{
140+
$response = self::createResponse(429);
141+
$this->assertTrue($response->isTooManyRequests());
142+
}
143+
144+
private static function createResponse(int $statusCode, array $content = ['foo' => 'bar']): MockResponse
145+
{
146+
$responseMock = new MockResponse(json_encode($content), [
147+
'http_code' => $statusCode,
148+
]);
149+
150+
$response = MockResponse::fromRequest('GET', 'https://example.com/some-endpoint', [], $responseMock);
151+
$response->toArray(false);
152+
153+
return $response;
154+
}
155+
156+
private static function createEmptyContentResponse(int $statusCode = 204, string $content = ''): MockResponse
157+
{
158+
$responseMock = new MockResponse($content, [
159+
'http_code' => $statusCode,
160+
]);
161+
162+
$response = MockResponse::fromRequest('GET', 'https://example.com/some-endpoint', [], $responseMock);
163+
$response->getContent(false);
164+
165+
return $response;
166+
}
167+
}

0 commit comments

Comments
 (0)