Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d7c696

Browse files
committed
bug #36839 [BrowserKit] Raw body with custom Content-Type header (azhurb)
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
2 parents 2e46c63 + d2dd92b commit 6d7c696

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Symfony/Component/BrowserKit/HttpBrowser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ public function __construct(HttpClientInterface $client = null, History $history
3939
parent::__construct([], $history, $cookieJar);
4040
}
4141

42+
/**
43+
* @param Request $request
44+
*/
4245
protected function doRequest($request): Response
4346
{
4447
$headers = $this->getHeaders($request);
45-
[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request);
48+
[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request, $headers);
4649

4750
$response = $this->client->request($request->getMethod(), $request->getUri(), [
4851
'headers' => array_merge($headers, $extraHeaders),
@@ -56,7 +59,7 @@ protected function doRequest($request): Response
5659
/**
5760
* @return array [$body, $headers]
5861
*/
59-
private function getBodyAndExtraHeaders(Request $request): array
62+
private function getBodyAndExtraHeaders(Request $request, array $headers): array
6063
{
6164
if (\in_array($request->getMethod(), ['GET', 'HEAD'])) {
6265
return ['', []];
@@ -67,6 +70,10 @@ private function getBodyAndExtraHeaders(Request $request): array
6770
}
6871

6972
if (null !== $content = $request->getContent()) {
73+
if (isset($headers['content-type'])) {
74+
return [$content, []];
75+
}
76+
7077
$part = new TextPart($content, 'utf-8', 'plain', '8bit');
7178

7279
return [$part->bodyToString(), $part->getPreparedHeaders()->toArray()];

src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function validContentTypes()
5959
['POST', 'http://example.com/', [], [], [], 'content'],
6060
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['Content-Type: text/plain; charset=utf-8', 'Content-Transfer-Encoding: 8bit'], 'body' => 'content', 'max_redirects' => 0]],
6161
];
62+
yield 'POST JSON' => [
63+
['POST', 'http://example.com/', [], [], ['CONTENT_TYPE' => 'application/json'], '["content"]'],
64+
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['content-type' => 'application/json'], 'body' => '["content"]', 'max_redirects' => 0]],
65+
];
6266
}
6367

6468
public function testMultiPartRequestWithSingleFile()

0 commit comments

Comments
 (0)