HummingBirdAnimeClient/tests/AnimeClient/API/APIRequestBuilderTest.php

137 lines
3.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2020-12-10 17:06:50 -05:00
* PHP version 7.4+
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
2020-01-08 15:39:49 -05:00
* @copyright 2015 - 2020 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Tests\API;
use function Amp\Promise\wait;
2018-11-29 11:00:50 -05:00
use function Aviat\AnimeClient\getResponse;
use Aviat\AnimeClient\API\APIRequestBuilder;
use Aviat\Ion\Json;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
class APIRequestBuilderTest extends TestCase {
2017-02-22 15:08:29 -05:00
protected $builder;
2019-12-06 09:15:49 -05:00
public function setUp(): void {
$this->builder = new class extends APIRequestBuilder {
2020-04-10 16:35:01 -04:00
protected string $baseUrl = 'https://httpbin.org/';
2020-04-10 16:35:01 -04:00
protected array $defaultHeaders = ['User-Agent' => "Tim's Anime Client Testsuite / 4.0"];
};
$this->builder->setLogger(new NullLogger);
}
public function testGzipRequest(): void
{
$request = $this->builder->newRequest('GET', 'gzip')
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
$this->assertEquals(1, $body['gzipped']);
}
public function testInvalidRequestMethod(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->builder->newRequest('FOO', 'gzip')
->getFullRequest();
}
public function testRequestWithBasicAuth(): void
{
$request = $this->builder->newRequest('GET', 'headers')
->setBasicAuth('username', 'password')
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
$this->assertEquals('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']);
}
public function testRequestWithQueryString(): void
{
$query = [
'foo' => 'bar',
'bar' => [
'foo' => 'bar'
],
'baz' => [
'bar' => 'foo'
]
];
$expected = [
'foo' => 'bar',
'bar[foo]' => 'bar',
'baz[bar]' => 'foo'
];
$request = $this->builder->newRequest('GET', 'get')
->setQuery($query)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
$this->assertEquals($expected, $body['args']);
}
public function testFormValueRequest(): void
{
$formValues = [
'foo' => 'bar',
'bar' => 'foo'
];
$request = $this->builder->newRequest('POST', 'post')
->setFormFields($formValues)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
$this->assertEquals($formValues, $body['form']);
}
public function testFullUrlRequest(): void
{
$data = [
'foo' => [
'bar' => 1,
'baz' => [2, 3, 4],
2018-01-16 14:58:07 -05:00
'bazbar' => [
'a' => 1,
'b' => 2
]
]
];
$request = $this->builder->newRequest('PUT', 'https://httpbin.org/put')
->setHeader('Content-Type', 'application/json')
->setJsonBody($data)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
$this->assertEquals($data, $body['json']);
}
}