kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Tests\Koded\Http; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | use Koded\Http\ClientRequest; |
||
| 7 | use Koded\Http\Interfaces\HttpMethod; |
||
| 8 | use Koded\Http\Interfaces\HttpStatus; |
||
| 9 | use Koded\Http\Interfaces\Request; |
||
| 10 | use Koded\Http\Uri; |
||
| 11 | use PHPUnit\Framework\TestCase; |
||
| 12 | use Psr\Http\Message\UriInterface; |
||
| 13 | |||
| 14 | class ClientRequestTest extends TestCase |
||
| 15 | { |
||
| 16 | private ClientRequest|Request $SUT; |
||
| 17 | |||
| 18 | public function test_defaults() |
||
| 19 | { |
||
| 20 | $this->assertSame(HttpMethod::POST->value, $this->SUT->getMethod()); |
||
| 21 | $this->assertInstanceOf(UriInterface::class, $this->SUT->getUri()); |
||
| 22 | $this->assertSame('/', $this->SUT->getRequestTarget(), "No URI (path) and no request-target is provided"); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function test_uri() |
||
| 26 | { |
||
| 27 | $this->assertInstanceOf(Uri::class, $this->SUT->getUri()); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function test_should_change_the_method() |
||
| 31 | { |
||
| 32 | $request = $this->SUT->withMethod('get'); |
||
| 33 | $this->assertSame('GET', $request->getMethod()); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function test_request_target() |
||
| 37 | { |
||
| 38 | $request = $this->SUT->withRequestTarget('42'); |
||
| 39 | $this->assertSame('42', $request->getRequestTarget()); |
||
| 40 | $this->assertNotSame($request, $this->SUT); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function test_request_target_with_query_string() |
||
| 44 | { |
||
| 45 | $uri = new Uri('http://example.net/home?foo=bared'); |
||
| 46 | $request = $this->SUT->withUri($uri); |
||
| 47 | |||
| 48 | $this->assertSame('/home?foo=bared', $request->getRequestTarget()); |
||
| 49 | $this->assertNotSame($request, $this->SUT); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function test_invalid_request_target() |
||
| 53 | { |
||
| 54 | $this->expectException(InvalidArgumentException::class); |
||
| 55 | $this->expectExceptionMessage(ClientRequest::E_INVALID_REQUEST_TARGET); |
||
| 56 | $this->expectExceptionCode(HttpStatus::BAD_REQUEST); |
||
| 57 | |||
| 58 | $this->SUT->withRequestTarget('foo bar'); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function test_with_uri_preserving_the_host() |
||
| 62 | { |
||
| 63 | $uri = new Uri('http://example.net/42'); |
||
| 64 | $request = $this->SUT->withUri($uri, true); |
||
| 65 | |||
| 66 | $this->assertSame(['example.org'], $request->getHeader('host'), 'The previous host is preserved in the header'); |
||
| 67 | $this->assertSame('example.net', $request->getUri()->getHost(), 'Request URI has its own hostname'); |
||
| 68 | $this->assertEquals('/42', $request->getPath()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 69 | } |
||
| 70 | |||
| 71 | public function test_with_uri_and_not_preserving_the_host() |
||
| 72 | { |
||
| 73 | $uri = new Uri('http://example.net/42'); |
||
| 74 | $request = $this->SUT->withUri($uri, false); |
||
| 75 | $this->assertSame(['example.net'], $request->getHeader('host'), 'Host is taken from Uri'); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function test_construction_with_array_body() |
||
| 79 | { |
||
| 80 | $this->SUT = new ClientRequest(HttpMethod::GET, 'http://example.org', ['foo' => 'bar']); |
||
| 81 | $this->assertSame('{"foo":"bar"}', $this->SUT->getBody()->getContents()); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function test_construction_with_iterable_body() |
||
| 85 | { |
||
| 86 | $this->SUT = new ClientRequest(HttpMethod::GET, 'http://example.org', new \ArrayObject(['foo' => 'bar'])); |
||
| 87 | $this->assertSame('{"foo":"bar"}', $this->SUT->getBody()->getContents()); |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function setUp(): void |
||
| 91 | { |
||
| 92 | $this->SUT = new ClientRequest(HttpMethod::POST, 'http://example.org'); |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function tearDown(): void |
||
| 96 | { |
||
| 97 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
| 98 | } |
||
| 99 | } |
||
| 100 |