You are here

public function FileUploadTest::testPostFileUploadInvalidHeaders in JSON:API 8.2

Tests using the file upload POST route with invalid headers.

File

tests/src/Functional/FileUploadTest.php, line 382

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostFileUploadInvalidHeaders() {
  $this
    ->setUpAuthorization('POST');
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $uri = Url::fromUri('base:' . static::$postUri);

  // The wrong content type header should return a 415 code.
  $response = $this
    ->fileRequest($uri, $this->testFileData, [
    'Content-Type' => 'application/vnd.api+json',
  ]);
  $this
    ->assertSame(415, $response
    ->getStatusCode());

  // 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.', $uri, $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.', $uri, $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.', $uri, $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.', $uri, $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.', $uri, $response);
}