diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index 4948900..b55cd0b 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -180,7 +180,7 @@ public function testInvalidBundle() { // Make sure the response is "Bad Request". $this->assertResponse(400); - $this->assertResponseBody('{"error":"\"bad_bundle_name\" is not a valid bundle type for denormalization."}'); + $this->assertResponseBody(NULL, '{"error":"\"bad_bundle_name\" is not a valid bundle type for denormalization."}'); } /** @@ -197,6 +197,6 @@ public function testMissingBundle() { // Make sure the response is "Bad Request". $this->assertResponse(400); - $this->assertResponseBody('{"error":"A string must be provided as a bundle value."}'); + $this->assertResponseBody(NULL, '{"error":"A string must be provided as a bundle value."}'); } } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index a2da3a7..4309e42 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -402,6 +402,10 @@ protected function removeNodeFieldsForNonAdminUsers(NodeInterface $node) { * Check to see if the HTTP request response body is identical to the expected * value. * + * + * @param $code + * (optional) Response code. For example 200 is a successful page request. For a list + * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. * @param $expected * The first value to check. * @param $message @@ -418,7 +422,11 @@ protected function removeNodeFieldsForNonAdminUsers(NodeInterface $node) { * @return bool * TRUE if the assertion succeeded, FALSE otherwise. */ - protected function assertResponseBody($expected, $message = '', $group = 'REST Response') { + protected function assertResponseBody($code = NULL, $expected, $message = '', $group = 'REST Response') { + if ($code) { + $this->assertResponse($code); + } return $this->assertIdentical($expected, $this->responseBody, $message ? $message : strtr('Response body @expected (expected) is equal to @response (actual).', array('@expected' => var_export($expected, TRUE), '@response' => var_export($this->responseBody, TRUE))), $group); } + } diff --git a/core/modules/rest/src/Tests/UserLoginTest.php b/core/modules/rest/src/Tests/UserLoginTest.php index 443d151..ffe00a3 100644 --- a/core/modules/rest/src/Tests/UserLoginTest.php +++ b/core/modules/rest/src/Tests/UserLoginTest.php @@ -39,58 +39,58 @@ public function testLogin() { $name = $account->getUsername(); $pass = $account->pass_raw; - // Add registration permission to anonymous user. + // Add login permission to anonymous user. Role::load(RoleInterface::ANONYMOUS_ID) ->grantPermission('restful post user_login') ->save(); $payload = array(); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'No op found. Use: status, login, logout.'); + $this->assertResponseBody('400', '{"error":"No op found. Use: status, login, logout."}'); $payload = $this->getPayload('garbage'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Unsupported op garbage.'); + $this->assertResponseBody('400', '{"error":"Unsupported op garbage."}'); $payload = $this->getPayload('status'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('200', 'You are logged in.'); + $this->assertResponseBody('200', '"You are not logged in."'); $payload = $this->getPayload('login'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Missing credentials.'); + $this->assertResponseBody('400', '{"error":"Missing credentials."}'); $payload = $this->getPayload('login', $name); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Missing credentials.pass.'); + $this->assertResponseBody('400', '{"error":"Missing credentials.pass."}'); $payload = $this->getPayload('login', NULL, $pass); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Missing credentials.name.'); + $this->assertResponseBody('400', '{"error":"Missing credentials.name."}'); $payload = $this->getPayload('login', $name, 'garbage'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Sorry, unrecognized username or password.'); + $this->assertResponseBody('400', '{"error":"Sorry, unrecognized username or password."}'); $payload = $this->getPayload('login', 'garbage', $pass); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('400', 'Sorry, unrecognized username or password.'); + $this->assertResponseBody('400', '{"error":"Sorry, unrecognized username or password."}'); $payload = $this->getPayload('login', $name, $pass); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('200', "You are logged in as $name."); + $this->assertResponseBody('200', '"You are logged in as ' . $name . '."'); $payload = $this->getPayload('status'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('200', 'You are logged in.'); + $this->assertResponseBody('200', '"You are logged in."'); $payload = $this->getPayload('logout'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('200', 'You are logged out.'); + $this->assertResponseBody('200', '"You are logged out."'); $payload = $this->getPayload('status'); $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); - $this->assertResponse('200', 'You are logged out.'); + $this->assertResponseBody('200', '"You are not logged in."'); }