Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit266e50f

Browse files
Dean151fabpot
authored andcommitted
[HttpClient] Add HttpOptions->addHeader as a shortcut to add an header in an existing options object
1 parent5d50fa8 commit266e50f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

‎src/Symfony/Component/HttpClient/HttpOptions.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ public function setQuery(array $query): static
6363
return$this;
6464
}
6565

66+
/**
67+
* @return $this
68+
*/
69+
publicfunctionaddHeader(string$key,string$value):static
70+
{
71+
$this->options['headers'] ??= [];
72+
$this->options['headers'][$key] =$value;
73+
74+
return$this;
75+
}
76+
6677
/**
6778
* @return $this
6879
*/

‎src/Symfony/Component/HttpClient/Tests/HttpOptionsTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ public function testSetAuthBearer()
3939
{
4040
$this->assertSame('foobar', (newHttpOptions())->setAuthBearer('foobar')->toArray()['auth_bearer']);
4141
}
42+
43+
publicfunctiontestAddHeader()
44+
{
45+
$options =newHttpOptions();
46+
$options->addHeader('Accept','application/json');
47+
$this->assertSame(['Accept' =>'application/json'],$options->toArray()['headers']);
48+
$options->addHeader('Accept-Language','en-US,en;q=0.5');
49+
$this->assertSame(['Accept' =>'application/json','Accept-Language' =>'en-US,en;q=0.5'],$options->toArray()['headers']);
50+
$options->addHeader('Accept','application/html');
51+
$this->assertSame(['Accept' =>'application/html','Accept-Language' =>'en-US,en;q=0.5'],$options->toArray()['headers']);
52+
}
4253
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp