Skip to content

Commit 0738aa1

Browse files
[10.x] Add PendingRequest withHeader() method (#47474)
* Add PendingRequest `withHeader()` method * Space * Test names * Fix code style * Update PendingRequest.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent f527d04 commit 0738aa1

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ public function withHeaders(array $headers)
393393
});
394394
}
395395

396+
/**
397+
* Add the given header to the request.
398+
*
399+
* @param string $name
400+
* @param mixed $value
401+
* @return $this
402+
*/
403+
public function withHeader($name, $value)
404+
{
405+
return $this->withHeaders([$name => $value]);
406+
}
407+
396408
/**
397409
* Replace the given headers on the request.
398410
*

tests/Http/HttpClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,34 @@ public function testItCanReplaceHeadersWhenNoHeadersYetSet()
909909
});
910910
}
911911

912+
public function testCanConfirmSingleStringHeader()
913+
{
914+
$this->factory->fake();
915+
916+
$this->factory->withHeader('X-Test-Header', 'foo')->post('http://foo.com/json');
917+
918+
$this->factory->assertSent(function (Request $request) {
919+
return $request->url() === 'http://foo.com/json' &&
920+
$request->hasHeaders([
921+
'X-Test-Header' => 'foo',
922+
]);
923+
});
924+
}
925+
926+
public function testCanConfirmSingleArrayHeader()
927+
{
928+
$this->factory->fake();
929+
930+
$this->factory->withHeader('X-Test-ArrayHeader', ['bar', 'baz'])->post('http://foo.com/json');
931+
932+
$this->factory->assertSent(function (Request $request) {
933+
return $request->url() === 'http://foo.com/json' &&
934+
$request->hasHeaders([
935+
'X-Test-ArrayHeader' => ['bar', 'baz'],
936+
]);
937+
});
938+
}
939+
912940
public function testExceptionAccessorOnSuccess()
913941
{
914942
$resp = new Response(new Psr7Response());

0 commit comments

Comments
 (0)