You are here

public function StyleSerializerTest::testResponseFormatConfiguration in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Tests/Views/StyleSerializerTest.php \Drupal\rest\Tests\Views\StyleSerializerTest::testResponseFormatConfiguration()

Tests the response format configuration.

File

core/modules/rest/src/Tests/Views/StyleSerializerTest.php, line 297
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testResponseFormatConfiguration() {
  $this
    ->drupalLogin($this->adminUser);
  $style_options = 'admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/style_options';

  // Select only 'xml' as an accepted format.
  $this
    ->drupalPostForm($style_options, array(
    'style_options[formats][xml]' => 'xml',
  ), t('Apply'));
  $this
    ->drupalPostForm(NULL, array(), t('Save'));

  // Should return a 406.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(406, 'A 406 response was returned when JSON was requested.');

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'xml');
  $this
    ->assertHeader('content-type', 'text/xml; charset=UTF-8');
  $this
    ->assertResponse(200, 'A 200 response was returned when XML was requested.');

  // Add 'json' as an accepted format, so we have multiple.
  $this
    ->drupalPostForm($style_options, array(
    'style_options[formats][json]' => 'json',
  ), t('Apply'));
  $this
    ->drupalPostForm(NULL, array(), t('Save'));

  // Should return a 200.
  // @todo This should be fixed when we have better content negotiation.
  $this
    ->drupalGet('test/serialize/field');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when any format was requested.');

  // Should return a 200. Emulates a sample Firefox header.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  ));
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when a browser accept header was requested.');

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when JSON was requested.');
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['content-type'], 'application/json', 'The header Content-type is correct.');

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'xml');
  $this
    ->assertHeader('content-type', 'text/xml; charset=UTF-8');
  $this
    ->assertResponse(200, 'A 200 response was returned when XML was requested');
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertTrue(strpos($headers['content-type'], 'text/xml') !== FALSE, 'The header Content-type is correct.');

  // Should return a 406.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'html');

  // We want to show the first format by default, see
  // \Drupal\rest\Plugin\views\style\Serializer::render.
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when HTML was requested.');

  // Now configure now format, so all of them should be allowed.
  $this
    ->drupalPostForm($style_options, array(
    'style_options[formats][json]' => '0',
    'style_options[formats][xml]' => '0',
  ), t('Apply'));

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when JSON was requested.');

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'xml');
  $this
    ->assertHeader('content-type', 'text/xml; charset=UTF-8');
  $this
    ->assertResponse(200, 'A 200 response was returned when XML was requested');

  // Should return a 200.
  $this
    ->drupalGetWithFormat('test/serialize/field', 'html');

  // We want to show the first format by default, see
  // \Drupal\rest\Plugin\views\style\Serializer::render.
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertResponse(200, 'A 200 response was returned when HTML was requested.');
}