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