diff --git a/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/DBLogResource.php b/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/DBLogResource.php index 7ec6b3e..5a1fe31 100644 --- a/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/DBLogResource.php +++ b/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/DBLogResource.php @@ -47,11 +47,6 @@ public function routes() { */ public function get($id = NULL) { if ($id) { - $result = db_select('watchdog', 'w') - ->condition('wid', $id) - ->fields('w') - ->execute() - ->fetchAll(); $result = db_query("SELECT * FROM {watchdog} WHERE wid = :wid", array(':wid' => $id)) ->fetchObject(); if (!empty($result)) { diff --git a/core/modules/rest/lib/Drupal/rest/RequestHandler.php b/core/modules/rest/lib/Drupal/rest/RequestHandler.php index a45ebf8..a374d14 100644 --- a/core/modules/rest/lib/Drupal/rest/RequestHandler.php +++ b/core/modules/rest/lib/Drupal/rest/RequestHandler.php @@ -55,7 +55,7 @@ public function handle($plugin, Request $request, $id = NULL) { // Accept headers. See http://drupal.org/node/1833440 $output = $serializer->serialize($data, 'drupal_jsonld'); $response->setContent($output); - $response->headers->set('Content-Type', 'application/ld+json'); + $response->headers->set('Content-Type', 'application/vnd.drupal.ld+json'); } return $response; } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/DBLogTest.php b/core/modules/rest/lib/Drupal/rest/Tests/DBLogTest.php index 44922c7..5002397 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/DBLogTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/DBLogTest.php @@ -50,15 +50,16 @@ public function testWatchdog() { $account = $this->drupalCreateUser(array('restful get dblog')); $this->drupalLogin($account); - $response = $this->httpRequest("dblog/$id", 'GET'); + $response = $this->httpRequest("dblog/$id", 'GET', NULL, 'application/json'); $this->assertResponse(200); + $this->assertHeader('Content-Type', 'application/json'); $log = drupal_json_decode($response); $this->assertEqual($log['wid'], $id, 'Log ID is correct.'); $this->assertEqual($log['type'], 'rest_test', 'Type of log message is correct.'); $this->assertEqual($log['message'], 'Test message', 'Log message text is correct.'); // Request an unknown log entry. - $response = $this->httpRequest("dblog/9999", 'GET'); + $response = $this->httpRequest("dblog/9999", 'GET', NULL, 'application/json'); $this->assertResponse(404); $this->assertEqual($response, 'Not Found', 'Response message is correct.'); } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index eed04fb..acf4802 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -15,6 +15,13 @@ abstract class RESTTestBase extends WebTestBase { /** + * Stores HTTP response headers from the last HTTP request. + * + * @var array + */ + protected $responseHeaders; + + /** * Helper function to issue a HTTP request with simpletest's cURL. * * @param string $url @@ -31,47 +38,62 @@ protected function httpRequest($url, $method, $body = NULL, $format = 'applicati case 'GET': // Set query if there are additional GET parameters. $options = isset($body) ? array('absolute' => TRUE, 'query' => $body) : array('absolute' => TRUE); - $response = $this->curlExec(array( + $curl_options = array( CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($url, $options), - CURLOPT_NOBODY => FALSE) + CURLOPT_NOBODY => FALSE ); break; + case 'POST': - $response = $this->curlExec(array( + $curl_options = array( CURLOPT_HTTPGET => FALSE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $body, CURLOPT_URL => url($url, array('absolute' => TRUE)), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array('Content-Type: ' . $format), - )); + ); break; + case 'PUT': - $response = $this->curlExec(array( + $curl_options = array( CURLOPT_HTTPGET => FALSE, CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $body, CURLOPT_URL => url($url, array('absolute' => TRUE)), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => array('Content-Type: ' . $format), - )); + ); break; + case 'DELETE': - $response = $this->curlExec(array( + $curl_options = array( CURLOPT_HTTPGET => FALSE, CURLOPT_CUSTOMREQUEST => 'DELETE', CURLOPT_URL => url($url, array('absolute' => TRUE)), CURLOPT_NOBODY => FALSE, - )); + ); break; } + // Include all HTTP headers in the response. + $curl_options[CURLOPT_HEADER] = TRUE; + + $response = $this->curlExec($curl_options); + + list($header, $body) = explode("\r\n\r\n", $response, 2); + $header_lines = explode("\r\n", $header); + foreach ($header_lines as $line) { + $parts = explode(':', $line, 2); + $this->responseHeaders[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : ''; + } $this->verbose($method . ' request to: ' . $url . '
Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . - '
Response: ' . $response); + '
Response headers: ' . $header . + '
Response body: ' . $body); - return $response; + return $body; } /** @@ -125,4 +147,27 @@ protected function enableService($resource_type) { // permissions get picked up. drupal_static_reset('checkPermissions'); } + + /** + * Check if a HTTP response header exists and has the expected value. + * + * @param string $header + * The header key, example: Content-Type + * @param string $value + * The header value. + * @param string $message + * (optional) A message to display with the assertion. + * @param string $group + * (optional) The group this message is in, which is displayed in a column + * in test output. Use 'Debug' to indicate this is debugging output. Do not + * translate this string. Defaults to 'Other'; most tests do not override + * this default. + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertHeader($header, $value, $message = '', $group = 'Browser') { + $match = isset($this->responseHeaders[$header]) && $this->responseHeaders[$header] == $value; + return $this->assertTrue($match, $message ? $message : 'HTTP response header ' . $header . ' with value ' . $value . ' found.', $group); + } } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php index 37a9e3a..28ffcea 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php @@ -51,8 +51,9 @@ public function testRead() { $entity = $this->entityCreate($entity_type); $entity->save(); // Read it over the web API. - $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, 'application/ld+json'); + $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, 'application/vnd.drupal.ld+json'); $this->assertResponse('200', 'HTTP response code is correct.'); + $this->assertHeader('Content-Type', 'application/vnd.drupal.ld+json'); $data = drupal_json_decode($response); // Only assert one example property here, other properties should be // checked in serialization tests. @@ -65,7 +66,7 @@ public function testRead() { // Try to read an entity without proper permissions. $this->drupalLogout(); - $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, 'application/ld+json'); + $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, 'application/vnd.drupal.ld+json'); $this->assertResponse(403); $this->assertNull(drupal_json_decode($response), 'No valid JSON found.'); } @@ -75,7 +76,7 @@ public function testRead() { // options. unset($this->curlHandle); $this->drupalLogin($account); - $response = $this->httpRequest('entity/user/' . $account->id(), 'GET', NULL, 'application/ld+json'); + $response = $this->httpRequest('entity/user/' . $account->id(), 'GET', NULL, 'application/vnd.drupal.ld+json'); $this->assertResponse(404); $this->assertNull(drupal_json_decode($response), 'No valid JSON found.'); }