| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
use MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter; |
| 6 |
use Psr\Http\Message\StreamInterface; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class MwHttpRequestToResponseInterfaceAdapterTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
public function testGivenNotExecutedRequest_constructorThrows() { |
| 19 |
$req = $this->newMockMWHttpRequestWithHeaders( [] ); |
| 20 |
|
| 21 |
$this->expectException( \LogicException::class ); |
| 22 |
new MwHttpRequestToResponseInterfaceAdapter( $req ); |
| 23 |
} |
| 24 |
|
| 25 |
public function testGetHeaders() { |
| 26 |
$headers = [ |
| 27 |
'foo' => [ 'bar' ], |
| 28 |
'some' => [ 'such', 'other' ] |
| 29 |
]; |
| 30 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( $headers ); |
| 31 |
|
| 32 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 33 |
$this->assertEquals( $headers, $response->getHeaders() ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testHasHeader() { |
| 37 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( [ |
| 38 |
'foo' => [ 'bar' ], |
| 39 |
] ); |
| 40 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 41 |
$this->assertTrue( $response->hasHeader( 'foo' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
public function testGivenExistingHeader_getHeaderReturnsValuesArray() { |
| 45 |
$headerValues = [ 'bar', 'baz' ]; |
| 46 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( [ |
| 47 |
'foo' => $headerValues, |
| 48 |
] ); |
| 49 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 50 |
$this->assertEquals( $headerValues, $response->getHeader( 'foo' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testGivenNonExistingHeader_getHeaderReturnsEmptyArray() { |
| 54 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( [ |
| 55 |
'some-header' => [ 'some', 'such' ], |
| 56 |
] ); |
| 57 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 58 |
$this->assertEquals( [], $response->getHeader( 'foo' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testGetBody() { |
| 62 |
$req = $this->newMockMWHttpRequestWithHeaders( [ 'Some-header' => [ 'abc' ] ] ); |
| 63 |
$body = '{ "some": "body" }'; |
| 64 |
$req->expects( $this->once() ) |
| 65 |
->method( 'getContent' ) |
| 66 |
->willReturn( $body ); |
| 67 |
|
| 68 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $req ); |
| 69 |
|
| 70 |
$this->assertEquals( $body, $response->getBody() ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testGetStatusCode() { |
| 74 |
$req = $this->newMockMWHttpRequestWithHeaders( [ 'Some-header' => [ 'abc' ] ] ); |
| 75 |
$status = 200; |
| 76 |
$req->expects( $this->once() ) |
| 77 |
->method( 'getStatus' ) |
| 78 |
->willReturn( $status ); |
| 79 |
|
| 80 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $req ); |
| 81 |
|
| 82 |
$this->assertEquals( $status, $response->getStatusCode() ); |
| 83 |
} |
| 84 |
|
| 85 |
public function testGivenExistingHeader_getHeaderLineReturnsCommaSeparatedValues() { |
| 86 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( [ |
| 87 |
'foo' => [ 'bar', 'baz' ], |
| 88 |
] ); |
| 89 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 90 |
$this->assertEquals( 'bar,baz', $response->getHeaderLine( 'foo' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
public function testGivenNonExistingHeader_getHeaderLineReturnsEmptyString() { |
| 94 |
$mwHttpReq = $this->newMockMWHttpRequestWithHeaders( [ |
| 95 |
'some-header' => [ 'some', 'such' ], |
| 96 |
] ); |
| 97 |
$response = new MwHttpRequestToResponseInterfaceAdapter( $mwHttpReq ); |
| 98 |
$this->assertSame( '', $response->getHeaderLine( 'foo' ) ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testGetProtocolVersionIsNotImplemented() { |
| 102 |
$this->expectException( \LogicException::class ); |
| 103 |
( new MwHttpRequestToResponseInterfaceAdapter( $this->createMock( MWHttpRequest::class ) ) ) |
| 104 |
->getProtocolVersion(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function testBuilderMethodsThrowLogicException( string $method, $args ) { |
| 111 |
$this->expectException( \LogicException::class ); |
| 112 |
( new MwHttpRequestToResponseInterfaceAdapter( $this->createMock( MWHttpRequest::class ) ) ) |
| 113 |
->{$method}( ...$args ); |
| 114 |
} |
| 115 |
|
| 116 |
public function unsupportedMethodsProvider() { |
| 117 |
return [ |
| 118 |
[ 'withAddedHeader', [ 'foo', 'bar' ] ], |
| 119 |
[ 'withBody', [ $this->createMock( StreamInterface::class ) ] ], |
| 120 |
[ 'withHeader', [ 'foo', 'bar' ] ], |
| 121 |
[ 'withoutHeader', [ 'foo', 'bar' ] ], |
| 122 |
[ 'withProtocolVersion', [ '1.1' ] ], |
| 123 |
[ 'withStatus', [ 200 ] ], |
| 124 |
]; |
| 125 |
} |
| 126 |
|
| 127 |
private function newMockMWHttpRequestWithHeaders( array $headers ) { |
| 128 |
$req = $this->createMock( MWHttpRequest::class ); |
| 129 |
$req->method( 'getResponseHeaders' ) |
| 130 |
->willReturn( $headers ); |
| 131 |
|
| 132 |
return $req; |
| 133 |
} |
| 134 |
|
| 135 |
} |