function StyleSerializerEntityTest::testResponseFormatConfiguration

Same name and namespace in other branches
  1. 11.x core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerEntityTest::testResponseFormatConfiguration()

Tests the response format configuration.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php, line 309

Class

StyleSerializerEntityTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testResponseFormatConfiguration() : void {
  $this->drupalLogin($this->adminUser);
  $style_options = 'admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/style_options';
  // Ensure a request with no format returns 406 Not Acceptable.
  $this->drupalGet('test/serialize/field');
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/html; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(406);
  // Select only 'xml' as an accepted format.
  $this->drupalGet($style_options);
  $this->submitForm([
    'style_options[formats][xml]' => 'xml',
  ], 'Apply');
  $this->submitForm([], 'Save');
  // Ensure a request for JSON returns 406 Not Acceptable.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'json',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $this->assertSession()
    ->statusCodeEquals(406);
  // Ensure a request for XML returns 200 OK.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'xml',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/xml; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Add 'json' as an accepted format, so we have multiple.
  $this->drupalGet($style_options);
  $this->submitForm([
    'style_options[formats][json]' => 'json',
  ], 'Apply');
  $this->submitForm([], 'Save');
  // Should return a 406. Emulates a sample Firefox header.
  $this->drupalGet('test/serialize/field', [], [
    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/html; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(406);
  // Ensure a request for HTML returns 406 Not Acceptable.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'html',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/html; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(406);
  // Ensure a request for JSON returns 200 OK.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'json',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Ensure a request XML returns 200 OK.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'xml',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/xml; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Now configure no format, so both serialization formats should be allowed.
  $this->drupalGet($style_options);
  $this->submitForm([
    'style_options[formats][json]' => '0',
    'style_options[formats][xml]' => '0',
  ], 'Apply');
  // Ensure a request for JSON returns 200 OK.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'json',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'application/json');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Ensure a request for XML returns 200 OK.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'xml',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/xml; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Should return a 406 for HTML still.
  $this->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'html',
    ],
  ]);
  $this->assertSession()
    ->responseHeaderEquals('content-type', 'text/html; charset=UTF-8');
  $this->assertSession()
    ->statusCodeEquals(406);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.