From d0016a8b647523f9930a2692436c3672af0131a0 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Fri, 31 May 2019 14:50:47 +1000 Subject: [PATCH] REST: tests for HelloHandler and HeaderContainer Change-Id: Ia214d4ad85bb2041e49b6cfe8278775387c30138 --- .../Rest/Handler/HelloHandlerTest.php | 81 +++++++++ .../includes/Rest/Handler/testRoutes.json | 6 + .../includes/Rest/HeaderContainerTest.php | 171 ++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 tests/phpunit/includes/Rest/Handler/HelloHandlerTest.php create mode 100644 tests/phpunit/includes/Rest/Handler/testRoutes.json create mode 100644 tests/phpunit/includes/Rest/HeaderContainerTest.php diff --git a/tests/phpunit/includes/Rest/Handler/HelloHandlerTest.php b/tests/phpunit/includes/Rest/Handler/HelloHandlerTest.php new file mode 100644 index 0000000000..9719f9ea67 --- /dev/null +++ b/tests/phpunit/includes/Rest/Handler/HelloHandlerTest.php @@ -0,0 +1,81 @@ + [ + [ + 'method' => 'GET', + 'uri' => self::makeUri( '/user/Tim/hello' ), + ], + [ + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'protocolVersion' => '1.1', + 'body' => '{"message":"Hello, Tim!"}', + ], + ], + 'method not allowed' => [ + [ + 'method' => 'POST', + 'uri' => self::makeUri( '/user/Tim/hello' ), + ], + [ + 'statusCode' => 405, + 'reasonPhrase' => 'Method Not Allowed', + 'protocolVersion' => '1.1', + 'body' => '{"httpCode":405,"httpReason":"Method Not Allowed"}', + ], + ], + ]; + } + + private static function makeUri( $path ) { + return new Uri( "http://www.example.com/rest$path" ); + } + + /** @dataProvider provideTestViaRouter */ + public function testViaRouter( $requestInfo, $responseInfo ) { + $router = new Router( + [ __DIR__ . '/testRoutes.json' ], + [], + '/rest', + new EmptyBagOStuff(), + new ResponseFactory() ); + $request = new RequestData( $requestInfo ); + $response = $router->execute( $request ); + if ( isset( $responseInfo['statusCode'] ) ) { + $this->assertSame( $responseInfo['statusCode'], $response->getStatusCode() ); + } + if ( isset( $responseInfo['reasonPhrase'] ) ) { + $this->assertSame( $responseInfo['reasonPhrase'], $response->getReasonPhrase() ); + } + if ( isset( $responseInfo['protocolVersion'] ) ) { + $this->assertSame( $responseInfo['protocolVersion'], $response->getProtocolVersion() ); + } + if ( isset( $responseInfo['body'] ) ) { + $this->assertSame( $responseInfo['body'], $response->getBody()->getContents() ); + } + $this->assertSame( + [], + array_diff( array_keys( $responseInfo ), [ + 'statusCode', + 'reasonPhrase', + 'protocolVersion', + 'body' + ] ), + '$responseInfo may not contain unknown keys' ); + } +} diff --git a/tests/phpunit/includes/Rest/Handler/testRoutes.json b/tests/phpunit/includes/Rest/Handler/testRoutes.json new file mode 100644 index 0000000000..6b440f7759 --- /dev/null +++ b/tests/phpunit/includes/Rest/Handler/testRoutes.json @@ -0,0 +1,6 @@ +[ + { + "path": "/user/{name}/hello", + "class": "MediaWiki\\Rest\\Handler\\HelloHandler" + } +] diff --git a/tests/phpunit/includes/Rest/HeaderContainerTest.php b/tests/phpunit/includes/Rest/HeaderContainerTest.php new file mode 100644 index 0000000000..d0fae9f567 --- /dev/null +++ b/tests/phpunit/includes/Rest/HeaderContainerTest.php @@ -0,0 +1,171 @@ + [ + [ + [ 'Test', 'foo' ] + ], + [ 'Test' => [ 'foo' ] ], + [ 'Test' => 'foo' ] + ], + 'replace' => [ + [ + [ 'Test', 'foo' ], + [ 'Test', 'bar' ], + ], + [ 'Test' => [ 'bar' ] ], + [ 'Test' => 'bar' ], + ], + 'array value' => [ + [ + [ 'Test', [ '1', '2' ] ], + [ 'Test', [ '3', '4' ] ], + ], + [ 'Test' => [ '3', '4' ] ], + [ 'Test' => '3, 4' ] + ], + 'preserve most recent case' => [ + [ + [ 'test', 'foo' ], + [ 'tesT', 'bar' ], + ], + [ 'tesT' => [ 'bar' ] ], + [ 'tesT' => 'bar' ] + ], + ]; + } + + /** @dataProvider provideSetHeader */ + public function testSetHeader( $setOps, $headers, $lines ) { + $hc = new HeaderContainer; + foreach ( $setOps as list( $name, $value ) ) { + $hc->setHeader( $name, $value ); + } + $this->assertSame( $headers, $hc->getHeaders() ); + $this->assertSame( $lines, $hc->getHeaderLines() ); + } + + public static function provideAddHeader() { + return [ + 'simple' => [ + [ + [ 'Test', 'foo' ] + ], + [ 'Test' => [ 'foo' ] ], + [ 'Test' => 'foo' ] + ], + 'add' => [ + [ + [ 'Test', 'foo' ], + [ 'Test', 'bar' ], + ], + [ 'Test' => [ 'foo', 'bar' ] ], + [ 'Test' => 'foo, bar' ], + ], + 'array value' => [ + [ + [ 'Test', [ '1', '2' ] ], + [ 'Test', [ '3', '4' ] ], + ], + [ 'Test' => [ '1', '2', '3', '4' ] ], + [ 'Test' => '1, 2, 3, 4' ] + ], + 'preserve original case' => [ + [ + [ 'Test', 'foo' ], + [ 'tesT', 'bar' ], + ], + [ 'Test' => [ 'foo', 'bar' ] ], + [ 'Test' => 'foo, bar' ] + ], + ]; + } + + /** @dataProvider provideAddHeader */ + public function testAddHeader( $addOps, $headers, $lines ) { + $hc = new HeaderContainer; + foreach ( $addOps as list( $name, $value ) ) { + $hc->addHeader( $name, $value ); + } + $this->assertSame( $headers, $hc->getHeaders() ); + $this->assertSame( $lines, $hc->getHeaderLines() ); + } + + public static function provideRemoveHeader() { + return [ + 'simple' => [ + [ [ 'Test', 'foo' ] ], + [ 'Test' ], + [], + [] + ], + 'case mismatch' => [ + [ [ 'Test', 'foo' ] ], + [ 'tesT' ], + [], + [] + ], + 'remove nonexistent' => [ + [ [ 'A', '1' ] ], + [ 'B' ], + [ 'A' => [ '1' ] ], + [ 'A' => '1' ] + ], + ]; + } + + /** @dataProvider provideRemoveHeader */ + public function testRemoveHeader( $addOps, $removeOps, $headers, $lines ) { + $hc = new HeaderContainer; + foreach ( $addOps as list( $name, $value ) ) { + $hc->addHeader( $name, $value ); + } + foreach ( $removeOps as $name ) { + $hc->removeHeader( $name ); + } + $this->assertSame( $headers, $hc->getHeaders() ); + $this->assertSame( $lines, $hc->getHeaderLines() ); + } + + public function testHasHeader() { + $hc = new HeaderContainer; + $hc->addHeader( 'A', '1' ); + $hc->addHeader( 'B', '2' ); + $hc->addHeader( 'C', '3' ); + $hc->removeHeader( 'B' ); + $hc->removeHeader( 'c' ); + $this->assertTrue( $hc->hasHeader( 'A' ) ); + $this->assertTrue( $hc->hasHeader( 'a' ) ); + $this->assertFalse( $hc->hasHeader( 'B' ) ); + $this->assertFalse( $hc->hasHeader( 'c' ) ); + $this->assertFalse( $hc->hasHeader( 'C' ) ); + } + + public function testGetRawHeaderLines() { + $hc = new HeaderContainer; + $hc->addHeader( 'A', '1' ); + $hc->addHeader( 'a', '2' ); + $hc->addHeader( 'b', '3' ); + $hc->addHeader( 'Set-Cookie', 'x' ); + $hc->addHeader( 'SET-cookie', 'y' ); + $this->assertSame( + [ + 'A: 1, 2', + 'b: 3', + 'Set-Cookie: x', + 'Set-Cookie: y', + ], + $hc->getRawHeaderLines() + ); + } +} -- 2.20.1