| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Tests\Psr7; |
| 4 |
|
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
use GuzzleHttp\Psr7\FnStream; |
| 7 |
use GuzzleHttp\Psr7\NoSeekStream; |
| 8 |
use GuzzleHttp\Psr7\Stream; |
| 9 |
|
| 10 |
class FunctionsTest extends BaseTest |
| 11 |
{ |
| 12 |
public function testCopiesToString() |
| 13 |
{ |
| 14 |
$s = Psr7\stream_for('foobaz'); |
| 15 |
$this->assertEquals('foobaz', Psr7\copy_to_string($s)); |
| 16 |
$s->seek(0); |
| 17 |
$this->assertEquals('foo', Psr7\copy_to_string($s, 3)); |
| 18 |
$this->assertEquals('baz', Psr7\copy_to_string($s, 3)); |
| 19 |
$this->assertEquals('', Psr7\copy_to_string($s)); |
| 20 |
} |
| 21 |
|
| 22 |
public function testCopiesToStringStopsWhenReadFails() |
| 23 |
{ |
| 24 |
$s1 = Psr7\stream_for('foobaz'); |
| 25 |
$s1 = FnStream::decorate($s1, [ |
| 26 |
'read' => function () { |
| 27 |
return ''; |
| 28 |
}, |
| 29 |
]); |
| 30 |
$result = Psr7\copy_to_string($s1); |
| 31 |
$this->assertEquals('', $result); |
| 32 |
} |
| 33 |
|
| 34 |
public function testCopiesToStream() |
| 35 |
{ |
| 36 |
$s1 = Psr7\stream_for('foobaz'); |
| 37 |
$s2 = Psr7\stream_for(''); |
| 38 |
Psr7\copy_to_stream($s1, $s2); |
| 39 |
$this->assertEquals('foobaz', (string)$s2); |
| 40 |
$s2 = Psr7\stream_for(''); |
| 41 |
$s1->seek(0); |
| 42 |
Psr7\copy_to_stream($s1, $s2, 3); |
| 43 |
$this->assertEquals('foo', (string)$s2); |
| 44 |
Psr7\copy_to_stream($s1, $s2, 3); |
| 45 |
$this->assertEquals('foobaz', (string)$s2); |
| 46 |
} |
| 47 |
|
| 48 |
public function testStopsCopyToStreamWhenWriteFails() |
| 49 |
{ |
| 50 |
$s1 = Psr7\stream_for('foobaz'); |
| 51 |
$s2 = Psr7\stream_for(''); |
| 52 |
$s2 = FnStream::decorate($s2, [ |
| 53 |
'write' => function () { |
| 54 |
return 0; |
| 55 |
}, |
| 56 |
]); |
| 57 |
Psr7\copy_to_stream($s1, $s2); |
| 58 |
$this->assertEquals('', (string)$s2); |
| 59 |
} |
| 60 |
|
| 61 |
public function testStopsCopyToSteamWhenWriteFailsWithMaxLen() |
| 62 |
{ |
| 63 |
$s1 = Psr7\stream_for('foobaz'); |
| 64 |
$s2 = Psr7\stream_for(''); |
| 65 |
$s2 = FnStream::decorate($s2, [ |
| 66 |
'write' => function () { |
| 67 |
return 0; |
| 68 |
}, |
| 69 |
]); |
| 70 |
Psr7\copy_to_stream($s1, $s2, 10); |
| 71 |
$this->assertEquals('', (string)$s2); |
| 72 |
} |
| 73 |
|
| 74 |
public function testCopyToStreamReadsInChunksInsteadOfAllInMemory() |
| 75 |
{ |
| 76 |
$sizes = []; |
| 77 |
$s1 = new Psr7\FnStream([ |
| 78 |
'eof' => function () { |
| 79 |
return false; |
| 80 |
}, |
| 81 |
'read' => function ($size) use (&$sizes) { |
| 82 |
$sizes[] = $size; |
| 83 |
return str_repeat('.', $size); |
| 84 |
}, |
| 85 |
]); |
| 86 |
$s2 = Psr7\stream_for(''); |
| 87 |
Psr7\copy_to_stream($s1, $s2, 16394); |
| 88 |
$s2->seek(0); |
| 89 |
$this->assertEquals(16394, strlen($s2->getContents())); |
| 90 |
$this->assertEquals(8192, $sizes[0]); |
| 91 |
$this->assertEquals(8192, $sizes[1]); |
| 92 |
$this->assertEquals(10, $sizes[2]); |
| 93 |
} |
| 94 |
|
| 95 |
public function testStopsCopyToSteamWhenReadFailsWithMaxLen() |
| 96 |
{ |
| 97 |
$s1 = Psr7\stream_for('foobaz'); |
| 98 |
$s1 = FnStream::decorate($s1, [ |
| 99 |
'read' => function () { |
| 100 |
return ''; |
| 101 |
}, |
| 102 |
]); |
| 103 |
$s2 = Psr7\stream_for(''); |
| 104 |
Psr7\copy_to_stream($s1, $s2, 10); |
| 105 |
$this->assertEquals('', (string)$s2); |
| 106 |
} |
| 107 |
|
| 108 |
public function testReadsLines() |
| 109 |
{ |
| 110 |
$s = Psr7\stream_for("foo\nbaz\nbar"); |
| 111 |
$this->assertEquals("foo\n", Psr7\readline($s)); |
| 112 |
$this->assertEquals("baz\n", Psr7\readline($s)); |
| 113 |
$this->assertEquals('bar', Psr7\readline($s)); |
| 114 |
} |
| 115 |
|
| 116 |
public function testReadsLinesUpToMaxLength() |
| 117 |
{ |
| 118 |
$s = Psr7\stream_for("12345\n"); |
| 119 |
$this->assertEquals('123', Psr7\readline($s, 4)); |
| 120 |
$this->assertEquals("45\n", Psr7\readline($s)); |
| 121 |
} |
| 122 |
|
| 123 |
public function testReadLinesEof() |
| 124 |
{ |
| 125 |
// Should return empty string on EOF |
| 126 |
$s = Psr7\stream_for("foo\nbar"); |
| 127 |
while (!$s->eof()) { |
| 128 |
Psr7\readline($s); |
| 129 |
} |
| 130 |
$this->assertSame('', Psr7\readline($s)); |
| 131 |
} |
| 132 |
|
| 133 |
public function testReadsLineUntilFalseReturnedFromRead() |
| 134 |
{ |
| 135 |
$s = $this->getMockBuilder('GuzzleHttp\Psr7\Stream') |
| 136 |
->setMethods(['read', 'eof']) |
| 137 |
->disableOriginalConstructor() |
| 138 |
->getMock(); |
| 139 |
$s->expects($this->exactly(2)) |
| 140 |
->method('read') |
| 141 |
->will($this->returnCallback(function () { |
| 142 |
static $c = false; |
| 143 |
if ($c) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
$c = true; |
| 147 |
return 'h'; |
| 148 |
})); |
| 149 |
$s->expects($this->exactly(2)) |
| 150 |
->method('eof') |
| 151 |
->will($this->returnValue(false)); |
| 152 |
$this->assertEquals('h', Psr7\readline($s)); |
| 153 |
} |
| 154 |
|
| 155 |
public function testCalculatesHash() |
| 156 |
{ |
| 157 |
$s = Psr7\stream_for('foobazbar'); |
| 158 |
$this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5')); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @expectedException \RuntimeException |
| 163 |
*/ |
| 164 |
public function testCalculatesHashThrowsWhenSeekFails() |
| 165 |
{ |
| 166 |
$s = new NoSeekStream(Psr7\stream_for('foobazbar')); |
| 167 |
$s->read(2); |
| 168 |
Psr7\hash($s, 'md5'); |
| 169 |
} |
| 170 |
|
| 171 |
public function testCalculatesHashSeeksToOriginalPosition() |
| 172 |
{ |
| 173 |
$s = Psr7\stream_for('foobazbar'); |
| 174 |
$s->seek(4); |
| 175 |
$this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5')); |
| 176 |
$this->assertEquals(4, $s->tell()); |
| 177 |
} |
| 178 |
|
| 179 |
public function testOpensFilesSuccessfully() |
| 180 |
{ |
| 181 |
$r = Psr7\try_fopen(__FILE__, 'r'); |
| 182 |
$this->assertInternalType('resource', $r); |
| 183 |
fclose($r); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @expectedException \RuntimeException |
| 188 |
* @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r |
| 189 |
*/ |
| 190 |
public function testThrowsExceptionNotWarning() |
| 191 |
{ |
| 192 |
Psr7\try_fopen('/path/to/does/not/exist', 'r'); |
| 193 |
} |
| 194 |
|
| 195 |
public function parseQueryProvider() |
| 196 |
{ |
| 197 |
return [ |
| 198 |
// Does not need to parse when the string is empty |
| 199 |
['', []], |
| 200 |
// Can parse mult-values items |
| 201 |
['q=a&q=b', ['q' => ['a', 'b']]], |
| 202 |
// Can parse multi-valued items that use numeric indices |
| 203 |
['q[0]=a&q[1]=b', ['q[0]' => 'a', 'q[1]' => 'b']], |
| 204 |
// Can parse duplicates and does not include numeric indices |
| 205 |
['q[]=a&q[]=b', ['q[]' => ['a', 'b']]], |
| 206 |
// Ensures that the value of "q" is an array even though one value |
| 207 |
['q[]=a', ['q[]' => 'a']], |
| 208 |
// Does not modify "." to "_" like PHP's parse_str() |
| 209 |
['q.a=a&q.b=b', ['q.a' => 'a', 'q.b' => 'b']], |
| 210 |
// Can decode %20 to " " |
| 211 |
['q%20a=a%20b', ['q a' => 'a b']], |
| 212 |
// Can parse funky strings with no values by assigning each to null |
| 213 |
['q&a', ['q' => null, 'a' => null]], |
| 214 |
// Does not strip trailing equal signs |
| 215 |
['data=abc=', ['data' => 'abc=']], |
| 216 |
// Can store duplicates without affecting other values |
| 217 |
['foo=a&foo=b&?µ=c', ['foo' => ['a', 'b'], '?µ' => 'c']], |
| 218 |
// Sets value to null when no "=" is present |
| 219 |
['foo', ['foo' => null]], |
| 220 |
// Preserves "0" keys. |
| 221 |
['0', ['0' => null]], |
| 222 |
// Sets the value to an empty string when "=" is present |
| 223 |
['0=', ['0' => '']], |
| 224 |
// Preserves falsey keys |
| 225 |
['var=0', ['var' => '0']], |
| 226 |
['a[b][c]=1&a[b][c]=2', ['a[b][c]' => ['1', '2']]], |
| 227 |
['a[b]=c&a[d]=e', ['a[b]' => 'c', 'a[d]' => 'e']], |
| 228 |
// Ensure it doesn't leave things behind with repeated values |
| 229 |
// Can parse mult-values items |
| 230 |
['q=a&q=b&q=c', ['q' => ['a', 'b', 'c']]], |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @dataProvider parseQueryProvider |
| 236 |
*/ |
| 237 |
public function testParsesQueries($input, $output) |
| 238 |
{ |
| 239 |
$result = Psr7\parse_query($input); |
| 240 |
$this->assertSame($output, $result); |
| 241 |
} |
| 242 |
|
| 243 |
public function testDoesNotDecode() |
| 244 |
{ |
| 245 |
$str = 'foo%20=bar'; |
| 246 |
$data = Psr7\parse_query($str, false); |
| 247 |
$this->assertEquals(['foo%20' => 'bar'], $data); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* @dataProvider parseQueryProvider |
| 252 |
*/ |
| 253 |
public function testParsesAndBuildsQueries($input) |
| 254 |
{ |
| 255 |
$result = Psr7\parse_query($input, false); |
| 256 |
$this->assertSame($input, Psr7\build_query($result, false)); |
| 257 |
} |
| 258 |
|
| 259 |
public function testEncodesWithRfc1738() |
| 260 |
{ |
| 261 |
$str = Psr7\build_query(['foo bar' => 'baz+'], PHP_QUERY_RFC1738); |
| 262 |
$this->assertEquals('foo+bar=baz%2B', $str); |
| 263 |
} |
| 264 |
|
| 265 |
public function testEncodesWithRfc3986() |
| 266 |
{ |
| 267 |
$str = Psr7\build_query(['foo bar' => 'baz+'], PHP_QUERY_RFC3986); |
| 268 |
$this->assertEquals('foo%20bar=baz%2B', $str); |
| 269 |
} |
| 270 |
|
| 271 |
public function testDoesNotEncode() |
| 272 |
{ |
| 273 |
$str = Psr7\build_query(['foo bar' => 'baz+'], false); |
| 274 |
$this->assertEquals('foo bar=baz+', $str); |
| 275 |
} |
| 276 |
|
| 277 |
public function testCanControlDecodingType() |
| 278 |
{ |
| 279 |
$result = Psr7\parse_query('var=foo+bar', PHP_QUERY_RFC3986); |
| 280 |
$this->assertEquals('foo+bar', $result['var']); |
| 281 |
$result = Psr7\parse_query('var=foo+bar', PHP_QUERY_RFC1738); |
| 282 |
$this->assertEquals('foo bar', $result['var']); |
| 283 |
} |
| 284 |
|
| 285 |
public function testParsesRequestMessages() |
| 286 |
{ |
| 287 |
$req = "GET /abc HTTP/1.0\r\nHost: foo.com\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest"; |
| 288 |
$request = Psr7\parse_request($req); |
| 289 |
$this->assertEquals('GET', $request->getMethod()); |
| 290 |
$this->assertEquals('/abc', $request->getRequestTarget()); |
| 291 |
$this->assertEquals('1.0', $request->getProtocolVersion()); |
| 292 |
$this->assertEquals('foo.com', $request->getHeaderLine('Host')); |
| 293 |
$this->assertEquals('Bar', $request->getHeaderLine('Foo')); |
| 294 |
$this->assertEquals('Bam, Qux', $request->getHeaderLine('Baz')); |
| 295 |
$this->assertEquals('Test', (string)$request->getBody()); |
| 296 |
$this->assertEquals('http://foo.com/abc', (string)$request->getUri()); |
| 297 |
} |
| 298 |
|
| 299 |
public function testParsesRequestMessagesWithHttpsScheme() |
| 300 |
{ |
| 301 |
$req = "PUT /abc?baz=bar HTTP/1.1\r\nHost: foo.com:443\r\n\r\n"; |
| 302 |
$request = Psr7\parse_request($req); |
| 303 |
$this->assertEquals('PUT', $request->getMethod()); |
| 304 |
$this->assertEquals('/abc?baz=bar', $request->getRequestTarget()); |
| 305 |
$this->assertEquals('1.1', $request->getProtocolVersion()); |
| 306 |
$this->assertEquals('foo.com:443', $request->getHeaderLine('Host')); |
| 307 |
$this->assertEquals('', (string)$request->getBody()); |
| 308 |
$this->assertEquals('https://foo.com/abc?baz=bar', (string)$request->getUri()); |
| 309 |
} |
| 310 |
|
| 311 |
public function testParsesRequestMessagesWithUriWhenHostIsNotFirst() |
| 312 |
{ |
| 313 |
$req = "PUT / HTTP/1.1\r\nFoo: Bar\r\nHost: foo.com\r\n\r\n"; |
| 314 |
$request = Psr7\parse_request($req); |
| 315 |
$this->assertEquals('PUT', $request->getMethod()); |
| 316 |
$this->assertEquals('/', $request->getRequestTarget()); |
| 317 |
$this->assertEquals('http://foo.com/', (string)$request->getUri()); |
| 318 |
} |
| 319 |
|
| 320 |
public function testParsesRequestMessagesWithFullUri() |
| 321 |
{ |
| 322 |
$req = "GET https://www.google.com:443/search?q=foobar HTTP/1.1\r\nHost: www.google.com\r\n\r\n"; |
| 323 |
$request = Psr7\parse_request($req); |
| 324 |
$this->assertEquals('GET', $request->getMethod()); |
| 325 |
$this->assertEquals('https://www.google.com:443/search?q=foobar', $request->getRequestTarget()); |
| 326 |
$this->assertEquals('1.1', $request->getProtocolVersion()); |
| 327 |
$this->assertEquals('www.google.com', $request->getHeaderLine('Host')); |
| 328 |
$this->assertEquals('', (string)$request->getBody()); |
| 329 |
$this->assertEquals('https://www.google.com/search?q=foobar', (string)$request->getUri()); |
| 330 |
} |
| 331 |
|
| 332 |
public function testParsesRequestMessagesWithCustomMethod() |
| 333 |
{ |
| 334 |
$req = "GET_DATA / HTTP/1.1\r\nFoo: Bar\r\nHost: foo.com\r\n\r\n"; |
| 335 |
$request = Psr7\parse_request($req); |
| 336 |
$this->assertEquals('GET_DATA', $request->getMethod()); |
| 337 |
} |
| 338 |
|
| 339 |
public function testParsesRequestMessagesWithFoldedHeadersOnHttp10() |
| 340 |
{ |
| 341 |
$req = "PUT / HTTP/1.0\r\nFoo: Bar\r\n Bam\r\n\r\n"; |
| 342 |
$request = Psr7\parse_request($req); |
| 343 |
$this->assertEquals('PUT', $request->getMethod()); |
| 344 |
$this->assertEquals('/', $request->getRequestTarget()); |
| 345 |
$this->assertEquals('Bar Bam', $request->getHeaderLine('Foo')); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @expectedException \InvalidArgumentException |
| 350 |
* @expectedExceptionMessage Invalid header syntax: Obsolete line folding |
| 351 |
*/ |
| 352 |
public function testRequestParsingFailsWithFoldedHeadersOnHttp11() |
| 353 |
{ |
| 354 |
Psr7\parse_response("GET_DATA / HTTP/1.1\r\nFoo: Bar\r\n Biz: Bam\r\n\r\n"); |
| 355 |
} |
| 356 |
|
| 357 |
public function testParsesRequestMessagesWhenHeaderDelimiterIsOnlyALineFeed() |
| 358 |
{ |
| 359 |
$req = "PUT / HTTP/1.0\nFoo: Bar\nBaz: Bam\n\n"; |
| 360 |
$request = Psr7\parse_request($req); |
| 361 |
$this->assertEquals('PUT', $request->getMethod()); |
| 362 |
$this->assertEquals('/', $request->getRequestTarget()); |
| 363 |
$this->assertEquals('Bar', $request->getHeaderLine('Foo')); |
| 364 |
$this->assertEquals('Bam', $request->getHeaderLine('Baz')); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* @expectedException \InvalidArgumentException |
| 369 |
*/ |
| 370 |
public function testValidatesRequestMessages() |
| 371 |
{ |
| 372 |
Psr7\parse_request("HTTP/1.1 200 OK\r\n\r\n"); |
| 373 |
} |
| 374 |
|
| 375 |
public function testParsesResponseMessages() |
| 376 |
{ |
| 377 |
$res = "HTTP/1.0 200 OK\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest"; |
| 378 |
$response = Psr7\parse_response($res); |
| 379 |
$this->assertSame(200, $response->getStatusCode()); |
| 380 |
$this->assertSame('OK', $response->getReasonPhrase()); |
| 381 |
$this->assertSame('1.0', $response->getProtocolVersion()); |
| 382 |
$this->assertSame('Bar', $response->getHeaderLine('Foo')); |
| 383 |
$this->assertSame('Bam, Qux', $response->getHeaderLine('Baz')); |
| 384 |
$this->assertSame('Test', (string)$response->getBody()); |
| 385 |
} |
| 386 |
|
| 387 |
public function testParsesResponseWithoutReason() |
| 388 |
{ |
| 389 |
$res = "HTTP/1.0 200\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest"; |
| 390 |
$response = Psr7\parse_response($res); |
| 391 |
$this->assertSame(200, $response->getStatusCode()); |
| 392 |
$this->assertSame('OK', $response->getReasonPhrase()); |
| 393 |
$this->assertSame('1.0', $response->getProtocolVersion()); |
| 394 |
$this->assertSame('Bar', $response->getHeaderLine('Foo')); |
| 395 |
$this->assertSame('Bam, Qux', $response->getHeaderLine('Baz')); |
| 396 |
$this->assertSame('Test', (string)$response->getBody()); |
| 397 |
} |
| 398 |
|
| 399 |
public function testParsesResponseWithLeadingDelimiter() |
| 400 |
{ |
| 401 |
$res = "\r\nHTTP/1.0 200\r\nFoo: Bar\r\n\r\nTest"; |
| 402 |
$response = Psr7\parse_response($res); |
| 403 |
$this->assertSame(200, $response->getStatusCode()); |
| 404 |
$this->assertSame('OK', $response->getReasonPhrase()); |
| 405 |
$this->assertSame('1.0', $response->getProtocolVersion()); |
| 406 |
$this->assertSame('Bar', $response->getHeaderLine('Foo')); |
| 407 |
$this->assertSame('Test', (string)$response->getBody()); |
| 408 |
} |
| 409 |
|
| 410 |
public function testParsesResponseWithFoldedHeadersOnHttp10() |
| 411 |
{ |
| 412 |
$res = "HTTP/1.0 200\r\nFoo: Bar\r\n Bam\r\n\r\nTest"; |
| 413 |
$response = Psr7\parse_response($res); |
| 414 |
$this->assertSame(200, $response->getStatusCode()); |
| 415 |
$this->assertSame('OK', $response->getReasonPhrase()); |
| 416 |
$this->assertSame('1.0', $response->getProtocolVersion()); |
| 417 |
$this->assertSame('Bar Bam', $response->getHeaderLine('Foo')); |
| 418 |
$this->assertSame('Test', (string)$response->getBody()); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* @expectedException \InvalidArgumentException |
| 423 |
* @expectedExceptionMessage Invalid header syntax: Obsolete line folding |
| 424 |
*/ |
| 425 |
public function testResponseParsingFailsWithFoldedHeadersOnHttp11() |
| 426 |
{ |
| 427 |
Psr7\parse_response("HTTP/1.1 200\r\nFoo: Bar\r\n Biz: Bam\r\nBaz: Qux\r\n\r\nTest"); |
| 428 |
} |
| 429 |
|
| 430 |
public function testParsesResponseWhenHeaderDelimiterIsOnlyALineFeed() |
| 431 |
{ |
| 432 |
$res = "HTTP/1.0 200\nFoo: Bar\nBaz: Bam\n\nTest\n\nOtherTest"; |
| 433 |
$response = Psr7\parse_response($res); |
| 434 |
$this->assertSame(200, $response->getStatusCode()); |
| 435 |
$this->assertSame('OK', $response->getReasonPhrase()); |
| 436 |
$this->assertSame('1.0', $response->getProtocolVersion()); |
| 437 |
$this->assertSame('Bar', $response->getHeaderLine('Foo')); |
| 438 |
$this->assertSame('Bam', $response->getHeaderLine('Baz')); |
| 439 |
$this->assertSame("Test\n\nOtherTest", (string)$response->getBody()); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* @expectedException \InvalidArgumentException |
| 444 |
* @expectedExceptionMessage Invalid message: Missing header delimiter |
| 445 |
*/ |
| 446 |
public function testResponseParsingFailsWithoutHeaderDelimiter() |
| 447 |
{ |
| 448 |
Psr7\parse_response("HTTP/1.0 200\r\nFoo: Bar\r\n Baz: Bam\r\nBaz: Qux\r\n"); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* @expectedException \InvalidArgumentException |
| 453 |
*/ |
| 454 |
public function testValidatesResponseMessages() |
| 455 |
{ |
| 456 |
Psr7\parse_response("GET / HTTP/1.1\r\n\r\n"); |
| 457 |
} |
| 458 |
|
| 459 |
public function testDetermineMimetype() |
| 460 |
{ |
| 461 |
$this->assertNull(Psr7\mimetype_from_extension('not-a-real-extension')); |
| 462 |
$this->assertEquals( |
| 463 |
'application/json', |
| 464 |
Psr7\mimetype_from_extension('json') |
| 465 |
); |
| 466 |
$this->assertEquals( |
| 467 |
'image/jpeg', |
| 468 |
Psr7\mimetype_from_filename('/tmp/images/IMG034821.JPEG') |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
public function testCreatesUriForValue() |
| 473 |
{ |
| 474 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Uri', Psr7\uri_for('/foo')); |
| 475 |
$this->assertInstanceOf( |
| 476 |
'GuzzleHttp\Psr7\Uri', |
| 477 |
Psr7\uri_for(new Psr7\Uri('/foo')) |
| 478 |
); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* @expectedException \InvalidArgumentException |
| 483 |
*/ |
| 484 |
public function testValidatesUri() |
| 485 |
{ |
| 486 |
Psr7\uri_for([]); |
| 487 |
} |
| 488 |
|
| 489 |
public function testKeepsPositionOfResource() |
| 490 |
{ |
| 491 |
$h = fopen(__FILE__, 'r'); |
| 492 |
fseek($h, 10); |
| 493 |
$stream = Psr7\stream_for($h); |
| 494 |
$this->assertEquals(10, $stream->tell()); |
| 495 |
$stream->close(); |
| 496 |
} |
| 497 |
|
| 498 |
public function testCreatesWithFactory() |
| 499 |
{ |
| 500 |
$stream = Psr7\stream_for('foo'); |
| 501 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $stream); |
| 502 |
$this->assertEquals('foo', $stream->getContents()); |
| 503 |
$stream->close(); |
| 504 |
} |
| 505 |
|
| 506 |
public function testFactoryCreatesFromEmptyString() |
| 507 |
{ |
| 508 |
$s = Psr7\stream_for(); |
| 509 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s); |
| 510 |
} |
| 511 |
|
| 512 |
public function testFactoryCreatesFromNull() |
| 513 |
{ |
| 514 |
$s = Psr7\stream_for(null); |
| 515 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s); |
| 516 |
} |
| 517 |
|
| 518 |
public function testFactoryCreatesFromResource() |
| 519 |
{ |
| 520 |
$r = fopen(__FILE__, 'r'); |
| 521 |
$s = Psr7\stream_for($r); |
| 522 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s); |
| 523 |
$this->assertSame(file_get_contents(__FILE__), (string)$s); |
| 524 |
} |
| 525 |
|
| 526 |
public function testFactoryCreatesFromObjectWithToString() |
| 527 |
{ |
| 528 |
$r = new HasToString(); |
| 529 |
$s = Psr7\stream_for($r); |
| 530 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s); |
| 531 |
$this->assertEquals('foo', (string)$s); |
| 532 |
} |
| 533 |
|
| 534 |
public function testCreatePassesThrough() |
| 535 |
{ |
| 536 |
$s = Psr7\stream_for('foo'); |
| 537 |
$this->assertSame($s, Psr7\stream_for($s)); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* @expectedException \InvalidArgumentException |
| 542 |
*/ |
| 543 |
public function testThrowsExceptionForUnknown() |
| 544 |
{ |
| 545 |
Psr7\stream_for(new \stdClass()); |
| 546 |
} |
| 547 |
|
| 548 |
public function testReturnsCustomMetadata() |
| 549 |
{ |
| 550 |
$s = Psr7\stream_for('foo', ['metadata' => ['hwm' => 3]]); |
| 551 |
$this->assertEquals(3, $s->getMetadata('hwm')); |
| 552 |
$this->assertArrayHasKey('hwm', $s->getMetadata()); |
| 553 |
} |
| 554 |
|
| 555 |
public function testCanSetSize() |
| 556 |
{ |
| 557 |
$s = Psr7\stream_for('', ['size' => 10]); |
| 558 |
$this->assertEquals(10, $s->getSize()); |
| 559 |
} |
| 560 |
|
| 561 |
public function testCanCreateIteratorBasedStream() |
| 562 |
{ |
| 563 |
$a = new \ArrayIterator(['foo', 'bar', '123']); |
| 564 |
$p = Psr7\stream_for($a); |
| 565 |
$this->assertInstanceOf('GuzzleHttp\Psr7\PumpStream', $p); |
| 566 |
$this->assertEquals('foo', $p->read(3)); |
| 567 |
$this->assertFalse($p->eof()); |
| 568 |
$this->assertEquals('b', $p->read(1)); |
| 569 |
$this->assertEquals('a', $p->read(1)); |
| 570 |
$this->assertEquals('r12', $p->read(3)); |
| 571 |
$this->assertFalse($p->eof()); |
| 572 |
$this->assertEquals('3', $p->getContents()); |
| 573 |
$this->assertTrue($p->eof()); |
| 574 |
$this->assertEquals(9, $p->tell()); |
| 575 |
} |
| 576 |
|
| 577 |
public function testConvertsRequestsToStrings() |
| 578 |
{ |
| 579 |
$request = new Psr7\Request('PUT', 'http://foo.com/hi?123', [ |
| 580 |
'Baz' => 'bar', |
| 581 |
'Qux' => 'ipsum', |
| 582 |
], 'hello', '1.0'); |
| 583 |
$this->assertEquals( |
| 584 |
"PUT /hi?123 HTTP/1.0\r\nHost: foo.com\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello", |
| 585 |
Psr7\str($request) |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
public function testConvertsResponsesToStrings() |
| 590 |
{ |
| 591 |
$response = new Psr7\Response(200, [ |
| 592 |
'Baz' => 'bar', |
| 593 |
'Qux' => 'ipsum', |
| 594 |
], 'hello', '1.0', 'FOO'); |
| 595 |
$this->assertEquals( |
| 596 |
"HTTP/1.0 200 FOO\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello", |
| 597 |
Psr7\str($response) |
| 598 |
); |
| 599 |
} |
| 600 |
|
| 601 |
public function parseParamsProvider() |
| 602 |
{ |
| 603 |
$res1 = [ |
| 604 |
[ |
| 605 |
'<http:/.../front.jpeg>', |
| 606 |
'rel' => 'front', |
| 607 |
'type' => 'image/jpeg', |
| 608 |
], |
| 609 |
[ |
| 610 |
'<http://.../back.jpeg>', |
| 611 |
'rel' => 'back', |
| 612 |
'type' => 'image/jpeg', |
| 613 |
], |
| 614 |
]; |
| 615 |
return [ |
| 616 |
[ |
| 617 |
'<http:/.../front.jpeg>; rel="front"; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"', |
| 618 |
$res1, |
| 619 |
], |
| 620 |
[ |
| 621 |
'<http:/.../front.jpeg>; rel="front"; type="image/jpeg",<http://.../back.jpeg>; rel=back; type="image/jpeg"', |
| 622 |
$res1, |
| 623 |
], |
| 624 |
[ |
| 625 |
'foo="baz"; bar=123, boo, test="123", foobar="foo;bar"', |
| 626 |
[ |
| 627 |
['foo' => 'baz', 'bar' => '123'], |
| 628 |
['boo'], |
| 629 |
['test' => '123'], |
| 630 |
['foobar' => 'foo;bar'], |
| 631 |
], |
| 632 |
], |
| 633 |
[ |
| 634 |
'<http://.../side.jpeg?test=1>; rel="side"; type="image/jpeg",<http://.../side.jpeg?test=2>; rel=side; type="image/jpeg"', |
| 635 |
[ |
| 636 |
['<http://.../side.jpeg?test=1>', 'rel' => 'side', 'type' => 'image/jpeg'], |
| 637 |
['<http://.../side.jpeg?test=2>', 'rel' => 'side', 'type' => 'image/jpeg'], |
| 638 |
], |
| 639 |
], |
| 640 |
[ |
| 641 |
'', |
| 642 |
[], |
| 643 |
], |
| 644 |
]; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* @dataProvider parseParamsProvider |
| 649 |
*/ |
| 650 |
public function testParseParams($header, $result) |
| 651 |
{ |
| 652 |
$this->assertEquals($result, Psr7\parse_header($header)); |
| 653 |
} |
| 654 |
|
| 655 |
public function testParsesArrayHeaders() |
| 656 |
{ |
| 657 |
$header = ['a, b', 'c', 'd, e']; |
| 658 |
$this->assertEquals(['a', 'b', 'c', 'd', 'e'], Psr7\normalize_header($header)); |
| 659 |
} |
| 660 |
|
| 661 |
public function testRewindsBody() |
| 662 |
{ |
| 663 |
$body = Psr7\stream_for('abc'); |
| 664 |
$res = new Psr7\Response(200, [], $body); |
| 665 |
Psr7\rewind_body($res); |
| 666 |
$this->assertEquals(0, $body->tell()); |
| 667 |
$body->rewind(); |
| 668 |
Psr7\rewind_body($res); |
| 669 |
$this->assertEquals(0, $body->tell()); |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* @expectedException \RuntimeException |
| 674 |
*/ |
| 675 |
public function testThrowsWhenBodyCannotBeRewound() |
| 676 |
{ |
| 677 |
$body = Psr7\stream_for('abc'); |
| 678 |
$body->read(1); |
| 679 |
$body = FnStream::decorate($body, [ |
| 680 |
'rewind' => function () { |
| 681 |
throw new \RuntimeException('a'); |
| 682 |
}, |
| 683 |
]); |
| 684 |
$res = new Psr7\Response(200, [], $body); |
| 685 |
Psr7\rewind_body($res); |
| 686 |
} |
| 687 |
|
| 688 |
public function testCanModifyRequestWithUri() |
| 689 |
{ |
| 690 |
$r1 = new Psr7\Request('GET', 'http://foo.com'); |
| 691 |
$r2 = Psr7\modify_request($r1, [ |
| 692 |
'uri' => new Psr7\Uri('http://www.foo.com'), |
| 693 |
]); |
| 694 |
$this->assertEquals('http://www.foo.com', (string)$r2->getUri()); |
| 695 |
$this->assertEquals('www.foo.com', (string)$r2->getHeaderLine('host')); |
| 696 |
} |
| 697 |
|
| 698 |
public function testCanModifyRequestWithUriAndPort() |
| 699 |
{ |
| 700 |
$r1 = new Psr7\Request('GET', 'http://foo.com:8000'); |
| 701 |
$r2 = Psr7\modify_request($r1, [ |
| 702 |
'uri' => new Psr7\Uri('http://www.foo.com:8000'), |
| 703 |
]); |
| 704 |
$this->assertEquals('http://www.foo.com:8000', (string)$r2->getUri()); |
| 705 |
$this->assertEquals('www.foo.com:8000', (string)$r2->getHeaderLine('host')); |
| 706 |
} |
| 707 |
|
| 708 |
public function testCanModifyRequestWithCaseInsensitiveHeader() |
| 709 |
{ |
| 710 |
$r1 = new Psr7\Request('GET', 'http://foo.com', ['User-Agent' => 'foo']); |
| 711 |
$r2 = Psr7\modify_request($r1, ['set_headers' => ['User-agent' => 'bar']]); |
| 712 |
$this->assertEquals('bar', $r2->getHeaderLine('User-Agent')); |
| 713 |
$this->assertEquals('bar', $r2->getHeaderLine('User-agent')); |
| 714 |
} |
| 715 |
|
| 716 |
public function testReturnsAsIsWhenNoChanges() |
| 717 |
{ |
| 718 |
$r1 = new Psr7\Request('GET', 'http://foo.com'); |
| 719 |
$r2 = Psr7\modify_request($r1, []); |
| 720 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Request', $r2); |
| 721 |
|
| 722 |
$r1 = new Psr7\ServerRequest('GET', 'http://foo.com'); |
| 723 |
$r2 = Psr7\modify_request($r1, []); |
| 724 |
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $r2); |
| 725 |
} |
| 726 |
|
| 727 |
public function testReturnsUriAsIsWhenNoChanges() |
| 728 |
{ |
| 729 |
$r1 = new Psr7\Request('GET', 'http://foo.com'); |
| 730 |
$r2 = Psr7\modify_request($r1, ['set_headers' => ['foo' => 'bar']]); |
| 731 |
$this->assertNotSame($r1, $r2); |
| 732 |
$this->assertEquals('bar', $r2->getHeaderLine('foo')); |
| 733 |
} |
| 734 |
|
| 735 |
public function testRemovesHeadersFromMessage() |
| 736 |
{ |
| 737 |
$r1 = new Psr7\Request('GET', 'http://foo.com', ['foo' => 'bar']); |
| 738 |
$r2 = Psr7\modify_request($r1, ['remove_headers' => ['foo']]); |
| 739 |
$this->assertNotSame($r1, $r2); |
| 740 |
$this->assertFalse($r2->hasHeader('foo')); |
| 741 |
} |
| 742 |
|
| 743 |
public function testAddsQueryToUri() |
| 744 |
{ |
| 745 |
$r1 = new Psr7\Request('GET', 'http://foo.com'); |
| 746 |
$r2 = Psr7\modify_request($r1, ['query' => 'foo=bar']); |
| 747 |
$this->assertNotSame($r1, $r2); |
| 748 |
$this->assertEquals('foo=bar', $r2->getUri()->getQuery()); |
| 749 |
} |
| 750 |
|
| 751 |
public function testModifyRequestKeepInstanceOfRequest() |
| 752 |
{ |
| 753 |
$r1 = new Psr7\Request('GET', 'http://foo.com'); |
| 754 |
$r2 = Psr7\modify_request($r1, ['remove_headers' => ['non-existent']]); |
| 755 |
$this->assertInstanceOf('GuzzleHttp\Psr7\Request', $r2); |
| 756 |
|
| 757 |
$r1 = new Psr7\ServerRequest('GET', 'http://foo.com'); |
| 758 |
$r2 = Psr7\modify_request($r1, ['remove_headers' => ['non-existent']]); |
| 759 |
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $r2); |
| 760 |
} |
| 761 |
|
| 762 |
public function testMessageBodySummaryWithSmallBody() |
| 763 |
{ |
| 764 |
$message = new Psr7\Response(200, [], 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); |
| 765 |
$this->assertEquals('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', Psr7\get_message_body_summary($message)); |
| 766 |
} |
| 767 |
|
| 768 |
public function testMessageBodySummaryWithLargeBody() |
| 769 |
{ |
| 770 |
$message = new Psr7\Response(200, [], 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); |
| 771 |
$this->assertEquals('Lorem ipsu (truncated...)', Psr7\get_message_body_summary($message, 10)); |
| 772 |
} |
| 773 |
|
| 774 |
public function testMessageBodySummaryWithEmptyBody() |
| 775 |
{ |
| 776 |
$message = new Psr7\Response(200, [], ''); |
| 777 |
$this->assertNull(Psr7\get_message_body_summary($message)); |
| 778 |
} |
| 779 |
|
| 780 |
public function testGetResponseBodySummaryOfNonReadableStream() |
| 781 |
{ |
| 782 |
$this->assertNull(Psr7\get_message_body_summary(new Psr7\Response(500, [], new ReadSeekOnlyStream()))); |
| 783 |
} |
| 784 |
|
| 785 |
public function testModifyServerRequestWithUploadedFiles() |
| 786 |
{ |
| 787 |
$request = new Psr7\ServerRequest('GET', 'http://example.com/bla'); |
| 788 |
$file = new Psr7\UploadedFile('Test', 100, \UPLOAD_ERR_OK); |
| 789 |
$request = $request->withUploadedFiles([$file]); |
| 790 |
|
| 791 |
/** @var Psr7\ServerRequest $modifiedRequest */ |
| 792 |
$modifiedRequest = Psr7\modify_request($request, ['set_headers' => ['foo' => 'bar']]); |
| 793 |
|
| 794 |
$this->assertCount(1, $modifiedRequest->getUploadedFiles()); |
| 795 |
|
| 796 |
$files = $modifiedRequest->getUploadedFiles(); |
| 797 |
$this->assertInstanceOf('GuzzleHttp\Psr7\UploadedFile', $files[0]); |
| 798 |
} |
| 799 |
|
| 800 |
public function testModifyServerRequestWithCookies() |
| 801 |
{ |
| 802 |
$request = (new Psr7\ServerRequest('GET', 'http://example.com/bla')) |
| 803 |
->withCookieParams(['name' => 'value']); |
| 804 |
|
| 805 |
/** @var Psr7\ServerRequest $modifiedRequest */ |
| 806 |
$modifiedRequest = Psr7\modify_request($request, ['set_headers' => ['foo' => 'bar']]); |
| 807 |
|
| 808 |
$this->assertEquals(['name' => 'value'], $modifiedRequest->getCookieParams()); |
| 809 |
} |
| 810 |
|
| 811 |
public function testModifyServerRequestParsedBody() |
| 812 |
{ |
| 813 |
$request = (new Psr7\ServerRequest('GET', 'http://example.com/bla')) |
| 814 |
->withParsedBody(['name' => 'value']); |
| 815 |
|
| 816 |
/** @var Psr7\ServerRequest $modifiedRequest */ |
| 817 |
$modifiedRequest = Psr7\modify_request($request, ['set_headers' => ['foo' => 'bar']]); |
| 818 |
|
| 819 |
$this->assertEquals(['name' => 'value'], $modifiedRequest->getParsedBody()); |
| 820 |
} |
| 821 |
|
| 822 |
public function testModifyServerRequestQueryParams() |
| 823 |
{ |
| 824 |
$request = (new Psr7\ServerRequest('GET', 'http://example.com/bla')) |
| 825 |
->withQueryParams(['name' => 'value']); |
| 826 |
|
| 827 |
/** @var Psr7\ServerRequest $modifiedRequest */ |
| 828 |
$modifiedRequest = Psr7\modify_request($request, ['set_headers' => ['foo' => 'bar']]); |
| 829 |
|
| 830 |
$this->assertEquals(['name' => 'value'], $modifiedRequest->getQueryParams()); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
class HasToString |
| 835 |
{ |
| 836 |
public function __toString() |
| 837 |
{ |
| 838 |
return 'foo'; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* convert it to an anonymous class on PHP7 |
| 844 |
*/ |
| 845 |
final class ReadSeekOnlyStream extends Stream |
| 846 |
{ |
| 847 |
public function __construct() |
| 848 |
{ |
| 849 |
parent::__construct(fopen('php://memory', 'wb')); |
| 850 |
} |
| 851 |
|
| 852 |
public function isSeekable() |
| 853 |
{ |
| 854 |
return true; |
| 855 |
} |
| 856 |
|
| 857 |
public function isReadable() |
| 858 |
{ |
| 859 |
return false; |
| 860 |
} |
| 861 |
} |
| 862 |
|