Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/http/src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @method PromiseInterface|mixed getAsync(Stringable|string $url, array $options = [])
* @method PromiseInterface|mixed postAsync(Stringable|string $url, mixed $body, array $options = [])
* @method PromiseInterface|mixed putAsync(Stringable|string $url, mixed $body, array $options = [])
* @method PromiseInterface|mixed queryAsync(Stringable|string $url, mixed $body = null, array $options = [])
* @method PromiseInterface|mixed deleteAsync(Stringable|string $url, mixed $body, array $options = [])
* @method PromiseInterface|mixed patchAsync(Stringable|string $url, mixed $body, array $options = [])
* @method PromiseInterface|mixed traceAsync(Stringable|string $url, array $options = [])
Expand All @@ -53,6 +54,8 @@
* HttpClientOptions())
* @method HttpClientResponse mustPut(Stringable|string $url, mixed $body = null, HttpClientOptions $options = new
* HttpClientOptions())
* @method HttpClientResponse mustQuery(Stringable|string $url, mixed $body = null, HttpClientOptions $options = new
* HttpClientOptions())
* @method HttpClientResponse mustDelete(Stringable|string $url, mixed $body = null, HttpClientOptions $options = new
* HttpClientOptions())
* @method HttpClientResponse mustPatch(Stringable|string $url, mixed $body = null, HttpClientOptions $options = new
Expand Down Expand Up @@ -320,6 +323,23 @@ public function patch(
return $this->request('PATCH', $url, $body, $options);
}

/**
* Method to send the QUERY command to the server.
*
* @param Stringable|string $url Path to the resource.
* @param mixed $body The query content.
* @param array|HttpClientOptions $options The request options.
*
* @return HttpClientResponse
*/
public function query(
Stringable|string $url,
mixed $body = null,
array|HttpClientOptions $options = new HttpClientOptions(),
): HttpClientResponse {
return $this->request('QUERY', $url, $body, $options);
}

public function connect(
Stringable|string $url,
array|HttpClientOptions $options = new HttpClientOptions(),
Expand Down Expand Up @@ -657,9 +677,9 @@ public function __call(string $name, array $args)
$ref = new ReflectionMethod($this, $method);

if (count($ref->getParameters()) >= 3) {
$promise = $this->requestAsync($method, ...$args);
$promise = $this->requestAsync(strtoupper($method), ...$args);
} else {
$promise = $this->requestAsync($method, $args[0] ?? '', null, $args[1] ?? []);
$promise = $this->requestAsync(strtoupper($method), $args[0] ?? '', null, $args[1] ?? []);
}

return $promise;
Expand Down
34 changes: 34 additions & 0 deletions packages/http/test/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,40 @@ public function postSendsFormDataBodyWithPostMethod(): void
self::assertEquals(http_build_query($data), $this->transport->request->getBody()->__toString());
}

#[Test]
public function querySendsQueryMethodWithBodyAndOptions(): void
{
$url = 'http://example.com/?foo=bar';
$body = ['filter' => ['status' => 'active']];
$headers = ['X-Foo' => 'Bar'];

$this->instance->query($url, $body, compact('headers'));

self::assertSame('QUERY', $this->transport->request->getMethod());
self::assertSame($url, $this->transport->request->getRequestTarget());
self::assertSame('{"filter":{"status":"active"}}', (string) $this->transport->request->getBody());
self::assertSame('Bar', $this->transport->request->getHeaderLine('X-Foo'));
}

#[Test]
public function queryAsyncUsesUppercaseQueryMethod(): void
{
$client = new class ([], new MockTransport()) extends HttpClient {
public ?RequestInterface $request = null;

public function sendAsyncRequest(RequestInterface $request): mixed
{
$this->request = $request;

return null;
}
};

$client->queryAsync('http://example.com', ['filter' => 'active']);

self::assertSame('QUERY', $client->request?->getMethod());
}

#[Test]
public function putSendsFormDataBodyWithPutMethod(): void
{
Expand Down