PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.6.0
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.6.0
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / guzzle / tests / Exception / RequestExceptionTest.php

RequestExceptionTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.6.0, at vendor/guzzlehttp/guzzle/tests/Exception/RequestExceptionTest.php

211 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace GuzzleHttp\Tests\Exception;
3
4 use GuzzleHttp\Exception\RequestException;
5 use GuzzleHttp\Psr7\Request;
6 use GuzzleHttp\Psr7\Response;
7 use GuzzleHttp\Psr7\Stream;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11 * @covers \GuzzleHttp\Exception\RequestException
12 */
13 class RequestExceptionTest extends TestCase
14 {
15 public function testHasRequestAndResponse()
16 {
17 $req = new Request('GET', '/');
18 $res = new Response(200);
19 $e = new RequestException('foo', $req, $res);
20 self::assertSame($req, $e->getRequest());
21 self::assertSame($res, $e->getResponse());
22 self::assertTrue($e->hasResponse());
23 self::assertSame('foo', $e->getMessage());
24 }
25
26 public function testCreatesGenerateException()
27 {
28 $e = RequestException::create(new Request('GET', '/'));
29 self::assertSame('Error completing request', $e->getMessage());
30 self::assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
31 }
32
33 public function testCreatesClientErrorResponseException()
34 {
35 $e = RequestException::create(new Request('GET', '/'), new Response(400));
36 self::assertContains(
37 'GET /',
38 $e->getMessage()
39 );
40 self::assertContains(
41 '400 Bad Request',
42 $e->getMessage()
43 );
44 self::assertInstanceOf('GuzzleHttp\Exception\ClientException', $e);
45 }
46
47 public function testCreatesServerErrorResponseException()
48 {
49 $e = RequestException::create(new Request('GET', '/'), new Response(500));
50 self::assertContains(
51 'GET /',
52 $e->getMessage()
53 );
54 self::assertContains(
55 '500 Internal Server Error',
56 $e->getMessage()
57 );
58 self::assertInstanceOf('GuzzleHttp\Exception\ServerException', $e);
59 }
60
61 public function testCreatesGenericErrorResponseException()
62 {
63 $e = RequestException::create(new Request('GET', '/'), new Response(300));
64 self::assertContains(
65 'GET /',
66 $e->getMessage()
67 );
68 self::assertContains(
69 '300 ',
70 $e->getMessage()
71 );
72 self::assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
73 }
74
75 /**
76 * @expectedException InvalidArgumentException
77 * @expectedExceptionMessage Status code must be an integer value between 1xx and 5xx.
78 */
79 public function testThrowsInvalidArgumentExceptionOnOutOfBoundsResponseCode()
80 {
81 throw RequestException::create(new Request('GET', '/'), new Response(600));
82 }
83
84 public function dataPrintableResponses()
85 {
86 return [
87 ['You broke the test!'],
88 ['<h1>zlomený zkouška</h1>'],
89 ['{"tester": "Philépe Gonzalez"}'],
90 ["<xml>\n\t<text>Your friendly test</text>\n</xml>"],
91 ['document.body.write("here comes a test");'],
92 ["body:before {\n\tcontent: 'test style';\n}"],
93 ];
94 }
95
96 /**
97 * @dataProvider dataPrintableResponses
98 */
99 public function testCreatesExceptionWithPrintableBodySummary($content)
100 {
101 $response = new Response(
102 500,
103 [],
104 $content
105 );
106 $e = RequestException::create(new Request('GET', '/'), $response);
107 self::assertContains(
108 $content,
109 $e->getMessage()
110 );
111 self::assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
112 }
113
114 public function testCreatesExceptionWithTruncatedSummary()
115 {
116 $content = str_repeat('+', 121);
117 $response = new Response(500, [], $content);
118 $e = RequestException::create(new Request('GET', '/'), $response);
119 $expected = str_repeat('+', 120) . ' (truncated...)';
120 self::assertContains($expected, $e->getMessage());
121 }
122
123 public function testExceptionMessageIgnoresEmptyBody()
124 {
125 $e = RequestException::create(new Request('GET', '/'), new Response(500));
126 self::assertStringEndsWith('response', $e->getMessage());
127 }
128
129 public function testCreatesExceptionWithoutPrintableBody()
130 {
131 $response = new Response(
132 500,
133 ['Content-Type' => 'image/gif'],
134 $content = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7') // 1x1 gif
135 );
136 $e = RequestException::create(new Request('GET', '/'), $response);
137 self::assertNotContains(
138 $content,
139 $e->getMessage()
140 );
141 self::assertInstanceOf('GuzzleHttp\Exception\RequestException', $e);
142 }
143
144 public function testHasStatusCodeAsExceptionCode()
145 {
146 $e = RequestException::create(new Request('GET', '/'), new Response(442));
147 self::assertSame(442, $e->getCode());
148 }
149
150 public function testWrapsRequestExceptions()
151 {
152 $e = new \Exception('foo');
153 $r = new Request('GET', 'http://www.oo.com');
154 $ex = RequestException::wrapException($r, $e);
155 self::assertInstanceOf('GuzzleHttp\Exception\RequestException', $ex);
156 self::assertSame($e, $ex->getPrevious());
157 }
158
159 public function testDoesNotWrapExistingRequestExceptions()
160 {
161 $r = new Request('GET', 'http://www.oo.com');
162 $e = new RequestException('foo', $r);
163 $e2 = RequestException::wrapException($r, $e);
164 self::assertSame($e, $e2);
165 }
166
167 public function testCanProvideHandlerContext()
168 {
169 $r = new Request('GET', 'http://www.oo.com');
170 $e = new RequestException('foo', $r, null, null, ['bar' => 'baz']);
171 self::assertSame(['bar' => 'baz'], $e->getHandlerContext());
172 }
173
174 public function testObfuscateUrlWithUsername()
175 {
176 $r = new Request('GET', 'http://[email protected]');
177 $e = RequestException::create($r, new Response(500));
178 self::assertContains('http://[email protected]', $e->getMessage());
179 }
180
181 public function testObfuscateUrlWithUsernameAndPassword()
182 {
183 $r = new Request('GET', 'http://user:[email protected]');
184 $e = RequestException::create($r, new Response(500));
185 self::assertContains('http://user:***@www.oo.com', $e->getMessage());
186 }
187
188 public function testGetResponseBodySummaryOfNonReadableStream()
189 {
190 self::assertNull(RequestException::getResponseBodySummary(new Response(500, [], new ReadSeekOnlyStream())));
191 }
192 }
193
194 final class ReadSeekOnlyStream extends Stream
195 {
196 public function __construct()
197 {
198 parent::__construct(fopen('php://memory', 'wb'));
199 }
200
201 public function isSeekable()
202 {
203 return true;
204 }
205
206 public function isReadable()
207 {
208 return false;
209 }
210 }
211