public function StyleSerializerTest::testReponseFormatConfiguration

Tests the response format configuration.

File

drupal/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php, line 133
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testReponseFormatConfiguration() {
  $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
    ->drupalPost($style_options, array(
    'style_options[formats][xml]' => 'xml',
  ), t('Apply'));
  $this
    ->drupalPost(NULL, array(), t('Save'));

  // Should return a 406.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: application/json',
  ));
  $this
    ->assertResponse(406, 'A 406 response was returned when JSON was requested.');

  // Should return a 200.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: application/xml',
  ));
  $this
    ->assertResponse(200, 'A 200 response was returned when XML was requested.');

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

  // Should return a 200.
  // @todo This should be fixed when we have better content negotiation.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: */*',
  ));
  $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
    ->assertResponse(200, 'A 200 response was returned when a browser accept header was requested.');

  // Should return a 200.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: application/json',
  ));
  $this
    ->assertResponse(200, 'A 200 response was returned when JSON was requested.');

  // Should return a 200.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: application/xml',
  ));
  $this
    ->assertResponse(200, 'A 200 response was returned when XML was requested');

  // Should return a 406.
  $this
    ->drupalGet('test/serialize/field', array(), array(
    'Accept: application/html',
  ));
  $this
    ->assertResponse(406, 'A 406 response was returned when HTML was requested.');
}