From d2dd92be77b919248c261c626897a1b8003726a3 Mon Sep 17 00:00:00 2001 From: Oleksii Zhurbytskyi Date: Sat, 16 May 2020 16:01:37 +0200 Subject: [PATCH] [BrowserKit] Raw body with custom Content-Type header --- src/Symfony/Component/BrowserKit/HttpBrowser.php | 11 +++++++++-- .../Component/BrowserKit/Tests/HttpBrowserTest.php | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/HttpBrowser.php b/src/Symfony/Component/BrowserKit/HttpBrowser.php index 9c2b3fcf57e5..ed6c10028145 100644 --- a/src/Symfony/Component/BrowserKit/HttpBrowser.php +++ b/src/Symfony/Component/BrowserKit/HttpBrowser.php @@ -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), @@ -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 ['', []]; @@ -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()]; diff --git a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php index 77586f44e774..1397d9b10c38 100644 --- a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php @@ -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()