diff --git a/src/Http/Client.php b/src/Http/Client.php index 9e1fc81..8d268d3 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -231,7 +231,7 @@ public function recorded(callable $filterCallback = null): array * * @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request. */ - public function json(string $method, string $url, array $params = []): Response + public function json(string $method, string $url, array $params = [], array $headers = []): Response { $method = strtoupper($method); @@ -240,6 +240,10 @@ public function json(string $method, string $url, array $params = []): Response ->withHeader('Content-Type', 'application/json') ->withHeader('Accept', 'application/json'); + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + if ($method !== 'GET') { $request = $request->withBody($this->streamFactory->createStream(json_encode($params))); } @@ -257,7 +261,7 @@ public function json(string $method, string $url, array $params = []): Response * * @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request. */ - public function form(string $method, string $url, array $params = []): Response + public function form(string $method, string $url, array $params = [], array $headers = []): Response { $method = strtoupper($method); @@ -265,6 +269,10 @@ public function form(string $method, string $url, array $params = []): Response ->createRequest($method, $this->buildUrl($method, $url, $params)) ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + if ($method !== 'GET') { $request = $request->withBody($this->streamFactory->createStream(http_build_query($params))); } diff --git a/tests/Client/FormTest.php b/tests/Client/FormTest.php index 5e10229..8e7bd1a 100644 --- a/tests/Client/FormTest.php +++ b/tests/Client/FormTest.php @@ -130,4 +130,37 @@ public function making_a_non_get_request_with_options_puts_them_in_the_body(stri $testingClient->client->form($verb, $url, ['active' => 1, 'page' => 12]); } + + /** + * @test + * @dataProvider httpVerbsProvider + */ + public function can_provide_custom_headers(string $verb) + { + $url = 'https://example.com'; + + $testingClient = $this->createTestingClient(); + $psr7Request = null; + + $psr7Response = $this->expectSendRequest($testingClient, function (RequestInterface $request) use ( + $url, + $verb, + &$psr7Request, + ) { + $this->assertSame('application/x-www-form-urlencoded', $request->getHeaderLine('Content-Type')); + $this->assertSame('Custom Value', $request->getHeaderLine('X-Custom-Header')); + + $this->assertSame($verb, strtoupper($request->getMethod())); + $this->assertSame($url, (string) $request->getUri()); + + $psr7Request = $request; + + return true; + }); + + $response = $testingClient->client->form($verb, $url, [], ['X-Custom-Header' => 'Custom Value']); + + $this->assertSame($psr7Request, $response->toPsr7Request()); + $this->assertSame($psr7Response, $response->toPsr7Response()); + } } diff --git a/tests/Client/JsonTest.php b/tests/Client/JsonTest.php index 1cfe674..7ea0cfe 100644 --- a/tests/Client/JsonTest.php +++ b/tests/Client/JsonTest.php @@ -136,4 +136,38 @@ public function making_a_non_get_request_with_options_puts_them_in_the_body(stri $testingClient->client->json($verb, $url, ['active' => 1, 'page' => 12]); } + + /** + * @test + * @dataProvider httpVerbsProvider + */ + public function can_provide_custom_headers(string $verb) + { + $url = 'https://example.com'; + + $testingClient = $this->createTestingClient(); + $psr7Request = null; + + $psr7Response = $this->expectSendRequest($testingClient, function (RequestInterface $request) use ( + $url, + $verb, + &$psr7Request, + ) { + $this->assertSame('application/json', $request->getHeaderLine('Content-Type')); + $this->assertSame('application/json', $request->getHeaderLine('Accept')); + $this->assertSame('Custom Value', $request->getHeaderLine('X-Custom-Header')); + + $this->assertSame($verb, strtoupper($request->getMethod())); + $this->assertSame($url, (string) $request->getUri()); + + $psr7Request = $request; + + return true; + }); + + $response = $testingClient->client->json($verb, $url, [], ['X-Custom-Header' => 'Custom Value']); + + $this->assertSame($psr7Request, $response->toPsr7Request()); + $this->assertSame($psr7Response, $response->toPsr7Response()); + } }