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
22 changes: 15 additions & 7 deletions src/Browser/HttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,27 @@ final public function files(): array
final public function server(): array
{
$server = $this->options['server'];
$headers = $this->options['headers'];
$headers = \array_combine(
\array_map(
static fn($header) => \mb_strtoupper(\str_replace('-', '_', $header)),
\array_keys($this->options['headers'])
),
$this->options['headers']
);

if (null !== $this->options['json'] && !\array_key_exists('ACCEPT', $headers)) {
$headers['ACCEPT'] = 'application/json';
}

if (null !== $this->options['json']) {
$headers['Content-Type'] = $headers['Accept'] = 'application/json';
if (null !== $this->options['json'] && !\array_key_exists('CONTENT_TYPE', $headers)) {
$headers['CONTENT_TYPE'] = 'application/json';
}

if (false !== $this->options['ajax']) {
$headers['X-Requested-With'] = 'XMLHttpRequest';
if (false !== $this->options['ajax'] && !\array_key_exists('X_REQUESTED_WITH', $headers)) {
$headers['X_REQUESTED_WITH'] = 'XMLHttpRequest';
}

foreach ($headers as $header => $value) {
$header = \mb_strtoupper(\str_replace('-', '_', $header));

// content type header cannot have HTTP_ prefix
if ('CONTENT_TYPE' !== $header) {
$header = "HTTP_{$header}";
Expand Down
2 changes: 2 additions & 0 deletions tests/BrowserKitBrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public function http_method_actions(): void
->post('/http-method', CustomHttpOptions::api('my-token'))
->assertContains('"content-type":["application\/json"]')
->assertContains('"x-token":["my-token"]')
->post('/http-method', HttpOptions::json()->withHeader('content-type', 'application/ld+json'))
->assertContains('"content-type":["application\/ld+json"]')
;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/HttpOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,26 @@ public function can_merge_with_http_options_object(): void
$options->server()
);
}

/**
* @test
*/
public function can_override_json_and_ajax_headers(): void
{
$options = HttpOptions::jsonAjax()
->withHeader('Accept', 'application/ld+json')
->withHeader('Content-Type', 'application/ld+json')
->withHeader('X-Requested-With', 'something')
;

$this->assertNull($options->body());
$this->assertSame(
[
'HTTP_ACCEPT' => 'application/ld+json',
'CONTENT_TYPE' => 'application/ld+json',
'HTTP_X_REQUESTED_WITH' => 'something',
],
$options->server()
);
}
}