Open Monograph Press  3.3.0
MessageParserProvider.php
1 <?php
2 
4 
6 {
7  public function requestProvider()
8  {
9  $auth = base64_encode('michael:foo');
10 
11  return array(
12 
13  // Empty request
14  array('', false),
15 
16  // Converts casing of request. Does not require host header.
17  array("GET / HTTP/1.1\r\n\r\n", array(
18  'method' => 'GET',
19  'protocol' => 'HTTP',
20  'version' => '1.1',
21  'request_url' => array(
22  'scheme' => 'http',
23  'host' => '',
24  'port' => '',
25  'path' => '/',
26  'query' => ''
27  ),
28  'headers' => array(),
29  'body' => ''
30  )),
31  // Path and query string, multiple header values per header and case sensitive storage
32  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(
33  'method' => 'HEAD',
34  'protocol' => 'HTTP',
35  'version' => '1.0',
36  'request_url' => array(
37  'scheme' => 'http',
38  'host' => 'example.com',
39  'port' => '',
40  'path' => '/path',
41  'query' => 'query=foo'
42  ),
43  'headers' => array(
44  'Host' => 'example.com',
45  'X-Foo' => array('foo', 'foo', 'Baz'),
46  'x-foo' => 'Bar'
47  ),
48  'body' => ''
49  )),
50  // Includes a body
51  array("PUT / HTTP/1.0\r\nhost: example.com:443\r\nContent-Length: 4\r\n\r\ntest", array(
52  'method' => 'PUT',
53  'protocol' => 'HTTP',
54  'version' => '1.0',
55  'request_url' => array(
56  'scheme' => 'https',
57  'host' => 'example.com',
58  'port' => '443',
59  'path' => '/',
60  'query' => ''
61  ),
62  'headers' => array(
63  'host' => 'example.com:443',
64  'Content-Length' => '4'
65  ),
66  'body' => 'test'
67  )),
68  // Includes Authorization headers
69  array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nAuthorization: Basic {$auth}\r\n\r\n", array(
70  'method' => 'GET',
71  'protocol' => 'HTTP',
72  'version' => '1.1',
73  'request_url' => array(
74  'scheme' => 'http',
75  'host' => 'example.com',
76  'port' => '8080',
77  'path' => '/',
78  'query' => ''
79  ),
80  'headers' => array(
81  'Host' => 'example.com:8080',
82  'Authorization' => "Basic {$auth}"
83  ),
84  'body' => ''
85  )),
86  // Include authorization header
87  array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nauthorization: Basic {$auth}\r\n\r\n", array(
88  'method' => 'GET',
89  'protocol' => 'HTTP',
90  'version' => '1.1',
91  'request_url' => array(
92  'scheme' => 'http',
93  'host' => 'example.com',
94  'port' => '8080',
95  'path' => '/',
96  'query' => ''
97  ),
98  'headers' => array(
99  'Host' => 'example.com:8080',
100  'authorization' => "Basic {$auth}"
101  ),
102  'body' => ''
103  )),
104  );
105  }
106 
107  public function responseProvider()
108  {
109  return array(
110  // Empty request
111  array('', false),
112 
113  array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", array(
114  'protocol' => 'HTTP',
115  'version' => '1.1',
116  'code' => '200',
117  'reason_phrase' => 'OK',
118  'headers' => array(
119  'Content-Length' => 0
120  ),
121  'body' => ''
122  )),
123  array("HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\n\r\n", array(
124  'protocol' => 'HTTP',
125  'version' => '1.0',
126  'code' => '400',
127  'reason_phrase' => 'Bad Request',
128  'headers' => array(
129  'Content-Length' => 0
130  ),
131  'body' => ''
132  )),
133  array("HTTP/1.0 100 Continue\r\n\r\n", array(
134  'protocol' => 'HTTP',
135  'version' => '1.0',
136  'code' => '100',
137  'reason_phrase' => 'Continue',
138  'headers' => array(),
139  'body' => ''
140  )),
141  array("HTTP/1.1 204 No Content\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\n\r\n", array(
142  'protocol' => 'HTTP',
143  'version' => '1.1',
144  'code' => '204',
145  'reason_phrase' => 'No Content',
146  'headers' => array(
147  'X-Foo' => array('foo', 'foo'),
148  'x-foo' => 'Bar'
149  ),
150  'body' => ''
151  )),
152  array("HTTP/1.1 200 Ok that is great!\r\nContent-Length: 4\r\n\r\nTest", array(
153  'protocol' => 'HTTP',
154  'version' => '1.1',
155  'code' => '200',
156  'reason_phrase' => 'Ok that is great!',
157  'headers' => array(
158  'Content-Length' => 4
159  ),
160  'body' => 'Test'
161  )),
162  );
163  }
164 
165  public function compareRequestResults($result, $expected)
166  {
167  if (!$result) {
168  $this->assertFalse($expected);
169  return;
170  }
171 
172  $this->assertEquals($result['method'], $expected['method']);
173  $this->assertEquals($result['protocol'], $expected['protocol']);
174  $this->assertEquals($result['version'], $expected['version']);
175  $this->assertEquals($result['request_url'], $expected['request_url']);
176  $this->assertEquals($result['body'], $expected['body']);
177  $this->compareHttpHeaders($result['headers'], $expected['headers']);
178  }
179 
180  public function compareResponseResults($result, $expected)
181  {
182  if (!$result) {
183  $this->assertFalse($expected);
184  return;
185  }
186 
187  $this->assertEquals($result['protocol'], $expected['protocol']);
188  $this->assertEquals($result['version'], $expected['version']);
189  $this->assertEquals($result['code'], $expected['code']);
190  $this->assertEquals($result['reason_phrase'], $expected['reason_phrase']);
191  $this->assertEquals($result['body'], $expected['body']);
192  $this->compareHttpHeaders($result['headers'], $expected['headers']);
193  }
194 
195  protected function normalizeHeaders($headers)
196  {
197  $normalized = array();
198  foreach ($headers as $key => $value) {
199  $key = strtolower($key);
200  if (!isset($normalized[$key])) {
201  $normalized[$key] = $value;
202  } elseif (!is_array($normalized[$key])) {
203  $normalized[$key] = array($value);
204  } else {
205  $normalized[$key][] = $value;
206  }
207  }
208 
209  foreach ($normalized as $key => &$value) {
210  if (is_array($value)) {
211  sort($value);
212  }
213  }
214 
215  return $normalized;
216  }
217 
218  public function compareHttpHeaders($result, $expected)
219  {
220  // Aggregate all headers case-insensitively
221  $result = $this->normalizeHeaders($result);
222  $expected = $this->normalizeHeaders($expected);
223  $this->assertEquals($result, $expected);
224  }
225 }
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Parser\Message\MessageParserProvider\compareRequestResults
compareRequestResults($result, $expected)
Definition: MessageParserProvider.php:165
Guzzle\Tests\Parser\Message\MessageParserProvider\normalizeHeaders
normalizeHeaders($headers)
Definition: MessageParserProvider.php:195
Guzzle\Tests\Parser\Message\MessageParserProvider\requestProvider
requestProvider()
Definition: MessageParserProvider.php:7
Guzzle\Tests\Parser\Message\MessageParserProvider\compareResponseResults
compareResponseResults($result, $expected)
Definition: MessageParserProvider.php:180
Guzzle\Tests\Parser\Message\MessageParserProvider\compareHttpHeaders
compareHttpHeaders($result, $expected)
Definition: MessageParserProvider.php:218
Guzzle\Tests\Parser\Message\MessageParserProvider\responseProvider
responseProvider()
Definition: MessageParserProvider.php:107
Guzzle\Tests\Parser\Message
Definition: MessageParserProvider.php:3
Guzzle\Tests\Parser\Message\MessageParserProvider
Definition: MessageParserProvider.php:5