|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Deviantintegral\Har\Tests\Unit\Adapter\Psr7; |
| 6 | + |
| 7 | +use Deviantintegral\Har\Adapter\Psr7\Response; |
| 8 | +use Deviantintegral\Har\Tests\Unit\HarTestBase; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers \Deviantintegral\Har\Adapter\Psr7\Response |
| 12 | + * @covers \Deviantintegral\Har\Adapter\Psr7\MessageBase |
| 13 | + */ |
| 14 | +class ResponseTest extends HarTestBase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Response |
| 18 | + */ |
| 19 | + private $response; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->response = new Response( |
| 24 | + $this->getHarFileRepository()->load( |
| 25 | + 'www.softwareishard.com-single-entry.har' |
| 26 | + )->getLog()->getEntries()[0]->getResponse() |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + public function testGetStatusCode() |
| 31 | + { |
| 32 | + $this->assertEquals(200, $this->response->getStatusCode()); |
| 33 | + } |
| 34 | + |
| 35 | + public function testWithStatus() |
| 36 | + { |
| 37 | + $withStatus = $this->response->withStatus(404, 'Not Found'); |
| 38 | + $this->assertEquals(404, $withStatus->getStatusCode()); |
| 39 | + $this->assertEquals('Not Found', $withStatus->getReasonPhrase()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGetReasonPhrase() |
| 43 | + { |
| 44 | + $this->assertEquals('OK', $this->response->getReasonPhrase()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testGetHarResponse() |
| 48 | + { |
| 49 | + $harResponse = $this->response->getHarResponse(); |
| 50 | + $this->assertInstanceOf(\Deviantintegral\Har\Response::class, $harResponse); |
| 51 | + $this->assertEquals(200, $harResponse->getStatus()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testWithHeader() |
| 55 | + { |
| 56 | + $withHeader = $this->response->withHeader('X-Test', 'value'); |
| 57 | + $this->assertEquals(['value'], $withHeader->getHeader('X-Test')); |
| 58 | + |
| 59 | + $withArrayValue = $this->response->withHeader( |
| 60 | + 'X-Test', |
| 61 | + ['value1', 'value2'] |
| 62 | + ); |
| 63 | + $this->assertEquals( |
| 64 | + ['value1', 'value2'], |
| 65 | + $withArrayValue->getHeader('X-Test') |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function testGetHeaderLine() |
| 70 | + { |
| 71 | + $withMultiple = $this->response->withAddedHeader('X-Custom', 'value1') |
| 72 | + ->withAddedHeader('X-Custom', 'value2'); |
| 73 | + $this->assertEquals('value1, value2', $withMultiple->getHeaderLine('X-Custom')); |
| 74 | + } |
| 75 | + |
| 76 | + public function testGetHeaderLineWhenHeaderNotPresent() |
| 77 | + { |
| 78 | + $this->assertEquals('', $this->response->getHeaderLine('X-NonExistent')); |
| 79 | + } |
| 80 | + |
| 81 | + public function testGetHeader() |
| 82 | + { |
| 83 | + $headers = $this->response->getHeader('Content-Type'); |
| 84 | + $this->assertIsArray($headers); |
| 85 | + } |
| 86 | + |
| 87 | + public function testHasHeader() |
| 88 | + { |
| 89 | + $this->assertTrue($this->response->hasHeader('Content-Type')); |
| 90 | + $this->assertFalse($this->response->hasHeader('X-NonExistent')); |
| 91 | + } |
| 92 | + |
| 93 | + public function testGetHeaders() |
| 94 | + { |
| 95 | + $headers = $this->response->getHeaders(); |
| 96 | + $this->assertIsArray($headers); |
| 97 | + $this->assertArrayHasKey('Content-Type', $headers); |
| 98 | + } |
| 99 | + |
| 100 | + public function testGetProtocolVersion() |
| 101 | + { |
| 102 | + $this->assertEquals('1.1', $this->response->getProtocolVersion()); |
| 103 | + } |
| 104 | + |
| 105 | + public function testWithProtocolVersion() |
| 106 | + { |
| 107 | + $withProtocol = $this->response->withProtocolVersion('2.0'); |
| 108 | + $this->assertEquals('2.0', $withProtocol->getProtocolVersion()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testWithoutHeader() |
| 112 | + { |
| 113 | + $withoutHeader = $this->response->withoutHeader('Content-Type'); |
| 114 | + $this->assertFalse($withoutHeader->hasHeader('Content-Type')); |
| 115 | + } |
| 116 | + |
| 117 | + public function testWithAddedHeader() |
| 118 | + { |
| 119 | + $withAdded = $this->response->withAddedHeader('X-Custom', 'value1'); |
| 120 | + $this->assertEquals(['value1'], $withAdded->getHeader('X-Custom')); |
| 121 | + |
| 122 | + $withMultiple = $withAdded->withAddedHeader('X-Custom', 'value2'); |
| 123 | + $this->assertEquals(['value1', 'value2'], $withMultiple->getHeader('X-Custom')); |
| 124 | + } |
| 125 | + |
| 126 | + public function testWithAddedHeaderArrayValue() |
| 127 | + { |
| 128 | + $withAdded = $this->response->withAddedHeader('X-Custom', ['value1', 'value2']); |
| 129 | + $this->assertEquals(['value1', 'value2'], $withAdded->getHeader('X-Custom')); |
| 130 | + } |
| 131 | +} |
0 commit comments