Skip to content

Commit 060960a

Browse files
authored
[8.x] HTTP client: only allow a single User-Agent header (#39085)
Co-authored-by: jules <[email protected]>
1 parent f8f524e commit 060960a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ public function withToken($token, $type = 'Bearer')
374374
*/
375375
public function withUserAgent($userAgent)
376376
{
377-
return $this->withHeaders(['User-Agent' => $userAgent]);
377+
return tap($this, function ($request) use ($userAgent) {
378+
return $this->options['headers']['User-Agent'] = trim($userAgent);
379+
});
378380
}
379381

380382
/**

tests/Http/HttpClientTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ public function testItCanSendUserAgent()
295295
});
296296
}
297297

298+
public function testItOnlySendsOneUserAgentHeader()
299+
{
300+
$this->factory->fake();
301+
302+
$this->factory->withUserAgent('Laravel')
303+
->withUserAgent('FooBar')
304+
->post('http://foo.com/json');
305+
306+
$this->factory->assertSent(function (Request $request) {
307+
$userAgent = $request->header('User-Agent');
308+
309+
return $request->url() === 'http://foo.com/json' &&
310+
count($userAgent) === 1 &&
311+
$userAgent[0] === 'FooBar';
312+
});
313+
}
314+
298315
public function testSequenceBuilder()
299316
{
300317
$this->factory->fake([

0 commit comments

Comments
 (0)