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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
<?php
namespace GuzzleHttp\Tests\Message;
use GuzzleHttp\Message\MessageParser;
/**
* @covers \GuzzleHttp\Message\MessageParser
*/
class MessageParserTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider requestProvider
*/
public function testParsesRequests($message, $parts)
{
$parser = new MessageParser();
$this->compareRequestResults($parts, $parser->parseRequest($message));
}
/**
* @dataProvider responseProvider
*/
public function testParsesResponses($message, $parts)
{
$parser = new MessageParser();
$this->compareResponseResults($parts, $parser->parseResponse($message));
}
public function testParsesRequestsWithMissingProtocol()
{
$parser = new MessageParser();
$parts = $parser->parseRequest("GET /\r\nHost: Foo.com\r\n\r\n");
$this->assertEquals('GET', $parts['method']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
public function testParsesRequestsWithMissingVersion()
{
$parser = new MessageParser();
$parts = $parser->parseRequest("GET / HTTP\r\nHost: Foo.com\r\n\r\n");
$this->assertEquals('GET', $parts['method']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
public function testParsesResponsesWithMissingReasonPhrase()
{
$parser = new MessageParser();
$parts = $parser->parseResponse("HTTP/1.1 200\r\n\r\n");
$this->assertEquals('200', $parts['code']);
$this->assertEquals('', $parts['reason_phrase']);
$this->assertEquals('HTTP', $parts['protocol']);
$this->assertEquals('1.1', $parts['protocol_version']);
}
public function requestProvider()
{
$auth = base64_encode('michael:foo');
return array(
// Empty request
array('', false),
// Converts casing of request. Does not require host header.
array("GET / HTTP/1.1\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => '',
'port' => '',
'path' => '/',
'query' => ''
),
'headers' => array(),
'body' => ''
)),
// Path and query string, multiple header values per header and case sensitive storage
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(
'method' => 'HEAD',
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '',
'path' => '/path',
'query' => 'query=foo'
),
'headers' => array(
'Host' => 'example.com',
'X-Foo' => array('foo', 'foo', 'Baz'),
'x-foo' => 'Bar'
),
'body' => ''
)),
// Includes a body
array("PUT / HTTP/1.0\r\nhost: example.com:443\r\nContent-Length: 4\r\n\r\ntest", array(
'method' => 'PUT',
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'request_url' => array(
'scheme' => 'https',
'host' => 'example.com',
'port' => '443',
'path' => '/',
'query' => ''
),
'headers' => array(
'host' => 'example.com:443',
'Content-Length' => '4'
),
'body' => 'test'
)),
// Includes Authorization headers
array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nAuthorization: Basic {$auth}\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '8080',
'path' => '/',
'query' => ''
),
'headers' => array(
'Host' => 'example.com:8080',
'Authorization' => "Basic {$auth}"
),
'body' => ''
)),
// Include authorization header
array("GET / HTTP/1.1\r\nHost: example.com:8080\r\nauthorization: Basic {$auth}\r\n\r\n", array(
'method' => 'GET',
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'request_url' => array(
'scheme' => 'http',
'host' => 'example.com',
'port' => '8080',
'path' => '/',
'query' => ''
),
'headers' => array(
'Host' => 'example.com:8080',
'authorization' => "Basic {$auth}"
),
'body' => ''
)),
);
}
public function responseProvider()
{
return array(
// Empty request
array('', false),
array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '200',
'reason_phrase' => 'OK',
'headers' => array(
'Content-Length' => 0
),
'body' => ''
)),
array("HTTP/1.0 400 Bad Request\r\nContent-Length: 0\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'code' => '400',
'reason_phrase' => 'Bad Request',
'headers' => array(
'Content-Length' => 0
),
'body' => ''
)),
array("HTTP/1.0 100 Continue\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.0',
'code' => '100',
'reason_phrase' => 'Continue',
'headers' => array(),
'body' => ''
)),
array("HTTP/1.1 204 No Content\r\nX-Foo: foo\r\nx-foo: Bar\r\nX-Foo: foo\r\n\r\n", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '204',
'reason_phrase' => 'No Content',
'headers' => array(
'X-Foo' => array('foo', 'foo'),
'x-foo' => 'Bar'
),
'body' => ''
)),
array("HTTP/1.1 200 Ok that is great!\r\nContent-Length: 4\r\n\r\nTest", array(
'protocol' => 'HTTP',
'protocol_version' => '1.1',
'code' => '200',
'reason_phrase' => 'Ok that is great!',
'headers' => array(
'Content-Length' => 4
),
'body' => 'Test'
)),
);
}
public function compareRequestResults($result, $expected)
{
if (!$result) {
$this->assertFalse($expected);
return;
}
$this->assertEquals($result['method'], $expected['method']);
$this->assertEquals($result['protocol'], $expected['protocol']);
$this->assertEquals($result['protocol_version'], $expected['protocol_version']);
$this->assertEquals($result['request_url'], $expected['request_url']);
$this->assertEquals($result['body'], $expected['body']);
$this->compareHttpHeaders($result['headers'], $expected['headers']);
}
public function compareResponseResults($result, $expected)
{
if (!$result) {
$this->assertFalse($expected);
return;
}
$this->assertEquals($result['protocol'], $expected['protocol']);
$this->assertEquals($result['protocol_version'], $expected['protocol_version']);
$this->assertEquals($result['code'], $expected['code']);
$this->assertEquals($result['reason_phrase'], $expected['reason_phrase']);
$this->assertEquals($result['body'], $expected['body']);
$this->compareHttpHeaders($result['headers'], $expected['headers']);
}
protected function normalizeHeaders($headers)
{
$normalized = array();
foreach ($headers as $key => $value) {
$key = strtolower($key);
if (!isset($normalized[$key])) {
$normalized[$key] = $value;
} elseif (!is_array($normalized[$key])) {
$normalized[$key] = array($value);
} else {
$normalized[$key][] = $value;
}
}
foreach ($normalized as $key => &$value) {
if (is_array($value)) {
sort($value);
}
}
return $normalized;
}
public function compareHttpHeaders($result, $expected)
{
// Aggregate all headers case-insensitively
$result = $this->normalizeHeaders($result);
$expected = $this->normalizeHeaders($expected);
$this->assertEquals($result, $expected);
}
}
|