diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 5486cbd..eeecb6f 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -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. * @@ -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; diff --git a/tests/TestCase/Controller/ApiProxyTraitTest.php b/tests/TestCase/Controller/ApiProxyTraitTest.php index b1ec25e..18d63e8 100644 --- a/tests/TestCase/Controller/ApiProxyTraitTest.php +++ b/tests/TestCase/Controller/ApiProxyTraitTest.php @@ -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. *