Skip to content

Commit 0a48401

Browse files
[12.x] Add Http::requestException() (#55241)
* add helper * test * Update Factory.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent c8363f6 commit 0a48401

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Illuminate/Http/Client/Factory.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,42 @@ public function globalOptions($options)
153153
* @return \GuzzleHttp\Promise\PromiseInterface
154154
*/
155155
public static function response($body = null, $status = 200, $headers = [])
156+
{
157+
return Create::promiseFor(
158+
static::psr7Response($body, $status, $headers)
159+
);
160+
}
161+
162+
/**
163+
* Create a new PSR-7 response instance for use during stubbing.
164+
*
165+
* @param array|string|null $body
166+
* @param int $status
167+
* @param array<string, mixed> $headers
168+
* @return \GuzzleHttp\Psr7\Response
169+
*/
170+
public static function psr7Response($body = null, $status = 200, $headers = [])
156171
{
157172
if (is_array($body)) {
158173
$body = json_encode($body);
159174

160175
$headers['Content-Type'] = 'application/json';
161176
}
162177

163-
$response = new Psr7Response($status, $headers, $body);
178+
return new Psr7Response($status, $headers, $body);
179+
}
164180

165-
return Create::promiseFor($response);
181+
/**
182+
* Create a new RequestException instance for use during stubbing.
183+
*
184+
* @param array|string|null $body
185+
* @param int $status
186+
* @param array<string, mixed> $headers
187+
* @return \Illuminate\Http\Client\RequestException
188+
*/
189+
public static function requestException($body = null, $status = 200, $headers = [])
190+
{
191+
return new RequestException(new Response(static::psr7Response($body, $status, $headers)));
166192
}
167193

168194
/**

tests/Http/HttpClientTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,16 @@ public function testRequestsWillBeWaitingSleepMillisecondsReceivedInBackoffArray
23622362
]);
23632363
}
23642364

2365+
public function testRequestException()
2366+
{
2367+
$requestException = $this->factory->requestException(['code' => 'not_found'], 404, ['X-RateLimit-Remaining' => 199]);
2368+
2369+
$this->assertInstanceOf(RequestException::class, $requestException);
2370+
$this->assertEqualsCanonicalizing(['code' => 'not_found'], $requestException->response->json());
2371+
$this->assertEquals(404, $requestException->response->status());
2372+
$this->assertEquals(199, $requestException->response->header('X-RateLimit-Remaining'));
2373+
}
2374+
23652375
public function testFakeConnectionException()
23662376
{
23672377
$this->factory->fake($this->factory->failedConnection('Fake'));

0 commit comments

Comments
 (0)