Skip to content

Commit d68e33c

Browse files
committed
fix(http-client-mock): add content-length to body
1 parent 5638731 commit d68e33c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/HttpClientMock/MockRequestBuilderFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ private function processBody(
5757
array $headers,
5858
): void {
5959
$contentType = (string) $mockRequestBuilder->getHeader('Content-Type');
60+
$contentLength = $mockRequestBuilder->getHeader('Content-Length') ?? 0;
6061

6162
// application/json; charset=utf-8
6263
if (strpos($contentType, 'application/json') === 0) {
6364
if (is_string($body)) {
6465
$mockRequestBuilder->json(json_decode($body, true));
6566
} elseif (is_callable($body)) {
66-
$mockRequestBuilder->json(json_decode((string) $body(), true));
67+
$mockRequestBuilder->json(json_decode((string) $body((int) $contentLength), true));
6768
} elseif (is_array($body)) {
6869
$mockRequestBuilder->json($body);
6970
} else {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\Tests\HttpClientMock;
6+
7+
use Brainbits\FunctionalTestHelpers\HttpClientMock\MockRequestBuilderFactory;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(MockRequestBuilderFactory::class)]
12+
final class MockRequestBuilderFactoryTest extends TestCase
13+
{
14+
public function testBuildsRequestWithoutBody(): void
15+
{
16+
$mockRequestBuilderFactory = new MockRequestBuilderFactory();
17+
18+
$options = [
19+
'headers' => ['Content-Type: application/json'],
20+
'json' => ['foo' => 'bar'],
21+
];
22+
$request = ($mockRequestBuilderFactory)('POST', 'https://service.com', $options);
23+
24+
self::assertSame('POST', $request->getMethod());
25+
self::assertSame('https://service.com', $request->getUri());
26+
self::assertSame(['foo' => 'bar'], $request->getJson());
27+
}
28+
}

0 commit comments

Comments
 (0)