| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests\Psr7; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7; |
| 5 |
use GuzzleHttp\Psr7\Request; |
| 6 |
use GuzzleHttp\Psr7\Uri; |
| 7 |
|
| 8 |
/** |
| 9 |
* @covers GuzzleHttp\Psr7\MessageTrait |
| 10 |
* @covers GuzzleHttp\Psr7\Request |
| 11 |
*/ |
| 12 |
class RequestTest extends BaseTest |
| 13 |
{ |
| 14 |
public function testRequestUriMayBeString() |
| 15 |
{ |
| 16 |
$r = new Request('GET', '/'); |
| 17 |
$this->assertEquals('/', (string) $r->getUri()); |
| 18 |
} |
| 19 |
|
| 20 |
public function testRequestUriMayBeUri() |
| 21 |
{ |
| 22 |
$uri = new Uri('/'); |
| 23 |
$r = new Request('GET', $uri); |
| 24 |
$this->assertSame($uri, $r->getUri()); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @expectedException \InvalidArgumentException |
| 29 |
*/ |
| 30 |
public function testValidateRequestUri() |
| 31 |
{ |
| 32 |
new Request('GET', '///'); |
| 33 |
} |
| 34 |
|
| 35 |
public function testCanConstructWithBody() |
| 36 |
{ |
| 37 |
$r = new Request('GET', '/', [], 'baz'); |
| 38 |
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody()); |
| 39 |
$this->assertEquals('baz', (string) $r->getBody()); |
| 40 |
} |
| 41 |
|
| 42 |
public function testNullBody() |
| 43 |
{ |
| 44 |
$r = new Request('GET', '/', [], null); |
| 45 |
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody()); |
| 46 |
$this->assertSame('', (string) $r->getBody()); |
| 47 |
} |
| 48 |
|
| 49 |
public function testFalseyBody() |
| 50 |
{ |
| 51 |
$r = new Request('GET', '/', [], '0'); |
| 52 |
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $r->getBody()); |
| 53 |
$this->assertSame('0', (string) $r->getBody()); |
| 54 |
} |
| 55 |
|
| 56 |
public function testConstructorDoesNotReadStreamBody() |
| 57 |
{ |
| 58 |
$streamIsRead = false; |
| 59 |
$body = Psr7\FnStream::decorate(Psr7\stream_for(''), [ |
| 60 |
'__toString' => function () use (&$streamIsRead) { |
| 61 |
$streamIsRead = true; |
| 62 |
return ''; |
| 63 |
} |
| 64 |
]); |
| 65 |
|
| 66 |
$r = new Request('GET', '/', [], $body); |
| 67 |
$this->assertFalse($streamIsRead); |
| 68 |
$this->assertSame($body, $r->getBody()); |
| 69 |
} |
| 70 |
|
| 71 |
public function testCapitalizesMethod() |
| 72 |
{ |
| 73 |
$r = new Request('get', '/'); |
| 74 |
$this->assertEquals('GET', $r->getMethod()); |
| 75 |
} |
| 76 |
|
| 77 |
public function testCapitalizesWithMethod() |
| 78 |
{ |
| 79 |
$r = new Request('GET', '/'); |
| 80 |
$this->assertEquals('PUT', $r->withMethod('put')->getMethod()); |
| 81 |
} |
| 82 |
|
| 83 |
public function testWithUri() |
| 84 |
{ |
| 85 |
$r1 = new Request('GET', '/'); |
| 86 |
$u1 = $r1->getUri(); |
| 87 |
$u2 = new Uri('http://www.example.com'); |
| 88 |
$r2 = $r1->withUri($u2); |
| 89 |
$this->assertNotSame($r1, $r2); |
| 90 |
$this->assertSame($u2, $r2->getUri()); |
| 91 |
$this->assertSame($u1, $r1->getUri()); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @dataProvider invalidMethodsProvider |
| 96 |
*/ |
| 97 |
public function testConstructWithInvalidMethods($method) |
| 98 |
{ |
| 99 |
$this->expectException('InvalidArgumentException'); |
| 100 |
new Request($method, '/'); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @dataProvider invalidMethodsProvider |
| 105 |
*/ |
| 106 |
public function testWithInvalidMethods($method) |
| 107 |
{ |
| 108 |
$r = new Request('get', '/'); |
| 109 |
$this->expectException('InvalidArgumentException'); |
| 110 |
$r->withMethod($method); |
| 111 |
} |
| 112 |
|
| 113 |
public function invalidMethodsProvider() |
| 114 |
{ |
| 115 |
return [ |
| 116 |
[null], |
| 117 |
[false], |
| 118 |
[['foo']], |
| 119 |
[new \stdClass()], |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
public function testSameInstanceWhenSameUri() |
| 124 |
{ |
| 125 |
$r1 = new Request('GET', 'http://foo.com'); |
| 126 |
$r2 = $r1->withUri($r1->getUri()); |
| 127 |
$this->assertSame($r1, $r2); |
| 128 |
} |
| 129 |
|
| 130 |
public function testWithRequestTarget() |
| 131 |
{ |
| 132 |
$r1 = new Request('GET', '/'); |
| 133 |
$r2 = $r1->withRequestTarget('*'); |
| 134 |
$this->assertEquals('*', $r2->getRequestTarget()); |
| 135 |
$this->assertEquals('/', $r1->getRequestTarget()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @expectedException \InvalidArgumentException |
| 140 |
*/ |
| 141 |
public function testRequestTargetDoesNotAllowSpaces() |
| 142 |
{ |
| 143 |
$r1 = new Request('GET', '/'); |
| 144 |
$r1->withRequestTarget('/foo bar'); |
| 145 |
} |
| 146 |
|
| 147 |
public function testRequestTargetDefaultsToSlash() |
| 148 |
{ |
| 149 |
$r1 = new Request('GET', ''); |
| 150 |
$this->assertEquals('/', $r1->getRequestTarget()); |
| 151 |
$r2 = new Request('GET', '*'); |
| 152 |
$this->assertEquals('*', $r2->getRequestTarget()); |
| 153 |
$r3 = new Request('GET', 'http://foo.com/bar baz/'); |
| 154 |
$this->assertEquals('/bar%20baz/', $r3->getRequestTarget()); |
| 155 |
} |
| 156 |
|
| 157 |
public function testBuildsRequestTarget() |
| 158 |
{ |
| 159 |
$r1 = new Request('GET', 'http://foo.com/baz?bar=bam'); |
| 160 |
$this->assertEquals('/baz?bar=bam', $r1->getRequestTarget()); |
| 161 |
} |
| 162 |
|
| 163 |
public function testBuildsRequestTargetWithFalseyQuery() |
| 164 |
{ |
| 165 |
$r1 = new Request('GET', 'http://foo.com/baz?0'); |
| 166 |
$this->assertEquals('/baz?0', $r1->getRequestTarget()); |
| 167 |
} |
| 168 |
|
| 169 |
public function testHostIsAddedFirst() |
| 170 |
{ |
| 171 |
$r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Foo' => 'Bar']); |
| 172 |
$this->assertEquals([ |
| 173 |
'Host' => ['foo.com'], |
| 174 |
'Foo' => ['Bar'] |
| 175 |
], $r->getHeaders()); |
| 176 |
} |
| 177 |
|
| 178 |
public function testCanGetHeaderAsCsv() |
| 179 |
{ |
| 180 |
$r = new Request('GET', 'http://foo.com/baz?bar=bam', [ |
| 181 |
'Foo' => ['a', 'b', 'c'] |
| 182 |
]); |
| 183 |
$this->assertEquals('a, b, c', $r->getHeaderLine('Foo')); |
| 184 |
$this->assertEquals('', $r->getHeaderLine('Bar')); |
| 185 |
} |
| 186 |
|
| 187 |
public function testHostIsNotOverwrittenWhenPreservingHost() |
| 188 |
{ |
| 189 |
$r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Host' => 'a.com']); |
| 190 |
$this->assertEquals(['Host' => ['a.com']], $r->getHeaders()); |
| 191 |
$r2 = $r->withUri(new Uri('http://www.foo.com/bar'), true); |
| 192 |
$this->assertEquals('a.com', $r2->getHeaderLine('Host')); |
| 193 |
} |
| 194 |
|
| 195 |
public function testWithUriSetsHostIfNotSet() |
| 196 |
{ |
| 197 |
$r = (new Request('GET', 'http://foo.com/baz?bar=bam'))->withoutHeader('Host'); |
| 198 |
$this->assertEquals([], $r->getHeaders()); |
| 199 |
$r2 = $r->withUri(new Uri('http://www.baz.com/bar'), true); |
| 200 |
$this->assertSame('www.baz.com', $r2->getHeaderLine('Host')); |
| 201 |
} |
| 202 |
|
| 203 |
public function testOverridesHostWithUri() |
| 204 |
{ |
| 205 |
$r = new Request('GET', 'http://foo.com/baz?bar=bam'); |
| 206 |
$this->assertEquals(['Host' => ['foo.com']], $r->getHeaders()); |
| 207 |
$r2 = $r->withUri(new Uri('http://www.baz.com/bar')); |
| 208 |
$this->assertEquals('www.baz.com', $r2->getHeaderLine('Host')); |
| 209 |
} |
| 210 |
|
| 211 |
public function testAggregatesHeaders() |
| 212 |
{ |
| 213 |
$r = new Request('GET', '', [ |
| 214 |
'ZOO' => 'zoobar', |
| 215 |
'zoo' => ['foobar', 'zoobar'] |
| 216 |
]); |
| 217 |
$this->assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $r->getHeaders()); |
| 218 |
$this->assertEquals('zoobar, foobar, zoobar', $r->getHeaderLine('zoo')); |
| 219 |
} |
| 220 |
|
| 221 |
public function testAddsPortToHeader() |
| 222 |
{ |
| 223 |
$r = new Request('GET', 'http://foo.com:8124/bar'); |
| 224 |
$this->assertEquals('foo.com:8124', $r->getHeaderLine('host')); |
| 225 |
} |
| 226 |
|
| 227 |
public function testAddsPortToHeaderAndReplacePreviousPort() |
| 228 |
{ |
| 229 |
$r = new Request('GET', 'http://foo.com:8124/bar'); |
| 230 |
$r = $r->withUri(new Uri('http://foo.com:8125/bar')); |
| 231 |
$this->assertEquals('foo.com:8125', $r->getHeaderLine('host')); |
| 232 |
} |
| 233 |
} |
| 234 |
|