Skip to content

Commit

Permalink
Add support for urlencoded form data in requests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Das-Ente committed Jan 6, 2021
1 parent 4ece3ea commit b37fcd6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/Api/ApiClient.php
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Auth/Request/AuthRequest.php
Expand Up @@ -26,7 +26,7 @@ public function getRefreshToken(): string

public function getUrl(): string
{
return '/auth{?refreshToken}';
return '/v1/auth';
}

public function getHttpMethod(): string
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Request/ScxApiRequest.php
Expand Up @@ -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;
Expand Down
7 changes: 0 additions & 7 deletions tests/AbstractTestCase.php
Expand Up @@ -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;
}
}
51 changes: 47 additions & 4 deletions tests/Api/ApiClientTest.php
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
23 changes: 17 additions & 6 deletions tests/Api/Auth/Request/AuthRequestTest.php
Expand Up @@ -8,6 +8,7 @@

namespace JTL\SCX\Client\Api\Auth\Request;

use JTL\SCX\Client\Request\ScxApiRequest;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -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());
Expand All @@ -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']);
}
}
9 changes: 6 additions & 3 deletions tests/Api/AuthAwareApiTest.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b37fcd6

Please sign in to comment.