public function StyleSerializerTest::testResponseFormatConfiguration in Drupal 9
Same name and namespace in other branches
- 8 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testResponseFormatConfiguration()
Tests the response format configuration.
File
- core/
modules/ rest/ tests/ src/ Functional/ Views/ StyleSerializerTest.php, line 386
Class
- StyleSerializerTest
- Tests the serializer style plugin.
Namespace
Drupal\Tests\rest\Functional\ViewsCode
public function testResponseFormatConfiguration() {
$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);
}