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