Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/Controller/ApiProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ trait ApiProxyTrait
{
use ViewVarsTrait;

/**
* Default allowed headers on proxied requests.
*
* @var array
*/
protected $allowedHeaders = ['Content-Type'];

/**
* An instance of a \Cake\Http\ServerRequest object that contains information about the current request.
*
Expand Down Expand Up @@ -182,6 +189,13 @@ protected function apiRequest(array $options): void
'body' => null,
'headers' => null,
];
foreach ($this->allowedHeaders as $headerName) {
if (empty($this->request->getHeader($headerName))) {
continue;
}
$options['headers'] = $options['headers'] ?? [];
$options['headers'][$headerName] = $this->request->getHeader($headerName);
}

if (empty($options['body'])) {
$options['body'] = null;
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Controller/ApiProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public function testGet(): void
}
}

/**
* Test get() method
*
* @return void
* @covers ::apiRequest()
*/
public function testGetWithHeaders(): void
{
$this->_request['headers'] = ['Content-type' => 'text/plain'];
$this->get('/api/users/1');
$this->assertResponseOk();
}

/**
* Test non found error proxied from API.
*
Expand Down