ResponseTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\Response;
  4. use RingCentral\Psr7;
  5. /**
  6. * @covers RingCentral\Psr7\MessageTrait
  7. * @covers RingCentral\Psr7\Response
  8. */
  9. class ResponseTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public function testAddsDefaultReason()
  12. {
  13. $r = new Response('200');
  14. $this->assertSame(200, $r->getStatusCode());
  15. $this->assertEquals('OK', $r->getReasonPhrase());
  16. }
  17. public function testCanGiveCustomReason()
  18. {
  19. $r = new Response(200, array(), null, '1.1', 'bar');
  20. $this->assertEquals('bar', $r->getReasonPhrase());
  21. }
  22. public function testCanGiveCustomProtocolVersion()
  23. {
  24. $r = new Response(200, array(), null, '1000');
  25. $this->assertEquals('1000', $r->getProtocolVersion());
  26. }
  27. public function testCanCreateNewResponseWithStatusAndNoReason()
  28. {
  29. $r = new Response(200);
  30. $r2 = $r->withStatus(201);
  31. $this->assertEquals(200, $r->getStatusCode());
  32. $this->assertEquals('OK', $r->getReasonPhrase());
  33. $this->assertEquals(201, $r2->getStatusCode());
  34. $this->assertEquals('Created', $r2->getReasonPhrase());
  35. }
  36. public function testCanCreateNewResponseWithStatusAndReason()
  37. {
  38. $r = new Response(200);
  39. $r2 = $r->withStatus(201, 'Foo');
  40. $this->assertEquals(200, $r->getStatusCode());
  41. $this->assertEquals('OK', $r->getReasonPhrase());
  42. $this->assertEquals(201, $r2->getStatusCode());
  43. $this->assertEquals('Foo', $r2->getReasonPhrase());
  44. }
  45. public function testCreatesResponseWithAddedHeaderArray()
  46. {
  47. $r = new Response();
  48. $r2 = $r->withAddedHeader('foo', array('baz', 'bar'));
  49. $this->assertFalse($r->hasHeader('foo'));
  50. $this->assertEquals('baz, bar', $r2->getHeaderLine('foo'));
  51. }
  52. public function testReturnsIdentityWhenRemovingMissingHeader()
  53. {
  54. $r = new Response();
  55. $this->assertSame($r, $r->withoutHeader('foo'));
  56. }
  57. public function testAlwaysReturnsBody()
  58. {
  59. $r = new Response();
  60. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody());
  61. }
  62. public function testCanSetHeaderAsArray()
  63. {
  64. $r = new Response(200, array(
  65. 'foo' => array('baz ', ' bar ')
  66. ));
  67. $this->assertEquals('baz, bar', $r->getHeaderLine('foo'));
  68. $this->assertEquals(array('baz', 'bar'), $r->getHeader('foo'));
  69. }
  70. public function testSameInstanceWhenSameBody()
  71. {
  72. $r = new Response(200, array(), 'foo');
  73. $b = $r->getBody();
  74. $this->assertSame($r, $r->withBody($b));
  75. }
  76. public function testNewInstanceWhenNewBody()
  77. {
  78. $r = new Response(200, array(), 'foo');
  79. $b2 = Psr7\stream_for('abc');
  80. $this->assertNotSame($r, $r->withBody($b2));
  81. }
  82. public function testSameInstanceWhenSameProtocol()
  83. {
  84. $r = new Response(200);
  85. $this->assertSame($r, $r->withProtocolVersion('1.1'));
  86. }
  87. public function testNewInstanceWhenNewProtocol()
  88. {
  89. $r = new Response(200);
  90. $this->assertNotSame($r, $r->withProtocolVersion('1.0'));
  91. }
  92. public function testNewInstanceWhenRemovingHeader()
  93. {
  94. $r = new Response(200, array('Foo' => 'Bar'));
  95. $r2 = $r->withoutHeader('Foo');
  96. $this->assertNotSame($r, $r2);
  97. $this->assertFalse($r2->hasHeader('foo'));
  98. }
  99. public function testNewInstanceWhenAddingHeader()
  100. {
  101. $r = new Response(200, array('Foo' => 'Bar'));
  102. $r2 = $r->withAddedHeader('Foo', 'Baz');
  103. $this->assertNotSame($r, $r2);
  104. $this->assertEquals('Bar, Baz', $r2->getHeaderLine('foo'));
  105. }
  106. public function testNewInstanceWhenAddingHeaderArray()
  107. {
  108. $r = new Response(200, array('Foo' => 'Bar'));
  109. $r2 = $r->withAddedHeader('Foo', array('Baz', 'Qux'));
  110. $this->assertNotSame($r, $r2);
  111. $this->assertEquals(array('Bar', 'Baz', 'Qux'), $r2->getHeader('foo'));
  112. }
  113. public function testNewInstanceWhenAddingHeaderThatWasNotThereBefore()
  114. {
  115. $r = new Response(200, array('Foo' => 'Bar'));
  116. $r2 = $r->withAddedHeader('Baz', 'Bam');
  117. $this->assertNotSame($r, $r2);
  118. $this->assertEquals('Bam', $r2->getHeaderLine('Baz'));
  119. $this->assertEquals('Bar', $r2->getHeaderLine('Foo'));
  120. }
  121. public function testRemovesPreviouslyAddedHeaderOfDifferentCase()
  122. {
  123. $r = new Response(200, array('Foo' => 'Bar'));
  124. $r2 = $r->withHeader('foo', 'Bam');
  125. $this->assertNotSame($r, $r2);
  126. $this->assertEquals('Bam', $r2->getHeaderLine('Foo'));
  127. }
  128. public function testBodyConsistent()
  129. {
  130. $r = new Response(200, array(), '0');
  131. $this->assertEquals('0', (string)$r->getBody());
  132. }
  133. }