From e2a00fcf7e93f413ad95b3e53557d2cca1488d27 Mon Sep 17 00:00:00 2001 From: Azuka Date: Wed, 23 Apr 2025 08:17:22 -0700 Subject: [PATCH] Add grabHttpHeaders() to return all response http headers --- src/Codeception/Module/REST.php | 17 +++++++++++++++++ tests/unit/Codeception/Module/RestTest.php | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Codeception/Module/REST.php b/src/Codeception/Module/REST.php index b7b6c33..7908289 100644 --- a/src/Codeception/Module/REST.php +++ b/src/Codeception/Module/REST.php @@ -200,6 +200,23 @@ protected function getRunningClient(): AbstractBrowser return $this->client; } + /** + * Returns all HTTP headers from the last response. + * + * ```php + * sendGet('/message/1'); + * $headers = $I->grabHttpHeaders(); + * ``` + * + * @part json + * @part xml + */ + public function grabHttpHeaders(): array + { + return $this->getRunningClient()->getInternalResponse()->getHeaders(); + } + /** * Sets a HTTP header to be used for all subsequent requests. Use [`unsetHttpHeader`](#unsetHttpHeader) to unset it. * diff --git a/tests/unit/Codeception/Module/RestTest.php b/tests/unit/Codeception/Module/RestTest.php index 114800a..da338f4 100644 --- a/tests/unit/Codeception/Module/RestTest.php +++ b/tests/unit/Codeception/Module/RestTest.php @@ -368,6 +368,18 @@ public function testUrlIsFull() $this->assertSame('http://localhost/api/v1/users', $request->getUri()); } + public function testGrabHttpHeaders() + { + $headers = [ + 'Cache-Control' => ['no-cache', 'no-store'], + 'Content_Language' => 'en-US' + ]; + $response = new SymfonyResponse("", 200, $headers); + $this->module->client->mockResponse($response); + $this->module->sendGET('/'); + $this->assertSame($headers, $this->module->grabHttpHeaders()); + } + public function testSeeHeaders() { $response = new SymfonyResponse("", 200, [