close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.admin.web_ui.BasicsAdminPanel'> (super(type, obj): obj (type BasicsAdminPanel) is not an instance or subtype of type (Markup).)

source: externals/php/vendor/guzzlehttp/psr7/tests/FunctionsTest.php@ b858e6f

Last change on this file since b858e6f was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 19.7 KB
Line 
1<?php
2namespace GuzzleHttp\Tests\Psr7;
3
4use GuzzleHttp\Psr7;
5use GuzzleHttp\Psr7\FnStream;
6use GuzzleHttp\Psr7\NoSeekStream;
7
8class FunctionsTest extends \PHPUnit_Framework_TestCase
9{
10 public function testCopiesToString()
11 {
12 $s = Psr7\stream_for('foobaz');
13 $this->assertEquals('foobaz', Psr7\copy_to_string($s));
14 $s->seek(0);
15 $this->assertEquals('foo', Psr7\copy_to_string($s, 3));
16 $this->assertEquals('baz', Psr7\copy_to_string($s, 3));
17 $this->assertEquals('', Psr7\copy_to_string($s));
18 }
19
20 public function testCopiesToStringStopsWhenReadFails()
21 {
22 $s1 = Psr7\stream_for('foobaz');
23 $s1 = FnStream::decorate($s1, [
24 'read' => function () { return ''; }
25 ]);
26 $result = Psr7\copy_to_string($s1);
27 $this->assertEquals('', $result);
28 }
29
30 public function testCopiesToStream()
31 {
32 $s1 = Psr7\stream_for('foobaz');
33 $s2 = Psr7\stream_for('');
34 Psr7\copy_to_stream($s1, $s2);
35 $this->assertEquals('foobaz', (string) $s2);
36 $s2 = Psr7\stream_for('');
37 $s1->seek(0);
38 Psr7\copy_to_stream($s1, $s2, 3);
39 $this->assertEquals('foo', (string) $s2);
40 Psr7\copy_to_stream($s1, $s2, 3);
41 $this->assertEquals('foobaz', (string) $s2);
42 }
43
44 public function testStopsCopyToStreamWhenWriteFails()
45 {
46 $s1 = Psr7\stream_for('foobaz');
47 $s2 = Psr7\stream_for('');
48 $s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
49 Psr7\copy_to_stream($s1, $s2);
50 $this->assertEquals('', (string) $s2);
51 }
52
53 public function testStopsCopyToSteamWhenWriteFailsWithMaxLen()
54 {
55 $s1 = Psr7\stream_for('foobaz');
56 $s2 = Psr7\stream_for('');
57 $s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
58 Psr7\copy_to_stream($s1, $s2, 10);
59 $this->assertEquals('', (string) $s2);
60 }
61
62 public function testStopsCopyToSteamWhenReadFailsWithMaxLen()
63 {
64 $s1 = Psr7\stream_for('foobaz');
65 $s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
66 $s2 = Psr7\stream_for('');
67 Psr7\copy_to_stream($s1, $s2, 10);
68 $this->assertEquals('', (string) $s2);
69 }
70
71 public function testReadsLines()
72 {
73 $s = Psr7\stream_for("foo\nbaz\nbar");
74 $this->assertEquals("foo\n", Psr7\readline($s));
75 $this->assertEquals("baz\n", Psr7\readline($s));
76 $this->assertEquals("bar", Psr7\readline($s));
77 }
78
79 public function testReadsLinesUpToMaxLength()
80 {
81 $s = Psr7\stream_for("12345\n");
82 $this->assertEquals("123", Psr7\readline($s, 4));
83 $this->assertEquals("45\n", Psr7\readline($s));
84 }
85
86 public function testReadsLineUntilFalseReturnedFromRead()
87 {
88 $s = $this->getMockBuilder('GuzzleHttp\Psr7\Stream')
89 ->setMethods(['read', 'eof'])
90 ->disableOriginalConstructor()
91 ->getMock();
92 $s->expects($this->exactly(2))
93 ->method('read')
94 ->will($this->returnCallback(function () {
95 static $c = false;
96 if ($c) {
97 return false;
98 }
99 $c = true;
100 return 'h';
101 }));
102 $s->expects($this->exactly(2))
103 ->method('eof')
104 ->will($this->returnValue(false));
105 $this->assertEquals("h", Psr7\readline($s));
106 }
107
108 public function testCalculatesHash()
109 {
110 $s = Psr7\stream_for('foobazbar');
111 $this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5'));
112 }
113
114 /**
115 * @expectedException \RuntimeException
116 */
117 public function testCalculatesHashThrowsWhenSeekFails()
118 {
119 $s = new NoSeekStream(Psr7\stream_for('foobazbar'));
120 $s->read(2);
121 Psr7\hash($s, 'md5');
122 }
123
124 public function testCalculatesHashSeeksToOriginalPosition()
125 {
126 $s = Psr7\stream_for('foobazbar');
127 $s->seek(4);
128 $this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5'));
129 $this->assertEquals(4, $s->tell());
130 }
131
132 public function testOpensFilesSuccessfully()
133 {
134 $r = Psr7\try_fopen(__FILE__, 'r');
135 $this->assertInternalType('resource', $r);
136 fclose($r);
137 }
138
139 /**
140 * @expectedException \RuntimeException
141 * @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
142 */
143 public function testThrowsExceptionNotWarning()
144 {
145 Psr7\try_fopen('/path/to/does/not/exist', 'r');
146 }
147
148 public function parseQueryProvider()
149 {
150 return [
151 // Does not need to parse when the string is empty
152 ['', []],
153 // Can parse mult-values items
154 ['q=a&q=b', ['q' => ['a', 'b']]],
155 // Can parse multi-valued items that use numeric indices
156 ['q[0]=a&q[1]=b', ['q[0]' => 'a', 'q[1]' => 'b']],
157 // Can parse duplicates and does not include numeric indices
158 ['q[]=a&q[]=b', ['q[]' => ['a', 'b']]],
159 // Ensures that the value of "q" is an array even though one value
160 ['q[]=a', ['q[]' => 'a']],
161 // Does not modify "." to "_" like PHP's parse_str()
162 ['q.a=a&q.b=b', ['q.a' => 'a', 'q.b' => 'b']],
163 // Can decode %20 to " "
164 ['q%20a=a%20b', ['q a' => 'a b']],
165 // Can parse funky strings with no values by assigning each to null
166 ['q&a', ['q' => null, 'a' => null]],
167 // Does not strip trailing equal signs
168 ['data=abc=', ['data' => 'abc=']],
169 // Can store duplicates without affecting other values
170 ['foo=a&foo=b&?µ=c', ['foo' => ['a', 'b'], '?µ' => 'c']],
171 // Sets value to null when no "=" is present
172 ['foo', ['foo' => null]],
173 // Preserves "0" keys.
174 ['0', ['0' => null]],
175 // Sets the value to an empty string when "=" is present
176 ['0=', ['0' => '']],
177 // Preserves falsey keys
178 ['var=0', ['var' => '0']],
179 ['a[b][c]=1&a[b][c]=2', ['a[b][c]' => ['1', '2']]],
180 ['a[b]=c&a[d]=e', ['a[b]' => 'c', 'a[d]' => 'e']],
181 // Ensure it doesn't leave things behind with repeated values
182 // Can parse mult-values items
183 ['q=a&q=b&q=c', ['q' => ['a', 'b', 'c']]],
184 ];
185 }
186
187 /**
188 * @dataProvider parseQueryProvider
189 */
190 public function testParsesQueries($input, $output)
191 {
192 $result = Psr7\parse_query($input);
193 $this->assertSame($output, $result);
194 }
195
196 public function testDoesNotDecode()
197 {
198 $str = 'foo%20=bar';
199 $data = Psr7\parse_query($str, false);
200 $this->assertEquals(['foo%20' => 'bar'], $data);
201 }
202
203 /**
204 * @dataProvider parseQueryProvider
205 */
206 public function testParsesAndBuildsQueries($input, $output)
207 {
208 $result = Psr7\parse_query($input, false);
209 $this->assertSame($input, Psr7\build_query($result, false));
210 }
211
212 public function testEncodesWithRfc1738()
213 {
214 $str = Psr7\build_query(['foo bar' => 'baz+'], PHP_QUERY_RFC1738);
215 $this->assertEquals('foo+bar=baz%2B', $str);
216 }
217
218 public function testEncodesWithRfc3986()
219 {
220 $str = Psr7\build_query(['foo bar' => 'baz+'], PHP_QUERY_RFC3986);
221 $this->assertEquals('foo%20bar=baz%2B', $str);
222 }
223
224 public function testDoesNotEncode()
225 {
226 $str = Psr7\build_query(['foo bar' => 'baz+'], false);
227 $this->assertEquals('foo bar=baz+', $str);
228 }
229
230 public function testCanControlDecodingType()
231 {
232 $result = Psr7\parse_query('var=foo+bar', PHP_QUERY_RFC3986);
233 $this->assertEquals('foo+bar', $result['var']);
234 $result = Psr7\parse_query('var=foo+bar', PHP_QUERY_RFC1738);
235 $this->assertEquals('foo bar', $result['var']);
236 }
237
238 public function testParsesRequestMessages()
239 {
240 $req = "GET /abc HTTP/1.0\r\nHost: foo.com\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest";
241 $request = Psr7\parse_request($req);
242 $this->assertEquals('GET', $request->getMethod());
243 $this->assertEquals('/abc', $request->getRequestTarget());
244 $this->assertEquals('1.0', $request->getProtocolVersion());
245 $this->assertEquals('foo.com', $request->getHeaderLine('Host'));
246 $this->assertEquals('Bar', $request->getHeaderLine('Foo'));
247 $this->assertEquals('Bam, Qux', $request->getHeaderLine('Baz'));
248 $this->assertEquals('Test', (string) $request->getBody());
249 $this->assertEquals('http://foo.com/abc', (string) $request->getUri());
250 }
251
252 public function testParsesRequestMessagesWithHttpsScheme()
253 {
254 $req = "PUT /abc?baz=bar HTTP/1.1\r\nHost: foo.com:443\r\n\r\n";
255 $request = Psr7\parse_request($req);
256 $this->assertEquals('PUT', $request->getMethod());
257 $this->assertEquals('/abc?baz=bar', $request->getRequestTarget());
258 $this->assertEquals('1.1', $request->getProtocolVersion());
259 $this->assertEquals('foo.com:443', $request->getHeaderLine('Host'));
260 $this->assertEquals('', (string) $request->getBody());
261 $this->assertEquals('https://foo.com/abc?baz=bar', (string) $request->getUri());
262 }
263
264 public function testParsesRequestMessagesWithUriWhenHostIsNotFirst()
265 {
266 $req = "PUT / HTTP/1.1\r\nFoo: Bar\r\nHost: foo.com\r\n\r\n";
267 $request = Psr7\parse_request($req);
268 $this->assertEquals('PUT', $request->getMethod());
269 $this->assertEquals('/', $request->getRequestTarget());
270 $this->assertEquals('http://foo.com/', (string) $request->getUri());
271 }
272
273 public function testParsesRequestMessagesWithFullUri()
274 {
275 $req = "GET https://www.google.com:443/search?q=foobar HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
276 $request = Psr7\parse_request($req);
277 $this->assertEquals('GET', $request->getMethod());
278 $this->assertEquals('https://www.google.com:443/search?q=foobar', $request->getRequestTarget());
279 $this->assertEquals('1.1', $request->getProtocolVersion());
280 $this->assertEquals('www.google.com', $request->getHeaderLine('Host'));
281 $this->assertEquals('', (string) $request->getBody());
282 $this->assertEquals('https://www.google.com/search?q=foobar', (string) $request->getUri());
283 }
284
285 /**
286 * @expectedException \InvalidArgumentException
287 */
288 public function testValidatesRequestMessages()
289 {
290 Psr7\parse_request("HTTP/1.1 200 OK\r\n\r\n");
291 }
292
293 public function testParsesResponseMessages()
294 {
295 $res = "HTTP/1.0 200 OK\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest";
296 $response = Psr7\parse_response($res);
297 $this->assertEquals(200, $response->getStatusCode());
298 $this->assertEquals('OK', $response->getReasonPhrase());
299 $this->assertEquals('1.0', $response->getProtocolVersion());
300 $this->assertEquals('Bar', $response->getHeaderLine('Foo'));
301 $this->assertEquals('Bam, Qux', $response->getHeaderLine('Baz'));
302 $this->assertEquals('Test', (string) $response->getBody());
303 }
304
305 /**
306 * @expectedException \InvalidArgumentException
307 */
308 public function testValidatesResponseMessages()
309 {
310 Psr7\parse_response("GET / HTTP/1.1\r\n\r\n");
311 }
312
313 public function testDetermineMimetype()
314 {
315 $this->assertNull(Psr7\mimetype_from_extension('not-a-real-extension'));
316 $this->assertEquals(
317 'application/json',
318 Psr7\mimetype_from_extension('json')
319 );
320 $this->assertEquals(
321 'image/jpeg',
322 Psr7\mimetype_from_filename('/tmp/images/IMG034821.JPEG')
323 );
324 }
325
326 public function testCreatesUriForValue()
327 {
328 $this->assertInstanceOf('GuzzleHttp\Psr7\Uri', Psr7\uri_for('/foo'));
329 $this->assertInstanceOf(
330 'GuzzleHttp\Psr7\Uri',
331 Psr7\uri_for(new Psr7\Uri('/foo'))
332 );
333 }
334
335 /**
336 * @expectedException \InvalidArgumentException
337 */
338 public function testValidatesUri()
339 {
340 Psr7\uri_for([]);
341 }
342
343 public function testKeepsPositionOfResource()
344 {
345 $h = fopen(__FILE__, 'r');
346 fseek($h, 10);
347 $stream = Psr7\stream_for($h);
348 $this->assertEquals(10, $stream->tell());
349 $stream->close();
350 }
351
352 public function testCreatesWithFactory()
353 {
354 $stream = Psr7\stream_for('foo');
355 $this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $stream);
356 $this->assertEquals('foo', $stream->getContents());
357 $stream->close();
358 }
359
360 public function testFactoryCreatesFromEmptyString()
361 {
362 $s = Psr7\stream_for();
363 $this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s);
364 }
365
366 public function testFactoryCreatesFromNull()
367 {
368 $s = Psr7\stream_for(null);
369 $this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s);
370 }
371
372 public function testFactoryCreatesFromResource()
373 {
374 $r = fopen(__FILE__, 'r');
375 $s = Psr7\stream_for($r);
376 $this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s);
377 $this->assertSame(file_get_contents(__FILE__), (string) $s);
378 }
379
380 public function testFactoryCreatesFromObjectWithToString()
381 {
382 $r = new HasToString();
383 $s = Psr7\stream_for($r);
384 $this->assertInstanceOf('GuzzleHttp\Psr7\Stream', $s);
385 $this->assertEquals('foo', (string) $s);
386 }
387
388 public function testCreatePassesThrough()
389 {
390 $s = Psr7\stream_for('foo');
391 $this->assertSame($s, Psr7\stream_for($s));
392 }
393
394 /**
395 * @expectedException \InvalidArgumentException
396 */
397 public function testThrowsExceptionForUnknown()
398 {
399 Psr7\stream_for(new \stdClass());
400 }
401
402 public function testReturnsCustomMetadata()
403 {
404 $s = Psr7\stream_for('foo', ['metadata' => ['hwm' => 3]]);
405 $this->assertEquals(3, $s->getMetadata('hwm'));
406 $this->assertArrayHasKey('hwm', $s->getMetadata());
407 }
408
409 public function testCanSetSize()
410 {
411 $s = Psr7\stream_for('', ['size' => 10]);
412 $this->assertEquals(10, $s->getSize());
413 }
414
415 public function testCanCreateIteratorBasedStream()
416 {
417 $a = new \ArrayIterator(['foo', 'bar', '123']);
418 $p = Psr7\stream_for($a);
419 $this->assertInstanceOf('GuzzleHttp\Psr7\PumpStream', $p);
420 $this->assertEquals('foo', $p->read(3));
421 $this->assertFalse($p->eof());
422 $this->assertEquals('b', $p->read(1));
423 $this->assertEquals('a', $p->read(1));
424 $this->assertEquals('r12', $p->read(3));
425 $this->assertFalse($p->eof());
426 $this->assertEquals('3', $p->getContents());
427 $this->assertTrue($p->eof());
428 $this->assertEquals(9, $p->tell());
429 }
430
431 public function testConvertsRequestsToStrings()
432 {
433 $request = new Psr7\Request('PUT', 'http://foo.com/hi?123', [
434 'Baz' => 'bar',
435 'Qux' => ' ipsum'
436 ], 'hello', '1.0');
437 $this->assertEquals(
438 "PUT /hi?123 HTTP/1.0\r\nHost: foo.com\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello",
439 Psr7\str($request)
440 );
441 }
442
443 public function testConvertsResponsesToStrings()
444 {
445 $response = new Psr7\Response(200, [
446 'Baz' => 'bar',
447 'Qux' => ' ipsum'
448 ], 'hello', '1.0', 'FOO');
449 $this->assertEquals(
450 "HTTP/1.0 200 FOO\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello",
451 Psr7\str($response)
452 );
453 }
454
455 public function parseParamsProvider()
456 {
457 $res1 = array(
458 array(
459 '<http:/.../front.jpeg>',
460 'rel' => 'front',
461 'type' => 'image/jpeg',
462 ),
463 array(
464 '<http://.../back.jpeg>',
465 'rel' => 'back',
466 'type' => 'image/jpeg',
467 ),
468 );
469 return array(
470 array(
471 '<http:/.../front.jpeg>; rel="front"; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"',
472 $res1
473 ),
474 array(
475 '<http:/.../front.jpeg>; rel="front"; type="image/jpeg",<http://.../back.jpeg>; rel=back; type="image/jpeg"',
476 $res1
477 ),
478 array(
479 'foo="baz"; bar=123, boo, test="123", foobar="foo;bar"',
480 array(
481 array('foo' => 'baz', 'bar' => '123'),
482 array('boo'),
483 array('test' => '123'),
484 array('foobar' => 'foo;bar')
485 )
486 ),
487 array(
488 '<http://.../side.jpeg?test=1>; rel="side"; type="image/jpeg",<http://.../side.jpeg?test=2>; rel=side; type="image/jpeg"',
489 array(
490 array('<http://.../side.jpeg?test=1>', 'rel' => 'side', 'type' => 'image/jpeg'),
491 array('<http://.../side.jpeg?test=2>', 'rel' => 'side', 'type' => 'image/jpeg')
492 )
493 ),
494 array(
495 '',
496 array()
497 )
498 );
499 }
500 /**
501 * @dataProvider parseParamsProvider
502 */
503 public function testParseParams($header, $result)
504 {
505 $this->assertEquals($result, Psr7\parse_header($header));
506 }
507
508 public function testParsesArrayHeaders()
509 {
510 $header = ['a, b', 'c', 'd, e'];
511 $this->assertEquals(['a', 'b', 'c', 'd', 'e'], Psr7\normalize_header($header));
512 }
513
514 public function testRewindsBody()
515 {
516 $body = Psr7\stream_for('abc');
517 $res = new Psr7\Response(200, [], $body);
518 Psr7\rewind_body($res);
519 $this->assertEquals(0, $body->tell());
520 $body->rewind(1);
521 Psr7\rewind_body($res);
522 $this->assertEquals(0, $body->tell());
523 }
524
525 /**
526 * @expectedException \RuntimeException
527 */
528 public function testThrowsWhenBodyCannotBeRewound()
529 {
530 $body = Psr7\stream_for('abc');
531 $body->read(1);
532 $body = FnStream::decorate($body, [
533 'rewind' => function () { throw new \RuntimeException('a'); }
534 ]);
535 $res = new Psr7\Response(200, [], $body);
536 Psr7\rewind_body($res);
537 }
538
539 public function testCanModifyRequestWithUri()
540 {
541 $r1 = new Psr7\Request('GET', 'http://foo.com');
542 $r2 = Psr7\modify_request($r1, [
543 'uri' => new Psr7\Uri('http://www.foo.com')
544 ]);
545 $this->assertEquals('http://www.foo.com', (string) $r2->getUri());
546 $this->assertEquals('www.foo.com', (string) $r2->getHeaderLine('host'));
547 }
548
549 public function testCanModifyRequestWithCaseInsensitiveHeader()
550 {
551 $r1 = new Psr7\Request('GET', 'http://foo.com', ['User-Agent' => 'foo']);
552 $r2 = Psr7\modify_request($r1, ['set_headers' => ['User-agent' => 'bar']]);
553 $this->assertEquals('bar', $r2->getHeaderLine('User-Agent'));
554 $this->assertEquals('bar', $r2->getHeaderLine('User-agent'));
555 }
556
557 public function testReturnsAsIsWhenNoChanges()
558 {
559 $request = new Psr7\Request('GET', 'http://foo.com');
560 $this->assertSame($request, Psr7\modify_request($request, []));
561 }
562
563 public function testReturnsUriAsIsWhenNoChanges()
564 {
565 $r1 = new Psr7\Request('GET', 'http://foo.com');
566 $r2 = Psr7\modify_request($r1, ['set_headers' => ['foo' => 'bar']]);
567 $this->assertNotSame($r1, $r2);
568 $this->assertEquals('bar', $r2->getHeaderLine('foo'));
569 }
570
571 public function testRemovesHeadersFromMessage()
572 {
573 $r1 = new Psr7\Request('GET', 'http://foo.com', ['foo' => 'bar']);
574 $r2 = Psr7\modify_request($r1, ['remove_headers' => ['foo']]);
575 $this->assertNotSame($r1, $r2);
576 $this->assertFalse($r2->hasHeader('foo'));
577 }
578
579 public function testAddsQueryToUri()
580 {
581 $r1 = new Psr7\Request('GET', 'http://foo.com');
582 $r2 = Psr7\modify_request($r1, ['query' => 'foo=bar']);
583 $this->assertNotSame($r1, $r2);
584 $this->assertEquals('foo=bar', $r2->getUri()->getQuery());
585 }
586}
Note: See TracBrowser for help on using the repository browser.