scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Message; |
||
4 | |||
5 | use GuzzleHttp\Message\MessageParser; |
||
6 | |||
7 | /** |
||
8 | * @covers \GuzzleHttp\Message\MessageParser |
||
9 | */ |
||
10 | class MessageParserTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @dataProvider requestProvider |
||
14 | */ |
||
15 | public function testParsesRequests($message, $parts) |
||
16 | { |
||
17 | $parser = new MessageParser(); |
||
18 | $this->compareRequestResults($parts, $parser->parseRequest($message)); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @dataProvider responseProvider |
||
23 | */ |
||
24 | public function testParsesResponses($message, $parts) |
||
25 | { |
||
26 | $parser = new MessageParser(); |
||
27 | $this->compareResponseResults($parts, $parser->parseResponse($message)); |
||
28 | } |
||
29 | |||
30 | public function testParsesRequestsWithMissingProtocol() |
||
31 | { |
||
32 | $parser = new MessageParser(); |
||
33 | $parts = $parser->parseRequest("GET /\r\nHost: Foo.com\r\n\r\n"); |
||
34 | $this->assertEquals('GET', $parts['method']); |
||
35 | $this->assertEquals('HTTP', $parts['protocol']); |
||
36 | $this->assertEquals('1.1', $parts['protocol_version']); |
||
37 | } |
||
38 | |||
39 | public function testParsesRequestsWithMissingVersion() |
||
40 | { |
||
41 | $parser = new MessageParser(); |
||
42 | $parts = $parser->parseRequest("GET / HTTP\r\nHost: Foo.com\r\n\r\n"); |
||
43 | $this->assertEquals('GET', $parts['method']); |
||
44 | $this->assertEquals('HTTP', $parts['protocol']); |
||
45 | $this->assertEquals('1.1', $parts['protocol_version']); |
||
46 | } |
||
47 | |||
48 | public function testParsesResponsesWithMissingReasonPhrase() |
||
49 | { |
||
50 | $parser = new MessageParser(); |
||
51 | $parts = $parser->parseResponse("HTTP/1.1 200\r\n\r\n"); |
||
52 | $this->assertEquals('200', $parts['code']); |
||
53 | $this->assertEquals('', $parts['reason_phrase']); |
||
54 | $this->assertEquals('HTTP', $parts['protocol']); |
||
55 | $this->assertEquals('1.1', $parts['protocol_version']); |
||
56 | } |
||
57 | |||
58 | public function requestProvider() |
||
59 | { |
||
60 | $auth = base64_encode('michael:foo'); |
||
61 | |||
62 | return array( |
||
63 | |||
64 | // Empty request |
||
65 | array('', false), |
||
66 | |||
67 | // Converts casing of request. Does not require host header. |
||
68 | array("GET / HTTP/1.1\r\n\r\n", array( |
||
69 | 'method' => 'GET', |
||
70 | 'protocol' => 'HTTP', |
||
71 | 'protocol_version' => '1.1', |
||
72 | 'request_url' => array( |
||
73 | 'scheme' => 'http', |
||
74 | 'host' => '', |
||
75 | 'port' => '', |
||
76 | 'path' => '/', |
||
77 | 'query' => '' |
||
78 | ), |
||
79 | 'headers' => array(), |
||
80 | 'body' => '' |
||
81 | )), |
||
82 | // Path and query string, multiple header values per header and case sensitive storage |
||
83 | array("HEAD /path?query=foo HTTP/1.0\r\nHost: example.com\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\nX-Foo: Baz\r\n\r\n", array( |
||
84 | 'method' => 'HEAD', |
||
85 | 'protocol' => 'HTTP', |
||
86 | 'protocol_version' => '1.0', |
||
87 | 'request_url' => array( |
||
88 | 'scheme' => 'http', |
||
89 | 'host' => 'example.com', |
||
90 | 'port' => '', |
||
91 | 'path' => '/path', |
||
92 | 'query' => 'query=foo' |
||
93 | ), |
||
94 | 'headers' => array( |
||
95 | 'Host' => 'example.com', |
||
96 | 'X-Foo' => array('foo', 'foo', 'Baz'), |
||
97 | 'x-foo' => 'Bar' |
||
98 | ), |
||
99 | 'body' => '' |
||
100 | )), |
||
101 | // Includes a body |
||
102 | array("PUT / HTTP/1.0\r\nhost: example.com:443\r\nContent-Length: 4\r\n\r\ntest", array( |
||
103 | 'method' => 'PUT', |
||
104 | 'protocol' => 'HTTP', |
||
105 | 'protocol_version' => '1.0', |
||
106 | 'request_url' => array( |
||
107 | 'scheme' => 'https', |
||
108 | 'host' => 'example.com', |
||
109 | 'port' => '443', |
||
110 | 'path' => '/', |
||
111 | 'query' => '' |
||
112 | ), |
||
113 | 'headers' => array( |
||
114 | 'host' => 'example.com:443', |
||
115 | 'Content-Length' => '4' |
||
116 | ), |
||
117 | 'body' => 'test' |
||
118 | )), |
||
119 | // Includes Authorization headers |
||
120 | array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nAuthorization: Basic {$auth}\r\n\r\n", array( |
||
121 | 'method' => 'GET', |
||
122 | 'protocol' => 'HTTP', |
||
123 | 'protocol_version' => '1.1', |
||
124 | 'request_url' => array( |
||
125 | 'scheme' => 'http', |
||
126 | 'host' => 'example.com', |
||
127 | 'port' => '8080', |
||
128 | 'path' => '/', |
||
129 | 'query' => '' |
||
130 | ), |
||
131 | 'headers' => array( |
||
132 | 'Host' => 'example.com:8080', |
||
133 | 'Authorization' => "Basic {$auth}" |
||
134 | ), |
||
135 | 'body' => '' |
||
136 | )), |
||
137 | // Include authorization header |
||
138 | array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nauthorization: Basic {$auth}\r\n\r\n", array( |
||
139 | 'method' => 'GET', |
||
140 | 'protocol' => 'HTTP', |
||
141 | 'protocol_version' => '1.1', |
||
142 | 'request_url' => array( |
||
143 | 'scheme' => 'http', |
||
144 | 'host' => 'example.com', |
||
145 | 'port' => '8080', |
||
146 | 'path' => '/', |
||
147 | 'query' => '' |
||
148 | ), |
||
149 | 'headers' => array( |
||
150 | 'Host' => 'example.com:8080', |
||
151 | 'authorization' => "Basic {$auth}" |
||
152 | ), |
||
153 | 'body' => '' |
||
154 | )), |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | public function responseProvider() |
||
159 | { |
||
160 | return array( |
||
161 | // Empty request |
||
162 | array('', false), |
||
163 | |||
164 | array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", array( |
||
165 | 'protocol' => 'HTTP', |
||
166 | 'protocol_version' => '1.1', |
||
167 | 'code' => '200', |
||
168 | 'reason_phrase' => 'OK', |
||
169 | 'headers' => array( |
||
170 | 'Content-Length' => 0 |
||
171 | ), |
||
172 | 'body' => '' |
||
173 | )), |
||
174 | array("HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\n\r\n", array( |
||
175 | 'protocol' => 'HTTP', |
||
176 | 'protocol_version' => '1.0', |
||
177 | 'code' => '400', |
||
178 | 'reason_phrase' => 'Bad Request', |
||
179 | 'headers' => array( |
||
180 | 'Content-Length' => 0 |
||
181 | ), |
||
182 | 'body' => '' |
||
183 | )), |
||
184 | array("HTTP/1.0 100 Continue\r\n\r\n", array( |
||
185 | 'protocol' => 'HTTP', |
||
186 | 'protocol_version' => '1.0', |
||
187 | 'code' => '100', |
||
188 | 'reason_phrase' => 'Continue', |
||
189 | 'headers' => array(), |
||
190 | 'body' => '' |
||
191 | )), |
||
192 | array("HTTP/1.1 204 No Content\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\n\r\n", array( |
||
193 | 'protocol' => 'HTTP', |
||
194 | 'protocol_version' => '1.1', |
||
195 | 'code' => '204', |
||
196 | 'reason_phrase' => 'No Content', |
||
197 | 'headers' => array( |
||
198 | 'X-Foo' => array('foo', 'foo'), |
||
199 | 'x-foo' => 'Bar' |
||
200 | ), |
||
201 | 'body' => '' |
||
202 | )), |
||
203 | array("HTTP/1.1 200 Ok that is great!\r\nContent-Length: 4\r\n\r\nTest", array( |
||
204 | 'protocol' => 'HTTP', |
||
205 | 'protocol_version' => '1.1', |
||
206 | 'code' => '200', |
||
207 | 'reason_phrase' => 'Ok that is great!', |
||
208 | 'headers' => array( |
||
209 | 'Content-Length' => 4 |
||
210 | ), |
||
211 | 'body' => 'Test' |
||
212 | )), |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | public function compareRequestResults($result, $expected) |
||
217 | { |
||
218 | if (!$result) { |
||
219 | $this->assertFalse($expected); |
||
220 | return; |
||
221 | } |
||
222 | |||
223 | $this->assertEquals($result['method'], $expected['method']); |
||
224 | $this->assertEquals($result['protocol'], $expected['protocol']); |
||
225 | $this->assertEquals($result['protocol_version'], $expected['protocol_version']); |
||
226 | $this->assertEquals($result['request_url'], $expected['request_url']); |
||
227 | $this->assertEquals($result['body'], $expected['body']); |
||
228 | $this->compareHttpHeaders($result['headers'], $expected['headers']); |
||
229 | } |
||
230 | |||
231 | public function compareResponseResults($result, $expected) |
||
232 | { |
||
233 | if (!$result) { |
||
234 | $this->assertFalse($expected); |
||
235 | return; |
||
236 | } |
||
237 | |||
238 | $this->assertEquals($result['protocol'], $expected['protocol']); |
||
239 | $this->assertEquals($result['protocol_version'], $expected['protocol_version']); |
||
240 | $this->assertEquals($result['code'], $expected['code']); |
||
241 | $this->assertEquals($result['reason_phrase'], $expected['reason_phrase']); |
||
242 | $this->assertEquals($result['body'], $expected['body']); |
||
243 | $this->compareHttpHeaders($result['headers'], $expected['headers']); |
||
244 | } |
||
245 | |||
246 | protected function normalizeHeaders($headers) |
||
247 | { |
||
248 | $normalized = array(); |
||
249 | foreach ($headers as $key => $value) { |
||
250 | $key = strtolower($key); |
||
251 | if (!isset($normalized[$key])) { |
||
252 | $normalized[$key] = $value; |
||
253 | } elseif (!is_array($normalized[$key])) { |
||
254 | $normalized[$key] = array($value); |
||
255 | } else { |
||
256 | $normalized[$key][] = $value; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | foreach ($normalized as $key => &$value) { |
||
261 | if (is_array($value)) { |
||
262 | sort($value); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | return $normalized; |
||
267 | } |
||
268 | |||
269 | public function compareHttpHeaders($result, $expected) |
||
270 | { |
||
271 | // Aggregate all headers case-insensitively |
||
272 | $result = $this->normalizeHeaders($result); |
||
273 | $expected = $this->normalizeHeaders($expected); |
||
274 | $this->assertEquals($result, $expected); |
||
275 | } |
||
276 | } |