| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use GuzzleHttp\Psr7\Uri; |
| 6 |
use MediaWiki\Rest\RequestData; |
| 7 |
use MediaWiki\Rest\StringStream; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class RequestDataTest extends \MediaWikiUnitTestCase { |
| 13 |
use RestTestTrait; |
| 14 |
|
| 15 |
public function testDefaultMethodIsGet() { |
| 16 |
$request = new RequestData( [] ); |
| 17 |
$this->assertSame( 'GET', $request->getMethod() ); |
| 18 |
} |
| 19 |
|
| 20 |
public function testGetMethod() { |
| 21 |
$request = new RequestData( [ |
| 22 |
'method' => 'HEAD' |
| 23 |
] ); |
| 24 |
$this->assertSame( 'HEAD', $request->getMethod() ); |
| 25 |
} |
| 26 |
|
| 27 |
public function testDefaultProtocolVersion() { |
| 28 |
$request = new RequestData( [] ); |
| 29 |
$this->assertSame( '1.1', $request->getProtocolVersion() ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testGetProtocolVersion() { |
| 33 |
$request = new RequestData( [ |
| 34 |
'protocolVersion' => '3.0' |
| 35 |
] ); |
| 36 |
$this->assertSame( '3.0', $request->getProtocolVersion() ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testDefaultServerParams() { |
| 40 |
$request = new RequestData( [] ); |
| 41 |
$this->assertSame( [], $request->getServerParams() ); |
| 42 |
} |
| 43 |
|
| 44 |
public function testGetServerParams() { |
| 45 |
$request = new RequestData( [ |
| 46 |
'serverParams' => [ 'serverFoo' => 'serverBar' ] |
| 47 |
] ); |
| 48 |
$this->assertSame( [ 'serverFoo' => 'serverBar' ], $request->getServerParams() ); |
| 49 |
} |
| 50 |
|
| 51 |
public function testDefaultCookieParams() { |
| 52 |
$request = new RequestData( [] ); |
| 53 |
$this->assertSame( [], $request->getCookieParams() ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testGetCookieParams() { |
| 57 |
$request = new RequestData( [ |
| 58 |
'cookieParams' => 'cookieData' |
| 59 |
] ); |
| 60 |
$this->assertSame( 'cookieData', $request->getCookieParams() ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testDefaultQueryParams() { |
| 64 |
$request = new RequestData( [] ); |
| 65 |
$this->assertSame( [], $request->getQueryParams() ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testGetQueryParams() { |
| 69 |
$request = new RequestData( [ |
| 70 |
'queryParams' => 'queryData' |
| 71 |
] ); |
| 72 |
$this->assertSame( 'queryData', $request->getQueryParams() ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testDefaultHeaders() { |
| 76 |
$request = new RequestData( [] ); |
| 77 |
$this->assertSame( [], $request->getHeaders() ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testGetHeaders() { |
| 81 |
$request = new RequestData( [ |
| 82 |
'headers' => [ 'headersData' ] |
| 83 |
] ); |
| 84 |
$this->assertSame( [ [ 'headersData' ] ], $request->getHeaders() ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testSetHeaders() { |
| 88 |
$request = new RequestData( [] ); |
| 89 |
$request->setHeaders( [ 'Content-type' => 'application/json' ] ); |
| 90 |
$this->assertSame( [ 'application/json' ], $request->getHeaders()[ 'Content-type' ] ); |
| 91 |
} |
| 92 |
|
| 93 |
public function testDefaultUri() { |
| 94 |
$request = new RequestData( [] ); |
| 95 |
$this->assertInstanceOf( Uri::class, $request->getUri() ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testGetUri() { |
| 99 |
$request = new RequestData( [ |
| 100 |
'uri' => new Uri( '/rest/mock/RouterTest/hello' ), |
| 101 |
] ); |
| 102 |
$this->assertInstanceOf( Uri::class, $request->getUri() ); |
| 103 |
$this->assertSame( '/rest/mock/RouterTest/hello', $request->getUri()->__toString() ); |
| 104 |
} |
| 105 |
|
| 106 |
public function testDefaultBody() { |
| 107 |
$request = new RequestData( [] ); |
| 108 |
$this->assertInstanceOf( StringStream::class, $request->getBody() ); |
| 109 |
$this->assertSame( "", $request->getBody()->getContents() ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testGetBody() { |
| 113 |
$request = new RequestData( [ |
| 114 |
'bodyContents' => 'bodyContents', |
| 115 |
] ); |
| 116 |
$this->assertInstanceOf( StringStream::class, $request->getBody() ); |
| 117 |
$this->assertSame( "bodyContents", $request->getBody()->getContents() ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
public function testDefaultCookiePrefix() { |
| 122 |
$request = new RequestData( [] ); |
| 123 |
$this->assertSame( '', $request->getCookiePrefix() ); |
| 124 |
} |
| 125 |
|
| 126 |
public function testGetCookiePrefix() { |
| 127 |
$request = new RequestData( [ |
| 128 |
'cookiePrefix' => 'cookiePrefixData' |
| 129 |
] ); |
| 130 |
$this->assertSame( 'cookiePrefixData', $request->getCookiePrefix() ); |
| 131 |
} |
| 132 |
|
| 133 |
public function testGetNullCookie() { |
| 134 |
$request = new RequestData( [] ); |
| 135 |
$this->assertNull( $request->getCookie( 'nonExistingCookie' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
public function testGetCookie() { |
| 139 |
$request = new RequestData( [ |
| 140 |
'cookieParams' => [ 'cookie1' => 'value1' ] |
| 141 |
] ); |
| 142 |
$this->assertSame( 'value1', $request->getCookie( 'cookie1' ) ); |
| 143 |
} |
| 144 |
|
| 145 |
public function testDefaultPathParams() { |
| 146 |
$request = new RequestData( [] ); |
| 147 |
$this->assertSame( [], $request->getPathParams() ); |
| 148 |
} |
| 149 |
|
| 150 |
public function testSetPathParams() { |
| 151 |
$request = new RequestData( [] ); |
| 152 |
$request->setPathParams( [ 'foo' => 'bar' ] ); |
| 153 |
$this->assertSame( [ 'foo' => 'bar' ], $request->getPathParams() ); |
| 154 |
} |
| 155 |
|
| 156 |
public function testNullPathParam() { |
| 157 |
$request = new RequestData( [] ); |
| 158 |
$this->assertNull( $request->getPathParam( 'Non-existing' ) ); |
| 159 |
} |
| 160 |
|
| 161 |
public function testGetPathParam() { |
| 162 |
$request = new RequestData( [ |
| 163 |
'pathParams' => [ 'foo' => 'bar' ] |
| 164 |
] ); |
| 165 |
$this->assertSame( 'bar', $request->getPathParam( 'foo' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
public function testDefaultParsedBody() { |
| 169 |
$request = new RequestData( [] ); |
| 170 |
$this->assertNull( $request->getParsedBody() ); |
| 171 |
} |
| 172 |
|
| 173 |
public function testSetParsedBody() { |
| 174 |
$request = new RequestData( [] ); |
| 175 |
$request->setParsedBody( [ 'bodyData' ] ); |
| 176 |
$this->assertEquals( [ 'bodyData' ], $request->getParsedBody() ); |
| 177 |
} |
| 178 |
|
| 179 |
public function testGetParsedBody() { |
| 180 |
$request = new RequestData( [ |
| 181 |
'parsedBody' => [ 'bodyData' ] |
| 182 |
] ); |
| 183 |
$this->assertEquals( [ 'bodyData' ], $request->getParsedBody() ); |
| 184 |
} |
| 185 |
|
| 186 |
public static function provideBodyTypeBasedOnHeader() { |
| 187 |
return [ |
| 188 |
'application/json' => [ |
| 189 |
[ 'Content-Type' => 'application/json' ], |
| 190 |
'application/json' |
| 191 |
], |
| 192 |
'application/json with charset' => [ |
| 193 |
[ 'Content-Type' => 'application/json; charset=utf-8' ], |
| 194 |
'application/json' |
| 195 |
], |
| 196 |
'application/form-urlencoded' => [ |
| 197 |
[ 'Content-Type' => 'application/x-www-form-urlencoded' ], |
| 198 |
'application/x-www-form-urlencoded' |
| 199 |
], |
| 200 |
'multipart/form-data' => [ |
| 201 |
[ 'Content-Type' => 'multipart/form-data' ], |
| 202 |
'multipart/form-data' |
| 203 |
], |
| 204 |
'no header' => [ |
| 205 |
[], |
| 206 |
null |
| 207 |
], |
| 208 |
'empty header' => [ |
| 209 |
'headers' => [ 'Content-Type' => '' ], |
| 210 |
null |
| 211 |
], |
| 212 |
|
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public function testBodyTypeBasedOnHeader( $headers, $expectedResult ) { |
| 218 |
$request = new RequestData( [ |
| 219 |
'headers' => $headers |
| 220 |
] ); |
| 221 |
$this->assertSame( $expectedResult, $request->getBodyType() ); |
| 222 |
} |
| 223 |
|
| 224 |
public static function provideHasBody() { |
| 225 |
yield 'nothing' |
| 226 |
=> [ [], false ]; |
| 227 |
|
| 228 |
yield 'content-length: 1' |
| 229 |
=> [ [ 'content-length' => '1' ], true ]; |
| 230 |
|
| 231 |
yield 'content-length: 0' |
| 232 |
=> [ [ 'content-length' => '0' ], true ]; |
| 233 |
|
| 234 |
yield 'content-length empty' |
| 235 |
=> [ [ 'content-length' => '' ], false ]; |
| 236 |
|
| 237 |
yield 'transfer-encoding: chunked' |
| 238 |
=> [ [ 'transfer-encoding' => 'chunked' ], true ]; |
| 239 |
|
| 240 |
yield 'transfer-encoding empty' |
| 241 |
=> [ [ 'transfer-encoding' => '' ], false ]; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function testHasBodyBasedOnHeader( $headers, $expected ) { |
| 248 |
$request = new RequestData( [ |
| 249 |
'headers' => $headers |
| 250 |
] ); |
| 251 |
$this->assertSame( $expected, $request->hasBody() ); |
| 252 |
} |
| 253 |
|
| 254 |
public function testHasBodyWithContent() { |
| 255 |
$request = new RequestData( [ |
| 256 |
'bodyContents' => 'test test test' |
| 257 |
] ); |
| 258 |
$this->assertTrue( $request->hasBody() ); |
| 259 |
} |
| 260 |
|
| 261 |
public function testHasBodyWithParsedBody() { |
| 262 |
$request = new RequestData( [ |
| 263 |
'parsedBody' => [ 'foo' => 'bar' ] |
| 264 |
] ); |
| 265 |
$this->assertTrue( $request->hasBody() ); |
| 266 |
} |
| 267 |
} |