function FileUploadResourceTestBase::testPostFileUploadInvalidHeaders

Same name in other branches
  1. 9 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::testPostFileUploadInvalidHeaders()
  2. 8.9.x core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::testPostFileUploadInvalidHeaders()
  3. 10 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::testPostFileUploadInvalidHeaders()

Tests using the file upload POST route with invalid headers.

File

core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php, line 260

Class

FileUploadResourceTestBase
Tests binary data file upload route.

Namespace

Drupal\Tests\rest\Functional

Code

public function testPostFileUploadInvalidHeaders() : void {
    $this->initAuthentication();
    $this->provisionResource([
        static::$format,
    ], static::$auth ? [
        static::$auth,
    ] : [], [
        'POST',
    ]);
    $this->setUpAuthorization('POST');
    $uri = Url::fromUri('base:' . static::$postUri);
    // The wrong content type header should return a 415 code.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Type' => static::$mimeType,
    ]);
    $this->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response);
    // An empty Content-Disposition header should return a 400.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Disposition' => FALSE,
    ]);
    $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided.', $response);
    // An empty filename with a context in the Content-Disposition header should
    // return a 400.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Disposition' => 'file; filename=""',
    ]);
    $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $response);
    // An empty filename without a context in the Content-Disposition header
    // should return a 400.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Disposition' => 'filename=""',
    ]);
    $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $response);
    // An invalid key-value pair in the Content-Disposition header should return
    // a 400.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Disposition' => 'not_a_filename="example.txt"',
    ]);
    $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $response);
    // Using filename* extended format is not currently supported.
    $response = $this->fileRequest($uri, $this->testFileData, [
        'Content-Disposition' => 'filename*="UTF-8 \' \' example.txt"',
    ]);
    $this->assertResourceErrorResponse(400, 'The extended "filename*" format is currently not supported in the "Content-Disposition" header.', $response);
}

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