RequestTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\Request;
  4. use RingCentral\Psr7\Uri;
  5. /**
  6. * @covers RingCentral\Psr7\Request
  7. */
  8. class RequestTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function testRequestUriMayBeString()
  11. {
  12. $r = new Request('GET', '/');
  13. $this->assertEquals('/', (string) $r->getUri());
  14. }
  15. public function testRequestUriMayBeUri()
  16. {
  17. $uri = new Uri('/');
  18. $r = new Request('GET', $uri);
  19. $this->assertSame($uri, $r->getUri());
  20. }
  21. /**
  22. * @expectedException \InvalidArgumentException
  23. */
  24. public function testValidateRequestUri()
  25. {
  26. new Request('GET', true);
  27. }
  28. public function testCanConstructWithBody()
  29. {
  30. $r = new Request('GET', '/', array(), 'baz');
  31. $this->assertEquals('baz', (string) $r->getBody());
  32. }
  33. public function testCapitalizesMethod()
  34. {
  35. $r = new Request('get', '/');
  36. $this->assertEquals('GET', $r->getMethod());
  37. }
  38. public function testCapitalizesWithMethod()
  39. {
  40. $r = new Request('GET', '/');
  41. $this->assertEquals('PUT', $r->withMethod('put')->getMethod());
  42. }
  43. public function testWithUri()
  44. {
  45. $r1 = new Request('GET', '/');
  46. $u1 = $r1->getUri();
  47. $u2 = new Uri('http://www.example.com');
  48. $r2 = $r1->withUri($u2);
  49. $this->assertNotSame($r1, $r2);
  50. $this->assertSame($u2, $r2->getUri());
  51. $this->assertSame($u1, $r1->getUri());
  52. }
  53. public function testSameInstanceWhenSameUri()
  54. {
  55. $r1 = new Request('GET', 'http://foo.com');
  56. $r2 = $r1->withUri($r1->getUri());
  57. $this->assertSame($r1, $r2);
  58. }
  59. public function testWithRequestTarget()
  60. {
  61. $r1 = new Request('GET', '/');
  62. $r2 = $r1->withRequestTarget('*');
  63. $this->assertEquals('*', $r2->getRequestTarget());
  64. $this->assertEquals('/', $r1->getRequestTarget());
  65. }
  66. /**
  67. * @expectedException \InvalidArgumentException
  68. */
  69. public function testRequestTargetDoesNotAllowSpaces()
  70. {
  71. $r1 = new Request('GET', '/');
  72. $r1->withRequestTarget('/foo bar');
  73. }
  74. public function testRequestTargetDefaultsToSlash()
  75. {
  76. $r1 = new Request('GET', '');
  77. $this->assertEquals('/', $r1->getRequestTarget());
  78. $r2 = new Request('GET', '*');
  79. $this->assertEquals('*', $r2->getRequestTarget());
  80. $r3 = new Request('GET', 'http://foo.com/bar baz/');
  81. $this->assertEquals('/bar%20baz/', $r3->getRequestTarget());
  82. }
  83. public function testBuildsRequestTarget()
  84. {
  85. $r1 = new Request('GET', 'http://foo.com/baz?bar=bam');
  86. $this->assertEquals('/baz?bar=bam', $r1->getRequestTarget());
  87. }
  88. public function testHostIsAddedFirst()
  89. {
  90. $r = new Request('GET', 'http://foo.com/baz?bar=bam', array('Foo' => 'Bar'));
  91. $this->assertEquals(array(
  92. 'Host' => array('foo.com'),
  93. 'Foo' => array('Bar')
  94. ), $r->getHeaders());
  95. }
  96. public function testCanGetHeaderAsCsv()
  97. {
  98. $r = new Request('GET', 'http://foo.com/baz?bar=bam', array(
  99. 'Foo' => array('a', 'b', 'c')
  100. ));
  101. $this->assertEquals('a, b, c', $r->getHeaderLine('Foo'));
  102. $this->assertEquals('', $r->getHeaderLine('Bar'));
  103. }
  104. public function testHostIsNotOverwrittenWhenPreservingHost()
  105. {
  106. $r = new Request('GET', 'http://foo.com/baz?bar=bam', array('Host' => 'a.com'));
  107. $this->assertEquals(array('Host' => array('a.com')), $r->getHeaders());
  108. $r2 = $r->withUri(new Uri('http://www.foo.com/bar'), true);
  109. $this->assertEquals('a.com', $r2->getHeaderLine('Host'));
  110. }
  111. public function testOverridesHostWithUri()
  112. {
  113. $r = new Request('GET', 'http://foo.com/baz?bar=bam');
  114. $this->assertEquals(array('Host' => array('foo.com')), $r->getHeaders());
  115. $r2 = $r->withUri(new Uri('http://www.baz.com/bar'));
  116. $this->assertEquals('www.baz.com', $r2->getHeaderLine('Host'));
  117. }
  118. public function testAggregatesHeaders()
  119. {
  120. $r = new Request('GET', 'http://foo.com', array(
  121. 'ZOO' => 'zoobar',
  122. 'zoo' => array('foobar', 'zoobar')
  123. ));
  124. $this->assertEquals('zoobar, foobar, zoobar', $r->getHeaderLine('zoo'));
  125. }
  126. public function testAddsPortToHeader()
  127. {
  128. $r = new Request('GET', 'http://foo.com:8124/bar');
  129. $this->assertEquals('foo.com:8124', $r->getHeaderLine('host'));
  130. }
  131. public function testAddsPortToHeaderAndReplacePreviousPort()
  132. {
  133. $r = new Request('GET', 'http://foo.com:8124/bar');
  134. $r = $r->withUri(new Uri('http://foo.com:8125/bar'));
  135. $this->assertEquals('foo.com:8125', $r->getHeaderLine('host'));
  136. }
  137. }