Skip to content

Commit f199f49

Browse files
daFishpl-github
authored andcommitted
fix(http-client-mock): add content-length to body
1 parent 5638731 commit f199f49

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"phpstan/phpstan-phpunit": "^1.0",
2727
"phpstan/phpstan-symfony": "^1.0",
2828
"phpunit/phpunit": "^11.4",
29+
"psr/http-message": "^2.0",
2930
"riverline/multipart-parser": "^2.1",
3031
"slam/phpstan-extensions": "^6.0",
3132
"squizlabs/php_codesniffer": "^3.10",

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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
use Psr\Http\Message\StreamInterface;
11+
12+
#[CoversClass(MockRequestBuilderFactory::class)]
13+
final class MockRequestBuilderFactoryTest extends TestCase
14+
{
15+
private readonly MockRequestBuilderFactory $mockRequestBuilderFactory;
16+
17+
public function setUp(): void
18+
{
19+
$this->mockRequestBuilderFactory = new MockRequestBuilderFactory();
20+
}
21+
22+
public function testBuildsRequestWithoutBody(): void
23+
{
24+
$options = [
25+
'headers' => ['Content-Type: application/json'],
26+
'json' => ['foo' => 'bar'],
27+
];
28+
$request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options);
29+
30+
self::assertSame('POST', $request->getMethod());
31+
self::assertSame('https://service.com', $request->getUri());
32+
self::assertSame(['foo' => 'bar'], $request->getJson());
33+
}
34+
35+
public function testBuildsRequestWithJsonInBody(): void
36+
{
37+
$options = [
38+
'headers' => [
39+
'Content-Length: 1',
40+
'Content-Type: application/json',
41+
],
42+
'body' => '{"foo": "bar"}',
43+
];
44+
45+
$request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options);
46+
47+
self::assertSame('POST', $request->getMethod());
48+
self::assertSame('https://service.com', $request->getUri());
49+
self::assertSame(['foo' => 'bar'], $request->getJson());
50+
self::assertTrue($request->isJson());
51+
}
52+
53+
public function testBuildsRequestWithCallableInBody(): void
54+
{
55+
$body = $this->getMockBuilder(StreamInterface::class)->getMock();
56+
$body->method('read')
57+
->willReturn('{"foo": "bar"}');
58+
59+
$options = [
60+
'headers' => [
61+
'Content-Length: 1',
62+
'Content-Type: application/json',
63+
],
64+
'body' => static fn (int $size) => $body->read($size),
65+
];
66+
67+
$request = ($this->mockRequestBuilderFactory)('POST', 'https://service.com', $options);
68+
69+
self::assertSame('POST', $request->getMethod());
70+
self::assertSame('https://service.com', $request->getUri());
71+
self::assertSame(['foo' => 'bar'], $request->getJson());
72+
self::assertTrue($request->isJson());
73+
}
74+
}

0 commit comments

Comments
 (0)