diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/libs/http')
-rw-r--r-- | www/wiki/tests/phpunit/includes/libs/http/HttpAcceptNegotiatorTest.php | 150 | ||||
-rw-r--r-- | www/wiki/tests/phpunit/includes/libs/http/HttpAcceptParserTest.php | 56 |
2 files changed, 206 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptNegotiatorTest.php b/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptNegotiatorTest.php new file mode 100644 index 00000000..02eac118 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptNegotiatorTest.php @@ -0,0 +1,150 @@ +<?php + +use Wikimedia\Http\HttpAcceptNegotiator; + +/** + * @covers Wikimedia\Http\HttpAcceptNegotiator + * + * @author Daniel Kinzler + */ +class HttpAcceptNegotiatorTest extends \PHPUnit\Framework\TestCase { + + public function provideGetFirstSupportedValue() { + return [ + [ // #0: empty + [], // supported + [], // accepted + null, // default + null, // expected + ], + [ // #1: simple + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xzy', 'text/bar' ], // accepted + null, // default + 'text/BAR', // expected + ], + [ // #2: default + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xzy', 'text/xoo' ], // accepted + 'X', // default + 'X', // expected + ], + [ // #3: preference + [ 'text/foo', 'text/bar', 'application/zuul' ], // supported + [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted + null, // default + 'text/bar', // expected + ], + [ // #4: * wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xoo', '*' ], // accepted + null, // default + 'text/foo', // expected + ], + [ // #5: */* wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xoo', '*/*' ], // accepted + null, // default + 'text/foo', // expected + ], + [ // #6: text/* wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'application/*', 'text/foo' ], // accepted + null, // default + 'application/zuul', // expected + ], + ]; + } + + /** + * @dataProvider provideGetFirstSupportedValue + */ + public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) { + $negotiator = new HttpAcceptNegotiator( $supported ); + $actual = $negotiator->getFirstSupportedValue( $accepted, $default ); + + $this->assertEquals( $expected, $actual ); + } + + public function provideGetBestSupportedKey() { + return [ + [ // #0: empty + [], // supported + [], // accepted + null, // default + null, // expected + ], + [ // #1: simple + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted + null, // default + 'text/BAR', // expected + ], + [ // #2: default + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted + 'X', // default + 'X', // expected + ], + [ // #3: weighted + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted + null, // default + 'text/BAR', // expected + ], + [ // #4: zero weight + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted + null, // default + null, // expected + ], + [ // #5: * wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted + null, // default + 'text/foo', // expected + ], + [ // #6: */* wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted + null, // default + 'text/foo', // expected + ], + [ // #7: text/* wildcard + [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported + [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted + null, // default + 'application/zuul', // expected + ], + [ // #8: Test specific format preferred over wildcard (T133314) + [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported + [ '*/*' => 1, 'text/html' => 1 ], // accepted + null, // default + 'text/html', // expected + ], + [ // #9: Test specific format preferred over range (T133314) + [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported + [ 'text/*' => 1, 'text/html' => 1 ], // accepted + null, // default + 'text/html', // expected + ], + [ // #10: Test range preferred over wildcard (T133314) + [ 'application/rdf+xml', 'text/html' ], // supported + [ '*/*' => 1, 'text/*' => 1 ], // accepted + null, // default + 'text/html', // expected + ], + ]; + } + + /** + * @dataProvider provideGetBestSupportedKey + */ + public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) { + $negotiator = new HttpAcceptNegotiator( $supported ); + $actual = $negotiator->getBestSupportedKey( $accepted, $default ); + + $this->assertEquals( $expected, $actual ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptParserTest.php b/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptParserTest.php new file mode 100644 index 00000000..e4b47b46 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptParserTest.php @@ -0,0 +1,56 @@ +<?php + +use Wikimedia\Http\HttpAcceptParser; + +/** + * @covers Wikimedia\Http\HttpAcceptParser + * + * @author Daniel Kinzler + */ +class HttpAcceptParserTest extends \PHPUnit\Framework\TestCase { + + public function provideParseWeights() { + return [ + [ // #0 + '', + [] + ], + [ // #1 + 'Foo/Bar', + [ 'foo/bar' => 1 ] + ], + [ // #2 + 'Accept: text/plain', + [ 'text/plain' => 1 ] + ], + [ // #3 + 'Accept: application/vnd.php.serialized, application/rdf+xml', + [ 'application/vnd.php.serialized' => 1, 'application/rdf+xml' => 1 ] + ], + [ // #4 + 'foo; q=0.2, xoo; q=0,text/n3', + [ 'text/n3' => 1, 'foo' => 0.2 ] + ], + [ // #5 + '*; q=0.2, */*; q=0.1,text/*', + [ 'text/*' => 1, '*' => 0.2, '*/*' => 0.1 ] + ], + // TODO: nicely ignore additional type paramerters + //[ // #6 + // 'Foo; q=0.2, Xoo; level=3, Bar; charset=xyz; q=0.4', + // [ 'xoo' => 1, 'bar' => 0.4, 'foo' => 0.1 ] + //], + ]; + } + + /** + * @dataProvider provideParseWeights + */ + public function testParseWeights( $header, $expected ) { + $parser = new HttpAcceptParser(); + $actual = $parser->parseWeights( $header ); + + $this->assertEquals( $expected, $actual ); // shouldn't be sensitive to order + } + +} |