Skip to content

Commit 0986b2c

Browse files
committed
:octocat: MessageUtil::toJSON(): add query parameters as array
1 parent 47ff3b3 commit 0986b2c

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/MessageUtil.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,23 @@ public static function toString(MessageInterface $message, bool|null $appendBody
102102
*/
103103
public static function toJSON(MessageInterface $message, bool|null $appendBody = null):string{
104104
$appendBody ??= true;
105-
$msg = [];
105+
$msg = ['headers' => []];
106106

107107
if($message instanceof RequestInterface){
108+
$uri = $message->getUri();
109+
108110
$msg['request'] = [
109-
'url' => (string)$message->getUri(),
111+
'url' => (string)$uri,
112+
'params' => QueryUtil::parse($uri->getQuery()),
110113
'method' => $message->getMethod(),
111114
'target' => $message->getRequestTarget(),
112115
'http' => $message->getProtocolVersion(),
113116
];
117+
118+
if(!$message->hasHeader('host')){
119+
$msg['headers']['Host'] = $message->getUri()->getHost();
120+
}
121+
114122
}
115123
elseif($message instanceof ResponseInterface){
116124
$msg['response'] = [
@@ -120,12 +128,6 @@ public static function toJSON(MessageInterface $message, bool|null $appendBody =
120128
];
121129
}
122130

123-
$msg['headers'] = [];
124-
125-
if($message instanceof RequestInterface && !$message->hasHeader('host')){
126-
$msg['headers']['Host'] = $message->getUri()->getHost();
127-
}
128-
129131
foreach($message->getHeaders() as $name => $values){
130132
$msg['headers'][$name] = implode(', ', $values);
131133
}

tests/Client/EchoClientTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ class EchoClientTest extends HTTPClientTestAbstract{
2121
protected string $HTTP_CLIENT_FACTORY = EchoClientFactory::class;
2222

2323
public function testSendRequest():void{
24-
$url = 'https://httpbin.org/get';
24+
$url = 'https://httpbin.org/get?whatever=value';
2525
$response = $this->httpClient->sendRequest($this->requestFactory->createRequest('GET', $url));
2626
$json = MessageUtil::decodeJSON($response);
2727

2828
$this::assertSame($url, $json->request->url);
2929
$this::assertSame('GET', $json->request->method);
30+
$this::assertSame('value', $json->request->params->{'whatever'});
3031
$this::assertSame('httpbin.org', $json->headers->{'Host'});
3132
}
3233

0 commit comments

Comments
 (0)