summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/libs/http/HttpAcceptNegotiatorTest.php
blob: 02eac11887827df034685694851db87006efb94a (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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 );
	}

}