Skip to content

Commit

Permalink
[10.x] Add PendingRequest withHeader() method (#47474)
Browse files Browse the repository at this point in the history
* Add PendingRequest `withHeader()` method

* Space

* Test names

* Fix code style

* Update PendingRequest.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
ralphjsmit and taylorotwell committed Jun 18, 2023
1 parent f527d04 commit 0738aa1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ public function withHeaders(array $headers)
});
}

/**
* Add the given header to the request.
*
* @param string $name
* @param mixed $value
* @return $this
*/
public function withHeader($name, $value)
{
return $this->withHeaders([$name => $value]);
}

/**
* Replace the given headers on the request.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,34 @@ public function testItCanReplaceHeadersWhenNoHeadersYetSet()
});
}

public function testCanConfirmSingleStringHeader()
{
$this->factory->fake();

$this->factory->withHeader('X-Test-Header', 'foo')->post('http://foo.com/json');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/json' &&
$request->hasHeaders([
'X-Test-Header' => 'foo',
]);
});
}

public function testCanConfirmSingleArrayHeader()
{
$this->factory->fake();

$this->factory->withHeader('X-Test-ArrayHeader', ['bar', 'baz'])->post('http://foo.com/json');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/json' &&
$request->hasHeaders([
'X-Test-ArrayHeader' => ['bar', 'baz'],
]);
});
}

public function testExceptionAccessorOnSuccess()
{
$resp = new Response(new Psr7Response());
Expand Down

0 comments on commit 0738aa1

Please sign in to comment.