Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BrowserKit] Raw body with custom Content-Type header #36839

Merged
merged 1 commit into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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