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