function FileUploadTest::fileRequest

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

Performs a file upload request. Wraps the Guzzle HTTP client.

Parameters

\Drupal\Core\Url $url: URL to request.

string $file_contents: The file contents to send as the request body.

array $headers: Additional headers to send with the request. Defaults will be added for Content-Type and Content-Disposition. In order to remove the defaults set the header value to FALSE.

Return value

\Psr\Http\Message\ResponseInterface The received response.

See also

\GuzzleHttp\ClientInterface::request()

11 calls to FileUploadTest::fileRequest()
FileUploadTest::testFileUploadInvalidFileType in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with an invalid file type.
FileUploadTest::testFileUploadLargerFileSize in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with a file size larger than allowed.
FileUploadTest::testFileUploadMaliciousExtension in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route with malicious extensions.
FileUploadTest::testFileUploadNoConfiguration in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route no configuration.
FileUploadTest::testFileUploadStrippedFilePath in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload route with any path prefixes being stripped.

... See full list

File

core/modules/jsonapi/tests/src/Functional/FileUploadTest.php, line 827

Class

FileUploadTest
Tests binary data file upload route.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function fileRequest(Url $url, $file_contents, array $headers = []) {
    $request_options = [];
    $headers = $headers + [
        // Set the required (and only accepted) content type for the request.
'Content-Type' => 'application/octet-stream',
        // Set the required Content-Disposition header for the file name.
'Content-Disposition' => 'file; filename="example.txt"',
        // Set the required JSON:API Accept header.
'Accept' => 'application/vnd.api+json',
    ];
    $request_options[RequestOptions::HEADERS] = array_filter($headers, function ($value) {
        return $value !== FALSE;
    });
    $request_options[RequestOptions::BODY] = $file_contents;
    $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
    return $this->request('POST', $url, $request_options);
}

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