From 68b79ea5348b4f94cfa2a1c2c9aa9262f79ccd25 Mon Sep 17 00:00:00 2001 From: Konstantin Auffinger <62616071+kauffinger@users.noreply.github.com> Date: Tue, 8 Jul 2025 17:36:03 +0200 Subject: [PATCH] fix: add JSON Accept header to HTTP requests HttpTransport now sends requests with the 'Accept: application/json' header to ensure proper content negotiation. Added a unit test to verify the header is included in outgoing requests. --- src/Transport/HttpTransport.php | 1 + tests/Unit/Transport/HttpTransportTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Transport/HttpTransport.php b/src/Transport/HttpTransport.php index 0409802..d09015d 100644 --- a/src/Transport/HttpTransport.php +++ b/src/Transport/HttpTransport.php @@ -73,6 +73,7 @@ protected function createRequestPayload(string $method, array $params = []): arr protected function sendHttpRequest(array $payload): Response { return Http::timeout($this->getTimeout()) + ->acceptJson() ->when( $this->hasApiKey(), fn ($http) => $http->withToken($this->getApiKey()) diff --git a/tests/Unit/Transport/HttpTransportTest.php b/tests/Unit/Transport/HttpTransportTest.php index 701ca77..889e4b5 100644 --- a/tests/Unit/Transport/HttpTransportTest.php +++ b/tests/Unit/Transport/HttpTransportTest.php @@ -183,3 +183,22 @@ expect($result)->toBeArray(); }); + +it('sends requests with JSON Accept header', function (): void { + $transport = new HttpTransport([ + 'url' => 'http://example.com/api', + 'timeout' => 30, + ]); + + Http::fake([ + 'http://example.com/api' => Http::response([ + 'jsonrpc' => '2.0', + 'id' => '1', + 'result' => ['status' => 'success'], + ]), + ]); + + $transport->sendRequest('test/method'); + + Http::assertSent(fn ($request) => $request->hasHeader('Accept', 'application/json')); +});