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
This commit is contained in:
andreaspiening 2018-05-08 12:09:02 +12:00 committed by Robbie Averill
parent 73c61e7d4c
commit cacf25fb9b
2 changed files with 7 additions and 7 deletions

View File

@ -489,7 +489,7 @@ class RestfulServer extends Controller
return $obj; return $obj;
} }
$this->getResponse()->setStatusCode(200); // Success $this->getResponse()->setStatusCode(202); // Accepted
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
// Append the default extension for the output format to the Location header // Append the default extension for the output format to the Location header

View File

@ -144,7 +144,7 @@ class RestfulServerTest extends SapphireTest
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; $_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
$_SERVER['PHP_AUTH_PW'] = 'editor'; $_SERVER['PHP_AUTH_PW'] = 'editor';
$response = Director::test($url, $data, null, 'PUT'); $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_USER']);
unset($_SERVER['PHP_AUTH_PW']); unset($_SERVER['PHP_AUTH_PW']);
@ -237,7 +237,7 @@ class RestfulServerTest extends SapphireTest
'Content-Type' => 'application/x-www-form-urlencoded' 'Content-Type' => 'application/x-www-form-urlencoded'
); );
$response = Director::test($url, null, null, 'PUT', $body, $headers); $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 // Assumption: XML is default output
$responseArr = Convert::xml2array($response->getBody()); $responseArr = Convert::xml2array($response->getBody());
$this->assertEquals($comment1->ID, $responseArr['ID']); $this->assertEquals($comment1->ID, $responseArr['ID']);
@ -306,7 +306,7 @@ class RestfulServerTest extends SapphireTest
'Content-Type'=>'application/json', 'Content-Type'=>'application/json',
'Accept' => 'application/json' 'Accept' => 'application/json'
)); ));
$this->assertEquals(200, $response->getStatusCode()); // Updated $this->assertEquals(202, $response->getStatusCode()); // Accepted
$obj = Convert::json2obj($response->getBody()); $obj = Convert::json2obj($response->getBody());
$this->assertEquals($comment1->ID, $obj->ID); $this->assertEquals($comment1->ID, $obj->ID);
$this->assertEquals('updated', $obj->Comment); $this->assertEquals('updated', $obj->Comment);
@ -316,7 +316,7 @@ class RestfulServerTest extends SapphireTest
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.json"; $url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.json";
$body = '{"Comment":"updated"}'; $body = '{"Comment":"updated"}';
$response = Director::test($url, null, null, 'PUT', $body); $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')); $this->assertEquals($url, $response->getHeader('Location'));
$obj = Convert::json2obj($response->getBody()); $obj = Convert::json2obj($response->getBody());
$this->assertEquals($comment1->ID, $obj->ID); $this->assertEquals($comment1->ID, $obj->ID);
@ -338,7 +338,7 @@ class RestfulServerTest extends SapphireTest
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID; $url = "{$this->baseURI}/api/v1/$urlSafeClassname/" . $comment1->ID;
$body = '<RestfulServerTestComment><Comment>updated</Comment></RestfulServerTestComment>'; $body = '<RestfulServerTestComment><Comment>updated</Comment></RestfulServerTestComment>';
$response = Director::test($url, null, null, 'PUT', $body, array('Content-Type'=>'text/xml')); $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()); $obj = Convert::xml2array($response->getBody());
$this->assertEquals($comment1->ID, $obj['ID']); $this->assertEquals($comment1->ID, $obj['ID']);
$this->assertEquals('updated', $obj['Comment']); $this->assertEquals('updated', $obj['Comment']);
@ -348,7 +348,7 @@ class RestfulServerTest extends SapphireTest
$url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.xml"; $url = "{$this->baseURI}/api/v1/$urlSafeClassname/{$comment1->ID}.xml";
$body = '<RestfulServerTestComment><Comment>updated</Comment></RestfulServerTestComment>'; $body = '<RestfulServerTestComment><Comment>updated</Comment></RestfulServerTestComment>';
$response = Director::test($url, null, null, 'PUT', $body); $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')); $this->assertEquals($url, $response->getHeader('Location'));
$obj = Convert::xml2array($response->getBody()); $obj = Convert::xml2array($response->getBody());
$this->assertEquals($comment1->ID, $obj['ID']); $this->assertEquals($comment1->ID, $obj['ID']);