Skip to content

Commit

Permalink
bug #36839 [BrowserKit] Raw body with custom Content-Type header (azh…
Browse files Browse the repository at this point in the history
…urb)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[BrowserKit] Raw body with custom Content-Type header

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| License       | MIT

Currently, if you try to send POST/PUT request with custom `Content-Type` header and specified body, the real request will contain `text/plain` content type.

Following code
```php
$client->request(
    'POST',
    '/url',
    [],
    [],
    [
        'CONTENT_TYPE' => 'application/json'
    ],
    '{"foo":"bar"}'
);
```
produces next request
```
POST /
Content-Type: text/plain; charset=utf-8

{"foo":"bar"}
```

With this fix, the request will be
```
POST /
Content-Type: application/json

{"foo":"bar"}
```

Commits
-------

d2dd92b [BrowserKit] Raw body with custom Content-Type header
  • Loading branch information
fabpot committed May 22, 2020
2 parents 2e46c63 + d2dd92b commit 6d7c696
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Symfony/Component/BrowserKit/HttpBrowser.php
Expand Up @@ -39,10 +39,13 @@ public function __construct(HttpClientInterface $client = null, History $history
parent::__construct([], $history, $cookieJar);
}

/**
* @param Request $request
*/
protected function doRequest($request): Response
{
$headers = $this->getHeaders($request);
[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request);
[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request, $headers);

$response = $this->client->request($request->getMethod(), $request->getUri(), [
'headers' => array_merge($headers, $extraHeaders),
Expand All @@ -56,7 +59,7 @@ protected function doRequest($request): Response
/**
* @return array [$body, $headers]
*/
private function getBodyAndExtraHeaders(Request $request): array
private function getBodyAndExtraHeaders(Request $request, array $headers): array
{
if (\in_array($request->getMethod(), ['GET', 'HEAD'])) {
return ['', []];
Expand All @@ -67,6 +70,10 @@ private function getBodyAndExtraHeaders(Request $request): array
}

if (null !== $content = $request->getContent()) {
if (isset($headers['content-type'])) {
return [$content, []];
}

$part = new TextPart($content, 'utf-8', 'plain', '8bit');

return [$part->bodyToString(), $part->getPreparedHeaders()->toArray()];
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php
Expand Up @@ -59,6 +59,10 @@ public function validContentTypes()
['POST', 'http://example.com/', [], [], [], 'content'],
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['Content-Type: text/plain; charset=utf-8', 'Content-Transfer-Encoding: 8bit'], 'body' => 'content', 'max_redirects' => 0]],
];
yield 'POST JSON' => [
['POST', 'http://example.com/', [], [], ['CONTENT_TYPE' => 'application/json'], '["content"]'],
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['content-type' => 'application/json'], 'body' => '["content"]', 'max_redirects' => 0]],
];
}

public function testMultiPartRequestWithSingleFile()
Expand Down

0 comments on commit 6d7c696

Please sign in to comment.