Skip to content

Commit

Permalink
[8.x] HTTP client: only allow a single User-Agent header (#39085)
Browse files Browse the repository at this point in the history
Co-authored-by: jules <[email protected]>
  • Loading branch information
julesjanssen and julesjanssen authored Oct 5, 2021
1 parent f8f524e commit 060960a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ public function withToken($token, $type = 'Bearer')
*/
public function withUserAgent($userAgent)
{
return $this->withHeaders(['User-Agent' => $userAgent]);
return tap($this, function ($request) use ($userAgent) {
return $this->options['headers']['User-Agent'] = trim($userAgent);
});
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ public function testItCanSendUserAgent()
});
}

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

$this->factory->withUserAgent('Laravel')
->withUserAgent('FooBar')
->post('http://foo.com/json');

$this->factory->assertSent(function (Request $request) {
$userAgent = $request->header('User-Agent');

return $request->url() === 'http://foo.com/json' &&
count($userAgent) === 1 &&
$userAgent[0] === 'FooBar';
});
}

public function testSequenceBuilder()
{
$this->factory->fake([
Expand Down

0 comments on commit 060960a

Please sign in to comment.