Skip to content

Commit

Permalink
Add a check for a seekable response body and rewind in `PsrTelegramCl…
Browse files Browse the repository at this point in the history
…ient` (#128)
  • Loading branch information
vjik authored Dec 18, 2024
1 parent b9996bc commit d998207
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Telegram Bot API for PHP Change Log

## 0.4.7.1 December 18, 2024

- Bug #128: Add a check for a seekable response body and rewind in `PsrTelegramClient`.

## 0.4.7 December 04, 2024

- New #126: Add `nanostarAmount` field to `StarTransaction` type.
Expand Down
7 changes: 6 additions & 1 deletion src/Client/PsrTelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public function send(TelegramRequestInterface $request): TelegramResponse
},
);

$body = $httpResponse->getBody();
if ($body->isSeekable()) {
$body->rewind();
}

return new TelegramResponse(
$httpResponse->getStatusCode(),
$httpResponse->getBody()->getContents(),
$body->getContents(),
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Client/PsrTelegramClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use HttpSoft\Message\Request;
use HttpSoft\Message\Response;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -206,4 +207,29 @@ public function testPostWithDataAndFiles(): void

$this->assertSame(201, $response->statusCode);
}

public function testRewind(): void
{
$streamFactory = new StreamFactory();

$httpResponse = new Response(201, body: $streamFactory->createStream('hello'));
$httpResponse->getBody()->getContents();

$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('sendRequest')->willReturn($httpResponse);

$httpRequestFactory = $this->createMock(RequestFactoryInterface::class);

$client = new PsrTelegramClient(
'04062024',
$httpClient,
$httpRequestFactory,
$streamFactory,
);

$response = $client->send(new TelegramRequest(HttpMethod::GET, 'getMe'));

$this->assertSame(201, $response->statusCode);
$this->assertSame('hello', $response->body);
}
}

0 comments on commit d998207

Please sign in to comment.