Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5cc8c2

Browse files
Victor Wellingjeskew
authored andcommitted
Fix for HEAD requests failing due to non-null message bodies
1 parent 643eb1a commit b5cc8c2

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/ElasticsearchPhpHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ private function createPsr7Request(array $ringPhpRequest)
8282
private function createRingRequest(RequestInterface $request)
8383
{
8484
$uri = $request->getUri();
85+
$body = (string) $request->getBody();
86+
87+
// RingPHP currently expects empty message bodies to be null:
88+
// https://github.com/guzzle/RingPHP/blob/4c8fe4c48a0fb7cc5e41ef529e43fecd6da4d539/src/Client/CurlFactory.php#L202
89+
if (empty($body)) {
90+
$body = null;
91+
}
92+
8593
$ringRequest = [
8694
'http_method' => $request->getMethod(),
8795
'scheme' => $uri->getScheme(),
8896
'uri' => $uri->getPath(),
89-
'body' => (string) $request->getBody(),
97+
'body' => $body,
9098
'headers' => $request->getHeaders(),
9199
];
92100
if ($uri->getQuery()) {

tests/ElasticsearchPhpHandlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,41 @@ public function testSignsWithProvidedCredentials()
6161
]);
6262
}
6363

64+
public function testEmptyRequestBodiesShouldBeNull()
65+
{
66+
$toWrap = function (array $ringRequest) {
67+
$this->assertNull($ringRequest['body']);
68+
69+
return $this->getGenericResponse();
70+
};
71+
72+
$client = $this->getElasticsearchClient(
73+
new ElasticsearchPhpHandler('us-west-2', null, $toWrap)
74+
);
75+
76+
$client->indices()->exists(['index' => 'index']);
77+
}
78+
79+
public function testNonEmptyRequestBodiesShouldNotBeNull()
80+
{
81+
$toWrap = function (array $ringRequest) {
82+
$this->assertNotNull($ringRequest['body']);
83+
84+
return $this->getGenericResponse();
85+
};
86+
87+
$client = $this->getElasticsearchClient(
88+
new ElasticsearchPhpHandler('us-west-2', null, $toWrap)
89+
);
90+
91+
$client->search([
92+
'index' => 'index',
93+
'body' => [
94+
'query' => [ 'match_all' => (object)[] ],
95+
],
96+
]);
97+
}
98+
6499
private function getElasticsearchClient(ElasticsearchPhpHandler $handler)
65100
{
66101
$builder = ClientBuilder::create()

0 commit comments

Comments
 (0)