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