Skip to content

Commit

Permalink
[HttpClient] Add HttpOptions->addHeader as a shortcut to add an heade…
Browse files Browse the repository at this point in the history
…r in an existing options object
  • Loading branch information
Dean151 authored and fabpot committed Dec 8, 2023
1 parent 5d50fa8 commit 266e50f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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']);
}
}

0 comments on commit 266e50f

Please sign in to comment.