|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Incapsula\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
8
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
9
|
|
|
use GuzzleHttp\HandlerStack; |
|
10
|
|
|
use GuzzleHttp\Middleware; |
|
11
|
|
|
use GuzzleHttp\Psr7\Response; |
|
12
|
|
|
use Incapsula\Client; |
|
13
|
|
|
use Incapsula\Credentials\Credentials; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @internal |
|
18
|
|
|
* @coversDefaultClass \Incapsula\Client |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ClientTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @covers ::send |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testGoodResponse(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$history = []; |
|
28
|
|
|
$httpClient = $this->createHttpClient([ |
|
29
|
|
|
new Response(200, [], file_get_contents(__DIR__.'/Responses/good_response.json')), |
|
30
|
|
|
], $history); |
|
31
|
|
|
|
|
32
|
|
|
$client = $this->createClient(); |
|
33
|
|
|
$client->setHttpClient($httpClient); |
|
34
|
|
|
$response = $client->send('https://dummy.incapsula.lan/api/something/v1/foo'); |
|
35
|
|
|
$request = $history[0]['request']; |
|
36
|
|
|
|
|
37
|
|
|
static::assertCount(1, $history); |
|
38
|
|
|
static::assertSame('application/x-www-form-urlencoded', $request->getHeader('Content-Type')[0]); |
|
39
|
|
|
static::assertSame('api_id=fakeid&api_key=fakekey', (string) $request->getBody()); |
|
40
|
|
|
static::assertSame('https://dummy.incapsula.lan/api/something/v1/foo', (string) $request->getUri()); |
|
41
|
|
|
|
|
42
|
|
|
static::assertSame(0, $response['res'], 'Good response code'); |
|
43
|
|
|
static::assertSame('OK', $response['res_message'], 'Good response message'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @covers ::send |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testBadResponse(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$httpClient = $this->createHttpClient([ |
|
52
|
|
|
new Response(200, [], file_get_contents(__DIR__.'/Responses/bad_response.json')), |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$this->expectException('Exception'); |
|
56
|
|
|
|
|
57
|
|
|
$client = $this->createClient(); |
|
58
|
|
|
$client->setHttpClient($httpClient); |
|
59
|
|
|
$client->send('https://dummy.incapsula.lan/api/something/v1/foo'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function createClient(): Client |
|
63
|
|
|
{ |
|
64
|
|
|
return new Client(['credentials' => new Credentials('fakeid', 'fakekey')]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Response[] $responses |
|
69
|
|
|
*/ |
|
70
|
|
|
private function createHttpClient(array $responses = [], array &$container = []): HttpClient |
|
71
|
|
|
{ |
|
72
|
|
|
$history = Middleware::history($container); |
|
73
|
|
|
$mock = new MockHandler($responses); |
|
74
|
|
|
$handler = HandlerStack::create($mock); |
|
75
|
|
|
$handler->push($history); |
|
76
|
|
|
|
|
77
|
|
|
return new HttpClient(['handler' => $handler]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|