From 0d61ec6f5e9cc9fb27b450a5ff1be9bd75f6506a Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Thu, 9 Nov 2023 19:53:09 +0100 Subject: [PATCH] Fix broken content-type for good --- docs/files.md | 4 ++-- src/Encoding/Mime/MultiPartEncoder.php | 2 +- tests/Unit/Encoding/Mime/MultiPartEncoderTest.php | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/files.md b/docs/files.md index fe1ea6a..c86d700 100644 --- a/docs/files.md +++ b/docs/files.md @@ -56,7 +56,7 @@ $transport = EncodedTransportFactory::create( ); $jsonData = $transport( - new Request('GET', '/some/file', [], new FormDataPart([ + new Request('POST', '/some/file', [], new FormDataPart([ 'name' => 'Jos bos', 'profile-pic' => DataPart::fromPath('/my-profile-pic.jpg') ])), @@ -83,6 +83,6 @@ $transport = EncodedTransportFactory::create( $multiPartStream = new MultipartStream($parts) $jsonData = $transport( - new Request('GET', '/some/file', [], $multiPartStream)), + new Request('POST', '/some/file', [], $multiPartStream)), ); ``` diff --git a/src/Encoding/Mime/MultiPartEncoder.php b/src/Encoding/Mime/MultiPartEncoder.php index 8526970..348b126 100644 --- a/src/Encoding/Mime/MultiPartEncoder.php +++ b/src/Encoding/Mime/MultiPartEncoder.php @@ -43,7 +43,7 @@ public function __invoke(RequestInterface $request, $data): RequestInterface return $request ->withAddedHeader( 'Content-Type', - string()->assert($data->getPreparedHeaders()->get('content-type')?->toString()) + string()->assert($data->getPreparedHeaders()->get('content-type')?->getBodyAsString()) ) ->withBody($this->streamFactory->createStream( $data->bodyToString() diff --git a/tests/Unit/Encoding/Mime/MultiPartEncoderTest.php b/tests/Unit/Encoding/Mime/MultiPartEncoderTest.php index f4d2d95..c618d20 100644 --- a/tests/Unit/Encoding/Mime/MultiPartEncoderTest.php +++ b/tests/Unit/Encoding/Mime/MultiPartEncoderTest.php @@ -27,7 +27,7 @@ public function it_can_encode_multi_part(): void $actual = $encoder($request, $data); - $expectedContentType = $data->getPreparedHeaders()->get('Content-Type')->toString(); + $expectedContentType = $data->getPreparedHeaders()->get('Content-Type')->getBodyAsString(); self::assertSame($request->getMethod(), $actual->getMethod()); self::assertSame($request->getUri(), $actual->getUri()); @@ -37,5 +37,7 @@ public function it_can_encode_multi_part(): void $actual->getHeader('Content-Type') ); self::assertStringContainsString('boundary', $expectedContentType); + self::assertStringContainsString('multipart/form-data', $expectedContentType); + self::assertStringNotContainsString('content-type:', mb_strtolower($expectedContentType)); } }