diff --git a/tests/Unit/Client/NativeCurlClient/RequestTest.php b/tests/Unit/Client/NativeCurlClient/RequestTest.php index 66aef190..fb6f5dd6 100644 --- a/tests/Unit/Client/NativeCurlClient/RequestTest.php +++ b/tests/Unit/Client/NativeCurlClient/RequestTest.php @@ -147,4 +147,76 @@ public function testRequestWithUploadSendsFilepathAsContentAsIs(): void $this->assertSame('application/json', $response->getContentType()); $this->assertSame('{"upload":{}}', $response->getContent()); } + + /** + * @dataProvider getRequestHeaderData + * + * @param array $expectedHeaders + */ + #[DataProvider('getRequestHeaderData')] + public function testRequestSendsCorrectHeaders(?string $password, ?string $impersonateUser, string $path, string $contentType, array $expectedHeaders): void + { + $namespace = 'Redmine\Client'; + + $curl = curl_init(); + + $curlInit = $this->getFunctionMock($namespace, 'curl_init'); + $curlInit->expects($this->exactly(1))->willReturn($curl); + + $curlExec = $this->getFunctionMock($namespace, 'curl_exec'); + $curlExec->expects($this->exactly(1))->willReturn(''); + + $curlSetoptArray = $this->getFunctionMock($namespace, 'curl_setopt_array'); + $curlSetoptArray->expects($this->exactly(1))->willReturnCallback(function ($handle, array $options) use ($expectedHeaders): bool { + $this->assertSame($expectedHeaders, $options[CURLOPT_HTTPHEADER]); + + return true; + }); + + $curlGetinfo = $this->getFunctionMock($namespace, 'curl_getinfo'); + $curlGetinfo->expects($this->exactly(2))->willReturnMap(([ + [$curl, CURLINFO_HTTP_CODE, 200], + [$curl, CURLINFO_CONTENT_TYPE, ''], + ])); + + $curlErrno = $this->getFunctionMock($namespace, 'curl_errno'); + $curlErrno->expects($this->exactly(1))->willReturn(CURLE_OK); + + $this->getFunctionMock($namespace, 'curl_close'); + + $client = new NativeCurlClient( + 'http://test.local', + 'username', + $password, + ); + + if ($impersonateUser !== null) { + $client->startImpersonateUser($impersonateUser); + } + + $request = $this->createStub(Request::class); + $request->method('getMethod')->willReturn('GET'); + $request->method('getPath')->willReturn($path); + $request->method('getContentType')->willReturn($contentType); + $request->method('getContent')->willReturn(''); + + $client->request($request); + } + + /** + * @return array> + */ + public static function getRequestHeaderData(): array + { + return [ + 'api key' => [null, null, '/path', '', ['Expect: ', 'X-Redmine-API-Key: username']], + 'basic auth' => ['secret', null, '/path', '', ['Expect: ', 'Authorization: Basic ' . base64_encode('username:secret')]], + 'impersonate user' => [null, 'jdoe', '/path', '', ['Expect: ', 'X-Redmine-Switch-User: jdoe', 'X-Redmine-API-Key: username']], + 'content type from request' => [null, null, '/path', 'application/json', ['Expect: ', 'X-Redmine-API-Key: username', 'Content-Type: application/json']], + 'content type guessed from json path' => [null, null, '/issues.json', '', ['Expect: ', 'X-Redmine-API-Key: username', 'Content-Type: application/json']], + 'content type guessed from xml path' => [null, null, '/issues.xml', '', ['Expect: ', 'X-Redmine-API-Key: username', 'Content-Type: text/xml']], + 'content type guessed from upload path' => [null, null, '/uploads.json', '', ['Expect: ', 'X-Redmine-API-Key: username', 'Content-Type: application/octet-stream']], + 'no content type guessed' => [null, null, '/path', '', ['Expect: ', 'X-Redmine-API-Key: username']], + ]; + } } diff --git a/tests/Unit/Client/Psr18Client/RequestTest.php b/tests/Unit/Client/Psr18Client/RequestTest.php index a934dd6f..bfc1c3e4 100644 --- a/tests/Unit/Client/Psr18Client/RequestTest.php +++ b/tests/Unit/Client/Psr18Client/RequestTest.php @@ -132,4 +132,67 @@ public function testRequestThrowsClientException(): void $client->request($request); } + + /** + * @dataProvider getGuessedContentTypeData + */ + #[DataProvider('getGuessedContentTypeData')] + public function testRequestGuessesContentTypeFromPath(string $path, ?string $expectedContentType): void + { + $stream = $this->createStub(StreamInterface::class); + $stream->method('__toString')->willReturn(''); + + $response = $this->createStub(ResponseInterface::class); + $response->method('getStatusCode')->willReturn(200); + $response->method('getHeaderLine')->willReturn(''); + $response->method('getBody')->willReturn($stream); + + $httpClient = $this->createStub(ClientInterface::class); + $httpClient->method('sendRequest')->willReturn($response); + + $psrRequest = $this->createMock(RequestInterface::class); + $psrRequest->method('withBody')->willReturn($psrRequest); + $contentTypes = []; + $psrRequest->method('withHeader')->willReturnCallback(function (string $name, string $value) use ($psrRequest, &$contentTypes): RequestInterface { + if ($name === 'Content-Type') { + $contentTypes[] = $value; + } + + return $psrRequest; + }); + + $requestFactory = $this->createStub(RequestFactoryInterface::class); + $requestFactory->method('createRequest')->willReturn($psrRequest); + + $client = new Psr18Client( + $httpClient, + $requestFactory, + $this->createStub(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + $request = $this->createStub(Request::class); + $request->method('getMethod')->willReturn('GET'); + $request->method('getPath')->willReturn($path); + $request->method('getContentType')->willReturn(''); + $request->method('getContent')->willReturn(''); + + $client->request($request); + + $this->assertSame($expectedContentType === null ? [] : [$expectedContentType], $contentTypes); + } + + /** + * @return array> + */ + public static function getGuessedContentTypeData(): array + { + return [ + 'json path' => ['/issues.json', 'application/json'], + 'xml path' => ['/issues.xml', 'text/xml'], + 'upload path' => ['/uploads.json', 'application/octet-stream'], + 'other path' => ['/path', null], + ]; + } }