function StyleSerializerTest::testResponseFormatConfiguration
Same name in other branches
- 9 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->drupalPostForm($style_options, [
'style_options[formats][xml]' => 'xml',
], t('Apply'));
$this->drupalPostForm(NULL, [], t('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->drupalPostForm($style_options, [
'style_options[formats][json]' => 'json',
], t('Apply'));
$this->drupalPostForm(NULL, [], t('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->drupalPostForm($style_options, [
'style_options[formats][json]' => '0',
'style_options[formats][xml]' => '0',
], t('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.