From 2df873412c268d45602d119765e073d6487e5e9d Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 16 Jul 2013 23:38:57 -0400 Subject: [PATCH 1/2] Add CakeResponse::location() This method provides an easy to use interface to get/set the location header in a response object. This is primarily to facilitate future development in 3.x --- lib/Cake/Network/CakeResponse.php | 16 ++++++++++++++++ lib/Cake/Test/Case/Network/CakeResponseTest.php | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index 999efa66e..9d680959b 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -584,6 +584,22 @@ class CakeResponse { return $this->_headers; } +/** + * Acccessor for the location header. + * + * Get/Set the Location header value. + * @param null|string $url Either null to get the current location, or a string to set one. + * @return string|null When setting the location null will be returned. When reading the location + * a string of the current location header value (if any) will be returned. + */ + public function location($url = null) { + if ($url === null) { + $headers = $this->header(); + return isset($headers['Location']) ? $headers['Location'] : null; + } + $this->header('Location', $url); + } + /** * Buffers the response message to be sent * if $content is null the current buffer is returned diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 598faa5b7..746572a70 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -1481,4 +1481,16 @@ class CakeResponseTest extends CakeTestCase { $result = $response->send(); } +/** + * Test the location method. + * + * @return void + */ + public function testLocation() { + $response = new CakeResponse(); + $this->assertNull($response->location()); + $this->assertNull($response->location('http://example.org')); + $this->assertEquals('http://example.org', $response->location()); + } + } From 6c18f6a2316c9adbb32935b316fd595cde9383c2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 17 Jul 2013 09:47:10 -0400 Subject: [PATCH 2/2] Add failure messages to test assertions. --- lib/Cake/Test/Case/Network/CakeResponseTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 746572a70..952efc47d 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -1488,9 +1488,9 @@ class CakeResponseTest extends CakeTestCase { */ public function testLocation() { $response = new CakeResponse(); - $this->assertNull($response->location()); - $this->assertNull($response->location('http://example.org')); - $this->assertEquals('http://example.org', $response->location()); + $this->assertNull($response->location(), 'No header should be set.'); + $this->assertNull($response->location('http://example.org'), 'Setting a location should return null'); + $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.'); } }