Skip to content

Commit

Permalink
Fix incorrect string values addition to POST request with files (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 27, 2024
1 parent 13513ec commit 5b4d235
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.
- Chg #30: Remove `TelegramRequestWithFilesInterface`.
- Bug #48: Fix incorrect string values addition to POST request with files in `PsrTelegramClient`.

## 0.1.0 June 10, 2024

Expand Down
5 changes: 4 additions & 1 deletion src/Client/PsrTelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ private function createPostRequest(TelegramRequestInterface $request): HttpReque
} else {
$streamBuilder = new MultipartStreamBuilder($this->streamFactory);
foreach ($data as $key => $value) {
$streamBuilder->addResource($key, json_encode($value, JSON_THROW_ON_ERROR));
$streamBuilder->addResource(
$key,
is_string($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR)
);
}
foreach ($files as $key => $file) {
$streamBuilder->addResource(
Expand Down
8 changes: 7 additions & 1 deletion tests/Client/PsrTelegramClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testPostWithDataAndFiles(): void
/** @var Request $request */
$requestHeaders = $request->getHeaders();
$this->assertSame(['Content-Length', 'Content-Type'], array_keys($requestHeaders));
$this->assertSame($requestHeaders['Content-Length'], ['287']);
$this->assertSame($requestHeaders['Content-Length'], ['390']);
$this->assertSame([0], array_keys($requestHeaders['Content-Type']));
$this->assertSame(
1,
Expand All @@ -156,6 +156,11 @@ public function testPostWithDataAndFiles(): void
123
--$matches[1]
Content-Disposition: form-data; name="caption"
Content-Length: 5
hello
--$matches[1]
Content-Disposition: form-data; name="photo"; filename="face.png"
Content-Length: 14
Content-Type: image/png
Expand Down Expand Up @@ -190,6 +195,7 @@ public function testPostWithDataAndFiles(): void
'sendPhoto',
[
'chat_id' => 123,
'caption' => 'hello',
'photo' => new InputFile(
(new StreamFactory())->createStream('test-file-body'),
'face.png',
Expand Down

0 comments on commit 5b4d235

Please sign in to comment.