From cacf25fb9b6f00a4297ea965bad1165cb3c3f66d Mon Sep 17 00:00:00 2001 From: andreaspiening Date: Tue, 8 May 2018 12:09:02 +1200 Subject: [PATCH] Fix infinite redirect after PUT (#62) * Fix infinite redirect after PUT by changing requestMethod through responsecode * Fix unit tests to reflect changes * Use 202 instead of 303 --- src/RestfulServer.php | 2 +- tests/unit/RestfulServerTest.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/RestfulServer.php b/src/RestfulServer.php index cda2bdc..69a60e0 100644 --- a/src/RestfulServer.php +++ b/src/RestfulServer.php @@ -489,7 +489,7 @@ class RestfulServer extends Controller return $obj; } - $this->getResponse()->setStatusCode(200); // Success + $this->getResponse()->setStatusCode(202); // Accepted $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); // Append the default extension for the output format to the Location header diff --git a/tests/unit/RestfulServerTest.php b/tests/unit/RestfulServerTest.php index 02266d6..0e2695c 100644 --- a/tests/unit/RestfulServerTest.php +++ b/tests/unit/RestfulServerTest.php @@ -144,7 +144,7 @@ class RestfulServerTest extends SapphireTest $_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; $_SERVER['PHP_AUTH_PW'] = 'editor'; $response = Director::test($url, $data, null, 'PUT'); - $this->assertEquals(200, $response->getStatusCode()); // Success + $this->assertEquals(202, $response->getStatusCode()); // Accepted unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); @@ -237,7 +237,7 @@ class RestfulServerTest extends SapphireTest 'Content-Type' => 'application/x-www-form-urlencoded' ); $response = Director::test($url, null, null, 'PUT', $body, $headers); - $this->assertEquals(200, $response->getStatusCode()); // Success + $this->assertEquals(202, $response->getStatusCode()); // Accepted // Assumption: XML is default output $responseArr = Convert::xml2array($response->getBody()); $this->assertEquals($comment1->ID, $responseArr['ID']); @@ -306,7 +306,7 @@ class RestfulServerTest extends SapphireTest 'Content-Type'=>'application/json', 'Accept' => 'application/json' )); - $this->assertEquals(200, $response->getStatusCode()); // Updated + $this->assertEquals(202, $response->getStatusCode()); // Accepted $obj = Convert::json2obj($response->getBody()); $this->assertEquals($comment1->ID, $obj->ID); $this->assertEquals('updated', $obj->Comment); @@ -316,7 +316,7 @@ class RestfulServerTest extends SapphireTest $url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.json"; $body = '{"Comment":"updated"}'; $response = Director::test($url, null, null, 'PUT', $body); - $this->assertEquals(200, $response->getStatusCode()); // Updated + $this->assertEquals(202, $response->getStatusCode()); // Accepted $this->assertEquals($url, $response->getHeader('Location')); $obj = Convert::json2obj($response->getBody()); $this->assertEquals($comment1->ID, $obj->ID); @@ -338,7 +338,7 @@ class RestfulServerTest extends SapphireTest $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID; $body = 'updated'; $response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'text/xml')); - $this->assertEquals(200, $response->getStatusCode()); // Updated + $this->assertEquals(202, $response->getStatusCode()); // Accepted $obj = Convert::xml2array($response->getBody()); $this->assertEquals($comment1->ID, $obj['ID']); $this->assertEquals('updated', $obj['Comment']); @@ -348,7 +348,7 @@ class RestfulServerTest extends SapphireTest $url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.xml"; $body = 'updated'; $response = Director::test($url, null, null, 'PUT', $body); - $this->assertEquals(200, $response->getStatusCode()); // Updated + $this->assertEquals(202, $response->getStatusCode()); // Accepted $this->assertEquals($url, $response->getHeader('Location')); $obj = Convert::xml2array($response->getBody()); $this->assertEquals($comment1->ID, $obj['ID']);