File: HttpAcceptParserTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (174 lines) | stat: -rw-r--r-- 3,368 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

namespace Wikimedia\Tests\Http;

use PHPUnit\Framework\TestCase;
use Wikimedia\Http\HttpAcceptParser;

/**
 * @covers \Wikimedia\Http\HttpAcceptParser
 *
 * @author Daniel Kinzler
 */
class HttpAcceptParserTest extends TestCase {

	public static 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
				'foo/*; q=0.2, */*; q=0.1,text/*',
				[ 'text/*' => 1, 'foo/*' => 0.2, '*/*' => 0.1 ]
			],
			[ // #6
				'Foo/*; q=0.2, Xoo/*; level=3, Bar/*; charset=xyz; q=0.4',
				[ 'xoo/*' => 1, 'bar/*' => 0.4, 'foo/*' => 0.2 ]
			],
		];
	}

	/**
	 * @dataProvider provideParseWeights
	 */
	public function testParseWeights( $header, $expected ) {
		$parser = new HttpAcceptParser();
		$actual = $parser->parseWeights( $header );

		$this->assertEquals( $expected, $actual ); // shouldn't be sensitive to order
	}

	public static function provideParseAccept() {
		return [
			[
				// Sort by decending q
				'test/123; q=0.5, test/456; q=0.8',
				[
					[
						'type' => 'test',
						'subtype' => '456',
						'q' => 0.8,
						'i' => 1,
						'params' => []
					],
					[
						'type' => 'test',
						'subtype' => '123',
						'q' => 0.5,
						'i' => 0,
						'params' => []
					],
				]
			],
			[
				// Sort by decending q, ascending order
				'test/123; q=0.5, test/789; q=0.8, test/456; q=0.8',
				[
					[
						'type' => 'test',
						'subtype' => '789',
						'q' => 0.8,
						'i' => 1,
						'params' => []
					],
					[
						'type' => 'test',
						'subtype' => '456',
						'q' => 0.8,
						'i' => 2,
						'params' => []
					],
					[
						'type' => 'test',
						'subtype' => '123',
						'q' => 0.5,
						'i' => 0,
						'params' => []
					]
				]
			],
			[
				// Test types and subtypes that contain non-alphanumeric characters
				'hi-ho/12.3; q=0.5, hi/ho+456; q=0.8',
				[
					[
						'type' => 'hi',
						'subtype' => 'ho+456',
						'q' => 0.8,
						'i' => 1,
						'params' => []
					],
					[
						'type' => 'hi-ho',
						'subtype' => '12.3',
						'q' => 0.5,
						'i' => 0,
						'params' => []
					]
				]
			],
			[
				// Test for params
				'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"',
				[
					[
						'type' => 'text',
						'subtype' => 'html',
						'q' => 1,
						'i' => 0,
						'params' => [
							'profile' => 'https://www.mediawiki.org/wiki/Specs/HTML/0.0.0'
						]
					]
				]
			],
			[
				// Incomplete q - T391867
				'test/123; q=0.5, test/789; q',
				[
					[
						'type' => 'test',
						'subtype' => '789',
						'q' => 1,
						'i' => 1,
						'params' => []
					],
					[
						'type' => 'test',
						'subtype' => '123',
						'q' => 0.5,
						'i' => 0,
						'params' => []
					],
				]
			],
		];
	}

	/**
	 * @dataProvider provideParseAccept
	 */
	public function testParseAccept( $header, $expected ) {
		$parser = new HttpAcceptParser();
		$actual = $parser->parseAccept( $header );
		$this->assertEquals( $expected, $actual );
	}

}