diff --git a/src/Network/Request.php b/src/Network/Request.php index ea965f893c1..384ce72a03e 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.php @@ -1010,6 +1010,19 @@ public function withMethod($method) return $new; } + /** + * Get all the server environment parameters. + * + * Read all of the 'environment' or 'server' data that was + * used to create this request. + * + * @return array + */ + public function getServerParams() + { + return $this->_environment; + } + /** * Get all the query parameters. * @@ -1367,6 +1380,29 @@ public function cookie($key) return null; } + /** + * Get all the cookie data from the request. + * + * @return array An array of cookie data. + */ + public function getCookieParams() + { + return $this->cookies; + } + + /** + * Replace the cookies and get a new request instance. + * + * @param array $cookies The new cookie data to use. + * @return static + */ + public function withCookieParams(array $cookies) + { + $new = clone $this; + $new->cookies = $cookies; + return $new; + } + /** * Get/Set value from the request's environment data. * Fallback to using env() if key not set in $environment property. diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index cebbef568ac..de7f2205a1c 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -761,6 +761,7 @@ public function testWithMethod() 'environment' => ['REQUEST_METHOD' => 'delete'] ]); $new = $request->withMethod('put'); + $this->assertNotSame($new, $request); $this->assertEquals('delete', $request->getMethod()); $this->assertEquals('put', $new->getMethod()); } @@ -2123,6 +2124,30 @@ public function testWithQueryParams() $this->assertSame(['new' => 'data'], $new->getQueryParams()); } + /** + * Test getServerParams + * + * @return void + */ + public function testGetServerParams() + { + $vars = [ + 'REQUEST_METHOD' => 'PUT', + 'HTTPS' => 'on', + ]; + + $request = new Request([ + 'environment' => $vars + ]); + $expected = $vars + [ + 'CONTENT_TYPE' => null, + 'HTTP_CONTENT_TYPE' => null, + 'HTTP_X_HTTP_METHOD_OVERRIDE' => null, + 'ORIGINAL_REQUEST_METHOD' => 'PUT', + ]; + $this->assertSame($expected, $request->getServerParams()); + } + /** * Test using param() * @@ -2511,7 +2536,7 @@ public function testIsRequested() * * @return void */ - public function testReadCookie() + public function testCookie() { $request = new Request([ 'cookies' => [ @@ -2525,6 +2550,37 @@ public function testReadCookie() $this->assertNull($result); } + /** + * Test getCookieParams() + * + * @return void + */ + public function testGetCookieParams() + { + $cookies = [ + 'testing' => 'A value in the cookie' + ]; + $request = new Request(['cookies' => $cookies]); + $this->assertSame($cookies, $request->getCookieParams()); + } + + /** + * Test withCookieParams() + * + * @return void + */ + public function testWithCookieParams() + { + $cookies = [ + 'testing' => 'A value in the cookie' + ]; + $request = new Request(['cookies' => $cookies]); + $new = $request->withCookieParams(['remember_me' => 1]); + $this->assertNotSame($new, $request); + $this->assertSame($cookies, $request->getCookieParams()); + $this->assertSame(['remember_me' => 1], $new->getCookieParams()); + } + /** * TestAllowMethod *