| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Http; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Http\HttpAcceptNegotiator; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class HttpAcceptNegotiatorTest extends TestCase { |
| 14 |
|
| 15 |
public static function provideGetFirstSupportedValue() { |
| 16 |
return [ |
| 17 |
[ |
| 18 |
[], |
| 19 |
[], |
| 20 |
null, |
| 21 |
null, |
| 22 |
], |
| 23 |
[ |
| 24 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 25 |
[ 'text/xzy', 'text/bar' ], |
| 26 |
null, |
| 27 |
'text/BAR', |
| 28 |
], |
| 29 |
[ |
| 30 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 31 |
[ 'text/xzy', 'text/xoo' ], |
| 32 |
'X', |
| 33 |
'X', |
| 34 |
], |
| 35 |
[ |
| 36 |
[ 'text/foo', 'text/bar', 'application/zuul' ], |
| 37 |
[ 'text/xoo', 'text/BAR', 'text/foo' ], |
| 38 |
null, |
| 39 |
'text/bar', |
| 40 |
], |
| 41 |
[ |
| 42 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 43 |
[ 'text/xoo', '*' ], |
| 44 |
null, |
| 45 |
'text/foo', |
| 46 |
], |
| 47 |
[ |
| 48 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 49 |
[ 'text/xoo', '*/*' ], |
| 50 |
null, |
| 51 |
'text/foo', |
| 52 |
], |
| 53 |
[ |
| 54 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 55 |
[ 'application/*', 'text/foo' ], |
| 56 |
null, |
| 57 |
'application/zuul', |
| 58 |
], |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) { |
| 66 |
$negotiator = new HttpAcceptNegotiator( $supported ); |
| 67 |
$actual = $negotiator->getFirstSupportedValue( $accepted, $default ); |
| 68 |
|
| 69 |
$this->assertEquals( $expected, $actual ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function provideGetBestSupportedKey() { |
| 73 |
return [ |
| 74 |
[ |
| 75 |
[], |
| 76 |
[], |
| 77 |
null, |
| 78 |
null, |
| 79 |
], |
| 80 |
[ |
| 81 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 82 |
[ 'text/xzy' => 1, 'text/bar' => 0.5 ], |
| 83 |
null, |
| 84 |
'text/BAR', |
| 85 |
], |
| 86 |
[ |
| 87 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 88 |
[ 'text/xzy' => 1, 'text/xoo' => 0.5 ], |
| 89 |
'X', |
| 90 |
'X', |
| 91 |
], |
| 92 |
[ |
| 93 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 94 |
[ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], |
| 95 |
null, |
| 96 |
'text/BAR', |
| 97 |
], |
| 98 |
[ |
| 99 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 100 |
[ 'text/foo' => 0, 'text/xoo' => 1 ], |
| 101 |
null, |
| 102 |
null, |
| 103 |
], |
| 104 |
[ |
| 105 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 106 |
[ 'text/xoo' => 0.5, '*' => 0.1 ], |
| 107 |
null, |
| 108 |
'text/foo', |
| 109 |
], |
| 110 |
[ |
| 111 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 112 |
[ 'text/xoo' => 0.5, '*/*' => 0.1 ], |
| 113 |
null, |
| 114 |
'text/foo', |
| 115 |
], |
| 116 |
[ |
| 117 |
[ 'text/foo', 'text/BAR', 'application/zuul' ], |
| 118 |
[ 'text/foo' => 0.3, 'application/*' => 0.8 ], |
| 119 |
null, |
| 120 |
'application/zuul', |
| 121 |
], |
| 122 |
[ |
| 123 |
[ 'application/rdf+xml', 'text/json', 'text/html' ], |
| 124 |
[ '*/*' => 1, 'text/html' => 1 ], |
| 125 |
null, |
| 126 |
'text/html', |
| 127 |
], |
| 128 |
[ |
| 129 |
[ 'application/rdf+xml', 'text/json', 'text/html' ], |
| 130 |
[ 'text/*' => 1, 'text/html' => 1 ], |
| 131 |
null, |
| 132 |
'text/html', |
| 133 |
], |
| 134 |
[ |
| 135 |
[ 'application/rdf+xml', 'text/html' ], |
| 136 |
[ '*/*' => 1, 'text/*' => 1 ], |
| 137 |
null, |
| 138 |
'text/html', |
| 139 |
], |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) { |
| 147 |
$negotiator = new HttpAcceptNegotiator( $supported ); |
| 148 |
$actual = $negotiator->getBestSupportedKey( $accepted, $default ); |
| 149 |
|
| 150 |
$this->assertEquals( $expected, $actual ); |
| 151 |
} |
| 152 |
|
| 153 |
} |