Skip to content

Commit f9e5e32

Browse files
Muhammad FaragMuhammadFarag
authored andcommitted
Fix: Rewind body stream
1 parent c6e13fc commit f9e5e32

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/Clients/HttpResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function fromResponse(ResponseInterface $response): HttpResponse
4242
*/
4343
public function getDecodedBody(): array|string|null
4444
{
45+
$this->getBody()->rewind();
4546
$responseBody = $this->getBody()->getContents();
4647
return $responseBody
4748
? json_decode(json: $responseBody, associative: true, flags: JSON_THROW_ON_ERROR)

tests/HttpRequestMatcher.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
class HttpRequestMatcher extends Constraint
99
{
1010

11-
private string $actualBody;
1211

1312
/**
1413
* HttpRequestMatcher constructor.
@@ -35,11 +34,10 @@ protected function matches($other): bool
3534
if (!($other instanceof RequestInterface)) {
3635
return false;
3736
}
38-
$this->actualBody = $this->actualBody ?? $other->getBody()->getContents();
3937
return
4038
($other->getUri() == $this->url)
4139
&& ($other->getMethod() === $this->method)
42-
&& $this->matchBody()
40+
&& $this->matchBody($other)
4341
&& $this->matchHeadersWithoutUserAgent($other)
4442
&& $this->matchUserAgent($other);
4543
}
@@ -50,9 +48,10 @@ private function matchUserAgent(RequestInterface $request): bool
5048
return preg_match($this->userAgent, $request->getHeaderLine('user-agent')) != false;
5149
}
5250

53-
private function matchBody(): bool
51+
private function matchBody(RequestInterface $request): bool
5452
{
55-
return $this->body === $this->actualBody;
53+
$request->getBody()->rewind();
54+
return $this->body === $request->getBody()->getContents();
5655
}
5756

5857
private function matchHeadersWithoutUserAgent(RequestInterface $request): bool
@@ -86,8 +85,9 @@ protected function additionalFailureDescription($other): string
8685
if ($other->getMethod() !== $this->method) {
8786
$diff[] = $this->diffLine("Method", $this->method, $other->getMethod());
8887
}
89-
if (!$this->matchBody()) {
90-
$diff[] = $this->diffLine("Body", $this->body, $this->actualBody);
88+
if (!$this->matchBody($other)) {
89+
$other->getBody()->rewind();
90+
$diff[] = $this->diffLine("Body", $this->body, $other->getBody()->getContents());
9191
}
9292

9393
if (!$this->matchUserAgent($other)) {

0 commit comments

Comments
 (0)