Skip to content

Commit 5a550cc

Browse files
authored
Add the ability to parse form_params as an array (#27)
* add the ability to parse also form params
1 parent f61cc92 commit 5a550cc

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Handler/MultiRecordArrayHandler.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,20 @@ private function formatBody(MessageInterface $message, array $options)
170170
}
171171

172172
$body = $stream->__toString();
173-
$isJson = preg_grep('/application\/[\w\.\+]*(json)/', $message->getHeader('Content-Type'));
173+
$contentType = $message->getHeader('Content-Type');
174+
175+
$isJson = preg_grep('/application\/[\w\.\+]*(json)/', $contentType);
174176
if (!empty($isJson)) {
175-
$body = json_decode($body, true);
177+
$result = json_decode($body, true);
178+
$stream->rewind();
179+
return $result;
180+
}
181+
182+
$isForm = preg_grep('/application\/x-www-form-urlencoded/', $contentType);
183+
if (!empty($isForm)) {
184+
$result = \GuzzleHttp\Psr7\parse_query($body);
185+
$stream->rewind();
186+
return $result;
176187
}
177188

178189
$stream->rewind();

tests/Handler/MultiRecordArrayHandlerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ public function logTransactionWithJsonResponse()
8080
);
8181
}
8282

83+
/**
84+
* @test
85+
*/
86+
public function logTransactionWithFormRequest()
87+
{
88+
$this
89+
->appendResponse(200)
90+
->createClient([
91+
RequestOptions::FORM_PARAMS => [
92+
'one_param' => 'test',
93+
'second_param' => 'test2',
94+
],
95+
])
96+
->get('/');
97+
98+
$this->assertCount(2, $this->logger->records);
99+
$this->assertSame(LogLevel::DEBUG, $this->logger->records[0]['level']);
100+
$this->assertSame('Guzzle HTTP request', $this->logger->records[0]['message']);
101+
$this->assertSame(
102+
[
103+
'one_param' => 'test',
104+
'second_param' => 'test2',
105+
],
106+
$this->logger->records[0]['context']['request']['body']
107+
);
108+
$this->assertSame(LogLevel::DEBUG, $this->logger->records[1]['level']);
109+
$this->assertSame('Guzzle HTTP response', $this->logger->records[1]['message']);
110+
$this->assertArrayNotHasKey('body', $this->logger->records[1]['context']['response']);
111+
}
112+
83113
/**
84114
* @test
85115
*/

0 commit comments

Comments
 (0)