|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus}; |
|
6
|
|
|
use Koded\Http\Uri; |
|
7
|
|
|
use function Koded\Http\create_stream; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait ClientTestCaseTrait ensures some consistent behaviour |
|
11
|
|
|
* across the HTTP client implementations. |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @group internet |
|
17
|
|
|
*/ |
|
18
|
|
|
trait ClientTestCaseTrait |
|
19
|
|
|
{ |
|
20
|
|
|
private ?HttpRequestClient $SUT; |
|
21
|
|
|
|
|
22
|
|
|
public function test_read_on_success() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->markTestSkipped(); |
|
|
|
|
|
|
25
|
|
|
$response = $this->SUT->read(); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$this->assertSame(HttpStatus::OK, $response->getStatusCode(), (string)$response->getBody()); |
|
|
|
|
|
|
28
|
|
|
$this->assertStringContainsString('text/html', $response->getHeaderLine('Content-Type')); |
|
|
|
|
|
|
29
|
|
|
$this->assertGreaterThan(0, (string)$response->getBody()->getSize()); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function test_should_exit_on_bad_url() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
|
|
|
|
|
35
|
|
|
$this->expectExceptionMessage('Please provide a valid URI'); |
|
|
|
|
|
|
36
|
|
|
$this->expectExceptionCode(HttpStatus::BAD_REQUEST); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$this->SUT->withUri(new Uri('scheme://host:port')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function test_should_exit_on_bad_request() |
|
42
|
|
|
{ |
|
43
|
|
|
/** @var HttpRequestClient $SUT */ |
|
44
|
|
|
$SUT = $this->SUT->withBody(create_stream(json_encode(['foo' => 'bar']))); |
|
45
|
|
|
|
|
46
|
|
|
$badResponse = $SUT->read(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertSame(HttpStatus::BAD_REQUEST, $badResponse->getStatusCode(), get_class($SUT)); |
|
49
|
|
|
$this->assertSame($badResponse->getHeaderLine('Content-type'), 'application/problem+json'); |
|
50
|
|
|
$this->assertStringContainsString('failed to open stream: you should not set the message body with safe HTTP methods', |
|
51
|
|
|
(string)$badResponse->getBody()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function tearDown(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->SUT = null; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|