From b37fcd6f900bcc4616dc541deaa02d45e6174a17 Mon Sep 17 00:00:00 2001 From: vermeulena Date: Wed, 6 Jan 2021 16:24:18 +0100 Subject: [PATCH] Add support for urlencoded form data in requests (#8) --- src/Api/ApiClient.php | 4 ++ src/Api/Auth/Request/AuthRequest.php | 4 +- src/Request/ScxApiRequest.php | 2 + tests/AbstractTestCase.php | 7 --- tests/Api/ApiClientTest.php | 51 ++++++++++++++++++++-- tests/Api/Auth/Request/AuthRequestTest.php | 23 +++++++--- tests/Api/AuthAwareApiTest.php | 9 ++-- 7 files changed, 78 insertions(+), 22 deletions(-) diff --git a/src/Api/ApiClient.php b/src/Api/ApiClient.php index bfb6aac..dc6ca84 100644 --- a/src/Api/ApiClient.php +++ b/src/Api/ApiClient.php @@ -69,6 +69,10 @@ public function request(ScxApiRequest $request): ResponseInterface $options['multipart'] = $this->buildMultipartBody($request); } + if ($request->getContentType() === ScxApiRequest::CONTENT_TYPE_FORM) { + $options['form_params'] = $request->getParams(); + } + return $this->client->send($apiRequest, $options); } catch (ClientException | ServerException $exception) { $response = $exception->getResponse(); diff --git a/src/Api/Auth/Request/AuthRequest.php b/src/Api/Auth/Request/AuthRequest.php index b644b61..f46fb83 100644 --- a/src/Api/Auth/Request/AuthRequest.php +++ b/src/Api/Auth/Request/AuthRequest.php @@ -26,7 +26,7 @@ public function getRefreshToken(): string public function getUrl(): string { - return '/auth{?refreshToken}'; + return '/v1/auth'; } public function getHttpMethod(): string @@ -46,7 +46,7 @@ public function getBody(): ?string public function getContentType(): string { - return self::CONTENT_TYPE_JSON; + return self::CONTENT_TYPE_FORM; } public function getAdditionalHeaders(): array diff --git a/src/Request/ScxApiRequest.php b/src/Request/ScxApiRequest.php index f8ce66d..5aeb067 100644 --- a/src/Request/ScxApiRequest.php +++ b/src/Request/ScxApiRequest.php @@ -22,6 +22,8 @@ interface ScxApiRequest public const CONTENT_TYPE_JSON = 'application/json'; + public const CONTENT_TYPE_FORM = 'application/x-www-form-urlencoded'; + public function getUrl(): string; public function getHttpMethod(): string; diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 88d09fc..423e000 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -63,13 +63,6 @@ protected function createRequestFactoryMock(string $method, string $body = null) */ protected function createUrlFactoryMock(string $url, array $params = []) { - $urlFactory = Mockery::mock(UrlFactory::class); - - $urlFactory->shouldReceive('create') - ->with('http://localhost', $url, $params) - ->once() - ->andReturn(uniqid('url', true)); - return $urlFactory; } } diff --git a/tests/Api/ApiClientTest.php b/tests/Api/ApiClientTest.php index 5493a5f..129daeb 100644 --- a/tests/Api/ApiClientTest.php +++ b/tests/Api/ApiClientTest.php @@ -16,6 +16,7 @@ use JTL\SCX\Client\Model\ErrorList; use JTL\SCX\Client\ObjectSerializer; use JTL\SCX\Client\Request\ScxApiRequest; +use JTL\SCX\Client\Request\UrlFactory; use Mockery; use Psr\Http\Message\ResponseInterface; @@ -36,7 +37,12 @@ public function testCanCall(): void $configuration = $this->createConfigurationMock(); $requestFactory = $this->createRequestFactoryMock(ScxApiRequest::HTTP_METHOD_POST); - $urlFactory = $this->createUrlFactoryMock('/foo'); + + $urlFactory = Mockery::mock(UrlFactory::class); + $urlFactory->shouldReceive('create') + ->with('http://localhost', '/foo', []) + ->once() + ->andReturn(uniqid('url', true)); $api = new ApiClient($configuration, $client, $requestFactory, $urlFactory); @@ -47,7 +53,40 @@ public function testCanCall(): void $requestMock->shouldReceive('getAdditionalHeaders')->andReturn([]); $requestMock->shouldReceive('getContentType')->andReturn('bier'); $requestMock->shouldReceive('getBody')->andReturnNull(); - $requestMock->shouldReceive('getOptions')->andReturn([]); + $apiResponse = $api->request($requestMock); + + $this->assertSame($response, $apiResponse); + } + + public function testCanSendWithFormDataEncoded(): void + { + $formParams = ['foo' => 'bar']; + $response = Mockery::mock(ResponseInterface::class); + + $client = $client = Mockery::mock(ClientInterface::class); + $client->shouldReceive('send') + ->once() + ->with(Mockery::type(Request::class), ['form_params' => $formParams]) + ->andReturn($response); + + $configuration = $this->createConfigurationMock(); + $requestFactory = $this->createRequestFactoryMock(ScxApiRequest::HTTP_METHOD_POST); + + $urlFactory = Mockery::mock(UrlFactory::class); + $urlFactory->shouldReceive('create') + ->with('http://localhost', '/foo', $formParams) + ->once() + ->andReturn(uniqid('url', true)); + + $api = new ApiClient($configuration, $client, $requestFactory, $urlFactory); + + $requestMock = Mockery::mock(ScxApiRequest::class); + $requestMock->shouldReceive('getUrl')->andReturn('/foo'); + $requestMock->shouldReceive('getParams')->andReturn($formParams); + $requestMock->shouldReceive('getHttpMethod')->andReturn(ScxApiRequest::HTTP_METHOD_POST); + $requestMock->shouldReceive('getAdditionalHeaders')->andReturn([]); + $requestMock->shouldReceive('getContentType')->andReturn(ScxApiRequest::CONTENT_TYPE_FORM); + $requestMock->shouldReceive('getBody')->andReturnNull(); $apiResponse = $api->request($requestMock); $this->assertSame($response, $apiResponse); @@ -76,7 +115,12 @@ public function testCanThrowException(): void $configuration = $this->createConfigurationMock(); $requestFactory = $this->createRequestFactoryMock(ScxApiRequest::HTTP_METHOD_POST); - $urlFactory = $this->createUrlFactoryMock('/foo'); + + $urlFactory = Mockery::mock(UrlFactory::class); + $urlFactory->shouldReceive('create') + ->with('http://localhost', '/foo', []) + ->once() + ->andReturn(uniqid('url', true)); $errorList = Mockery::mock(ErrorList::class); @@ -95,7 +139,6 @@ public function testCanThrowException(): void $requestMock->shouldReceive('getAdditionalHeaders')->andReturn([]); $requestMock->shouldReceive('getContentType')->andReturn('bier'); $requestMock->shouldReceive('getBody')->andReturnNull(); - $requestMock->shouldReceive('getOptions')->andReturn([]); $this->expectException(RequestFailedException::class); $api->request($requestMock); diff --git a/tests/Api/Auth/Request/AuthRequestTest.php b/tests/Api/Auth/Request/AuthRequestTest.php index 4069e04..8a969c1 100644 --- a/tests/Api/Auth/Request/AuthRequestTest.php +++ b/tests/Api/Auth/Request/AuthRequestTest.php @@ -8,6 +8,7 @@ namespace JTL\SCX\Client\Api\Auth\Request; +use JTL\SCX\Client\Request\ScxApiRequest; use PHPUnit\Framework\TestCase; /** @@ -31,16 +32,25 @@ public function it_has_correct_refresh_token(): void /** * @test */ - public function it_has_correct_url() + public function it_has_correct_url(): void { $sut = new AuthRequest('foo'); - $this->assertEquals('/auth{?refreshToken}', $sut->getUrl()); + $this->assertEquals('/v1/auth', $sut->getUrl()); } /** * @test */ - public function it_has_correct_http_method() + public function it_has_correct_content_type(): void + { + $sut = new AuthRequest('foo'); + $this->assertEquals(ScxApiRequest::CONTENT_TYPE_FORM, $sut->getContentType()); + } + + /** + * @test + */ + public function it_has_correct_http_method(): void { $sut = new AuthRequest('foo'); $this->assertEquals(AuthRequest::HTTP_METHOD_POST, $sut->getHttpMethod()); @@ -49,11 +59,12 @@ public function it_has_correct_http_method() /** * @test */ - public function it_has_correct_http_parameters() + public function it_has_correct_http_parameters(): void { - $sut = new AuthRequest('a_refresh_token'); + $token = 'a_refresh_token'; + $sut = new AuthRequest($token); $params = $sut->getParams(); $this->assertArrayHasKey('refreshToken', $params); - $this->assertEquals('a_refresh_token', $params['refreshToken']); + $this->assertEquals($token, $params['refreshToken']); } } diff --git a/tests/Api/AuthAwareApiTest.php b/tests/Api/AuthAwareApiTest.php index e15e720..e802d46 100644 --- a/tests/Api/AuthAwareApiTest.php +++ b/tests/Api/AuthAwareApiTest.php @@ -101,7 +101,12 @@ public function testCanRequestWithoutSessionToken(): void ->andReturn($this->response); $requestFactory = $this->createRequestFactoryMock(ScxApiRequest::HTTP_METHOD_POST); - $urlFactory = $this->createUrlFactoryMock('/foo'); + + $urlFactory = Mockery::mock(UrlFactory::class); + $urlFactory->shouldReceive('create') + ->with('http://localhost', '/foo', []) + ->once() + ->andReturn(uniqid('url', true)); $testAuthApi = new AuthAwareApiClient( $configuration, @@ -144,7 +149,6 @@ public function testCanRequestWithoutSessionToken(): void $requestMock->shouldReceive('getAdditionalHeaders')->andReturn([]); $requestMock->shouldReceive('getContentType')->andReturn('bier'); $requestMock->shouldReceive('getBody')->andReturnNull(); - $requestMock->shouldReceive('getOptions')->andReturn([]); $response = $testAuthApi->request($requestMock); $this->assertSame($this->response, $response); @@ -235,7 +239,6 @@ public function testCanRequestIfRequestFails(): void $requestMock->shouldReceive('getAdditionalHeaders')->andReturn([]); $requestMock->shouldReceive('getContentType')->andReturn('bier'); $requestMock->shouldReceive('getBody')->andReturnNull(); - $requestMock->shouldReceive('getOptions')->andReturn([]); $response = $testAuthApi->request($requestMock); $this->assertSame($this->response, $response);