FunctionsTest.php 21 KB

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