Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpClient] Add HttpOptions->addHeader as a shortcut to add an heade…
…r in an existing options object
  • Loading branch information
Dean151 authored and fabpot committed Dec 8, 2023
commit 266e50f4c489d93c1b0d2bafbd9fb411643b31f4
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpClient/HttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public function setQuery(array $query): static
return $this;
}

/**
* @return $this
*/
public function addHeader(string $key, string $value): static
{
$this->options['headers'] ??= [];
$this->options['headers'][$key] = $value;

return $this;
}

/**
* @return $this
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ public function testSetAuthBearer()
{
$this->assertSame('foobar', (new HttpOptions())->setAuthBearer('foobar')->toArray()['auth_bearer']);
}

public function testAddHeader()
{
$options = new HttpOptions();
$options->addHeader('Accept', 'application/json');
$this->assertSame(['Accept' => 'application/json'], $options->toArray()['headers']);
$options->addHeader('Accept-Language', 'en-US,en;q=0.5');
$this->assertSame(['Accept' => 'application/json', 'Accept-Language' => 'en-US,en;q=0.5'], $options->toArray()['headers']);
$options->addHeader('Accept', 'application/html');
$this->assertSame(['Accept' => 'application/html', 'Accept-Language' => 'en-US,en;q=0.5'], $options->toArray()['headers']);
}
}