File: HttpAcceptParserTest.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (151 lines) | stat: -rw-r--r-- 2,980 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
<?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
				'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 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'
						]
					]
				]
			],
		];
	}

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

}