Skip to content

Commit

Permalink
handle empty encoded responses (#1337)
Browse files Browse the repository at this point in the history
at least one otel backend product sends an empty response with a gzip encoding, which we fail to decode (because
an empty string is not valid). Work around this by not trying to decode an empty value.
  • Loading branch information
brettmc authored Jun 19, 2024
1 parent 6bf422c commit fc28032
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/SDK/Common/Export/Http/PsrUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public static function encode(string $value, array $encodings, ?array &$appliedE
*/
public static function decode(string $value, array $encodings): string
{
if ($value === '') {
return $value;
}

for ($i = count($encodings); --$i >= 0;) {
if (strcasecmp($encodings[$i], 'identity') === 0) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SDK/Common/Export/Http/PsrTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function test_send_decodes_response_body(): void

public function test_send_decode_unknown_encoding_returns_error(): void
{
$this->client->expects($this->once())->method('sendRequest')->willReturn(new Response(200, ['Content-Encoding' => 'invalid'], ''));
$this->client->expects($this->once())->method('sendRequest')->willReturn(new Response(200, ['Content-Encoding' => 'invalid'], 'foo'));
$transport = $this->factory->create('http://localhost', 'text/plain');

$response = $transport->send('');
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/SDK/Common/Export/Http/PsrUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ public function test_decode_stream_unknown_encoding(): void
{
$this->expectException(UnexpectedValueException::class);

PsrUtils::decode('', ['invalid']);
PsrUtils::decode('foo', ['invalid']);
}

public function test_decode_empty_value(): void
{
$this->assertSame('', PsrUtils::decode('', ['gzip']));
}

#[DataProvider('compressionProvider')]
Expand Down

0 comments on commit fc28032

Please sign in to comment.