| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Rest\RequestBase; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class RequestBaseTest extends \MediaWikiUnitTestCase { |
| 11 |
use RestTestTrait; |
| 12 |
|
| 13 |
public function testGetCookiePrefix() { |
| 14 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 15 |
$this->assertSame( 'cookiePrefix', $rb->getCookiePrefix() ); |
| 16 |
} |
| 17 |
|
| 18 |
public function testGetNullCookie() { |
| 19 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 20 |
$rb->expects( $this->once() ) |
| 21 |
->method( 'getCookieParams' ) |
| 22 |
->willReturn( [ 'cookiePrefixcookie1' => 'value1' ] ); |
| 23 |
$this->assertNull( $rb->getCookie( 'nonExistingCookie' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function testGetCookie() { |
| 27 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 28 |
$rb->expects( $this->once() ) |
| 29 |
->method( 'getCookieParams' ) |
| 30 |
->willReturn( [ 'cookiePrefixcookie1' => 'value1' ] ); |
| 31 |
$this->assertSame( 'value1', $rb->getCookie( 'cookie1' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
public function testDefaultPathParams() { |
| 35 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 36 |
$this->assertSame( [], $rb->getPathParams() ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testSetPathParams() { |
| 40 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 41 |
$rb->setPathParams( [ 'foo' => 'bar' ] ); |
| 42 |
$this->assertSame( [ 'foo' => 'bar' ], $rb->getPathParams() ); |
| 43 |
} |
| 44 |
|
| 45 |
public function testNullPathParam() { |
| 46 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 47 |
$this->assertNull( $rb->getPathParam( 'Non-existing' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
public function testGetPathParam() { |
| 51 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 52 |
$rb->setPathParams( [ 'foo' => 'bar' ] ); |
| 53 |
$this->assertSame( 'bar', $rb->getPathParam( 'foo' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testDefaultHeaders() { |
| 57 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 58 |
$rb->setHeaders( [] ); |
| 59 |
$this->assertSame( [], $rb->getHeaders() ); |
| 60 |
} |
| 61 |
|
| 62 |
public function testGetHeaders() { |
| 63 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 64 |
$rb->setHeaders( [ 'Content-type' => 'application/json' ] ); |
| 65 |
$this->assertSame( [ 'application/json' ], $rb->getHeaders()[ 'Content-type' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
public static function provideHasBody() { |
| 69 |
yield 'nothing' |
| 70 |
=> [ [], false ]; |
| 71 |
|
| 72 |
yield 'content-length: 1' |
| 73 |
=> [ [ 'content-length' => '1' ], true ]; |
| 74 |
|
| 75 |
yield 'content-length: 0' |
| 76 |
=> [ [ 'content-length' => '0' ], true ]; |
| 77 |
|
| 78 |
yield 'content-length empty' |
| 79 |
=> [ [ 'content-length' => '' ], false ]; |
| 80 |
|
| 81 |
yield 'transfer-encoding: chunked' |
| 82 |
=> [ [ 'transfer-encoding' => 'chunked' ], true ]; |
| 83 |
|
| 84 |
yield 'transfer-encoding empty' |
| 85 |
=> [ [ 'transfer-encoding' => '' ], false ]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function testHasBody( $headers, $expected ) { |
| 92 |
$rb = $this->getMockForAbstractClass( RequestBase::class, [ 'cookiePrefix' ] ); |
| 93 |
$rb->setHeaders( $headers ); |
| 94 |
$this->assertSame( $expected, $rb->hasBody() ); |
| 95 |
} |
| 96 |
|
| 97 |
} |