Skip to content

Commit 38a6860

Browse files
authored
enchance error builder: output body only for json, limit body to 5 Kb (#405)
fixes #403 supersedes #404
1 parent 8f2ba03 commit 38a6860

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

src/Common/Error/Builder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class Builder
2121
{
22+
public const MAX_BODY_LENGTH = 5000;
23+
2224
/**
2325
* The default domain to use for further link documentation.
2426
*
@@ -96,8 +98,13 @@ public function str(MessageInterface $message, int $verbosity = 0): string
9698
return $msg;
9799
}
98100

99-
if (ini_get('memory_limit') < 0 || $message->getBody()->getSize() < ini_get('memory_limit')) {
100-
$msg .= "\r\n\r\n".$message->getBody();
101+
$contentType = strtolower($message->getHeaderLine('content-type'));
102+
if (false !== strpos($contentType, 'application/json')) {
103+
$body = $message->getBody()->read(self::MAX_BODY_LENGTH);
104+
$msg .= "\r\n\r\n".$body;
105+
if ('' !== $message->getBody()->read(1)) {
106+
$msg .= '...';
107+
}
101108
}
102109

103110
return trim($msg);

tests/sample/Compute/v2/ServerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ public function testSuspend()
374374
$server->waitUntil('SUSPENDED');
375375
$this->assertEquals('SUSPENDED', $server->status);
376376

377+
// wait for the server to be fully suspended
378+
sleep(5);
379+
377380
return $server;
378381
}
379382

tests/unit/Common/Error/BuilderTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GuzzleHttp\ClientInterface;
66
use GuzzleHttp\Exception\ClientException;
7+
use GuzzleHttp\Psr7\NoSeekStream;
8+
use GuzzleHttp\Psr7\PumpStream;
79
use GuzzleHttp\Psr7\Request;
810
use GuzzleHttp\Psr7\Response;
911
use GuzzleHttp\Psr7\Utils;
@@ -75,6 +77,85 @@ public function verbosityProvider(): array
7577
];
7678
}
7779

80+
public function test_it_outputs_body_for_json()
81+
{
82+
$value = 'foobar';
83+
84+
$request = new Request(
85+
'POST',
86+
'/servers',
87+
['Content-Type' => 'application/json'],
88+
json_encode(['foo' => $value])
89+
);
90+
91+
$str = $this->builder->str($request, 2);
92+
$this->assertStringContainsString($value, $str);
93+
}
94+
95+
public function test_it_skips_body_for_low_verbosity()
96+
{
97+
$value = 'foobar';
98+
99+
$request = new Request(
100+
'POST',
101+
'/servers',
102+
['Content-Type' => 'application/json'],
103+
json_encode(['foo' => $value])
104+
);
105+
106+
$str = $this->builder->str($request, 1);
107+
$this->assertStringNotContainsString($value, $str);
108+
}
109+
110+
public function test_it_cuts_big_body_for_json()
111+
{
112+
$value = str_repeat('A', Builder::MAX_BODY_LENGTH);
113+
114+
$request = new Request(
115+
'POST',
116+
'/servers',
117+
['Content-Type' => 'application/json'],
118+
json_encode(['foo' => $value])
119+
);
120+
121+
$str = $this->builder->str($request, 2);
122+
$this->assertStringNotContainsString($value, $str);
123+
$this->assertStringContainsString('AAAAAA...', $str);
124+
}
125+
126+
public function test_it_did_not_read_full_body_for_json()
127+
{
128+
$value = str_repeat('A', Builder::MAX_BODY_LENGTH + 1);
129+
130+
$request = new Request(
131+
'POST',
132+
'/servers',
133+
['Content-Type' => 'application/json'],
134+
new PumpStream(function ($size) {
135+
return str_repeat('A', $size);
136+
})
137+
);
138+
139+
$str = $this->builder->str($request, 2);
140+
$this->assertStringNotContainsString($value, $str);
141+
$this->assertStringContainsString('AAAAAA...', $str);
142+
}
143+
144+
public function test_it_skips_body_for_binary()
145+
{
146+
$value = 'foobar';
147+
148+
$request = new Request(
149+
'POST',
150+
'/servers',
151+
['Content-Type' => 'binary/octet-stream'],
152+
$value
153+
);
154+
155+
$str = $this->builder->str($request, 2);
156+
$this->assertStringNotContainsString($value, $str);
157+
}
158+
78159
public function test_it_builds_user_input_errors()
79160
{
80161
$expected = 'A well-formed string';

0 commit comments

Comments
 (0)