1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
<?php
namespace GuzzleHttp\Tests\Psr7;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\FnStream;
class MessageTest extends BaseTest
{
public function testConvertsRequestsToStrings()
{
$request = new Psr7\Request('PUT', 'http://foo.com/hi?123', [
'Baz' => 'bar',
'Qux' => 'ipsum',
], 'hello', '1.0');
$this->assertSame(
"PUT /hi?123 HTTP/1.0\r\nHost: foo.com\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello",
Psr7\Message::toString($request)
);
}
public function testConvertsResponsesToStrings()
{
$response = new Psr7\Response(200, [
'Baz' => 'bar',
'Qux' => 'ipsum',
], 'hello', '1.0', 'FOO');
$this->assertSame(
"HTTP/1.0 200 FOO\r\nBaz: bar\r\nQux: ipsum\r\n\r\nhello",
Psr7\Message::toString($response)
);
}
public function testCorrectlyRendersSetCookieHeadersToString()
{
$response = new Psr7\Response(200, [
'Set-Cookie' => ['bar','baz','qux']
], 'hello', '1.0', 'FOO');
$this->assertSame(
"HTTP/1.0 200 FOO\r\nSet-Cookie: bar\r\nSet-Cookie: baz\r\nSet-Cookie: qux\r\n\r\nhello",
Psr7\Message::toString($response)
);
}
public function testRewindsBody()
{
$body = Psr7\Utils::streamFor('abc');
$res = new Psr7\Response(200, [], $body);
Psr7\Message::rewindBody($res);
$this->assertSame(0, $body->tell());
$body->rewind();
Psr7\Message::rewindBody($res);
$this->assertSame(0, $body->tell());
}
public function testThrowsWhenBodyCannotBeRewound()
{
$body = Psr7\Utils::streamFor('abc');
$body->read(1);
$body = FnStream::decorate($body, [
'rewind' => function () {
throw new \RuntimeException('a');
},
]);
$res = new Psr7\Response(200, [], $body);
$this->expectExceptionGuzzle('RuntimeException');
Psr7\Message::rewindBody($res);
}
public function testParsesRequestMessages()
{
$req = "GET /abc HTTP/1.0\r\nHost: foo.com\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('GET', $request->getMethod());
$this->assertSame('/abc', $request->getRequestTarget());
$this->assertSame('1.0', $request->getProtocolVersion());
$this->assertSame('foo.com', $request->getHeaderLine('Host'));
$this->assertSame('Bar', $request->getHeaderLine('Foo'));
$this->assertSame('Bam, Qux', $request->getHeaderLine('Baz'));
$this->assertSame('Test', (string)$request->getBody());
$this->assertSame('http://foo.com/abc', (string)$request->getUri());
}
public function testParsesRequestMessagesWithHttpsScheme()
{
$req = "PUT /abc?baz=bar HTTP/1.1\r\nHost: foo.com:443\r\n\r\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/abc?baz=bar', $request->getRequestTarget());
$this->assertSame('1.1', $request->getProtocolVersion());
$this->assertSame('foo.com:443', $request->getHeaderLine('Host'));
$this->assertSame('', (string)$request->getBody());
$this->assertSame('https://foo.com/abc?baz=bar', (string)$request->getUri());
}
public function testParsesRequestMessagesWithUriWhenHostIsNotFirst()
{
$req = "PUT / HTTP/1.1\r\nFoo: Bar\r\nHost: foo.com\r\n\r\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/', $request->getRequestTarget());
$this->assertSame('http://foo.com/', (string)$request->getUri());
}
public function testParsesRequestMessagesWithFullUri()
{
$req = "GET https://www.google.com:443/search?q=foobar HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('GET', $request->getMethod());
$this->assertSame('https://www.google.com:443/search?q=foobar', $request->getRequestTarget());
$this->assertSame('1.1', $request->getProtocolVersion());
$this->assertSame('www.google.com', $request->getHeaderLine('Host'));
$this->assertSame('', (string)$request->getBody());
$this->assertSame('https://www.google.com/search?q=foobar', (string)$request->getUri());
}
public function testParsesRequestMessagesWithCustomMethod()
{
$req = "GET_DATA / HTTP/1.1\r\nFoo: Bar\r\nHost: foo.com\r\n\r\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('GET_DATA', $request->getMethod());
}
public function testParsesRequestMessagesWithFoldedHeadersOnHttp10()
{
$req = "PUT / HTTP/1.0\r\nFoo: Bar\r\n Bam\r\n\r\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/', $request->getRequestTarget());
$this->assertSame('Bar Bam', $request->getHeaderLine('Foo'));
}
public function testRequestParsingFailsWithFoldedHeadersOnHttp11()
{
$this->expectExceptionGuzzle('InvalidArgumentException', 'Invalid header syntax: Obsolete line folding');
Psr7\Message::parseResponse("GET_DATA / HTTP/1.1\r\nFoo: Bar\r\n Biz: Bam\r\n\r\n");
}
public function testParsesRequestMessagesWhenHeaderDelimiterIsOnlyALineFeed()
{
$req = "PUT / HTTP/1.0\nFoo: Bar\nBaz: Bam\n\n";
$request = Psr7\Message::parseRequest($req);
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/', $request->getRequestTarget());
$this->assertSame('Bar', $request->getHeaderLine('Foo'));
$this->assertSame('Bam', $request->getHeaderLine('Baz'));
}
public function testValidatesRequestMessages()
{
$this->expectExceptionGuzzle('InvalidArgumentException');
Psr7\Message::parseRequest("HTTP/1.1 200 OK\r\n\r\n");
}
public function testParsesResponseMessages()
{
$res = "HTTP/1.0 200 OK\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest";
$response = Psr7\Message::parseResponse($res);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame('1.0', $response->getProtocolVersion());
$this->assertSame('Bar', $response->getHeaderLine('Foo'));
$this->assertSame('Bam, Qux', $response->getHeaderLine('Baz'));
$this->assertSame('Test', (string)$response->getBody());
}
public function testParsesResponseWithoutReason()
{
$res = "HTTP/1.0 200\r\nFoo: Bar\r\nBaz: Bam\r\nBaz: Qux\r\n\r\nTest";
$response = Psr7\Message::parseResponse($res);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame('1.0', $response->getProtocolVersion());
$this->assertSame('Bar', $response->getHeaderLine('Foo'));
$this->assertSame('Bam, Qux', $response->getHeaderLine('Baz'));
$this->assertSame('Test', (string)$response->getBody());
}
public function testParsesResponseWithLeadingDelimiter()
{
$res = "\r\nHTTP/1.0 200\r\nFoo: Bar\r\n\r\nTest";
$response = Psr7\Message::parseResponse($res);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame('1.0', $response->getProtocolVersion());
$this->assertSame('Bar', $response->getHeaderLine('Foo'));
$this->assertSame('Test', (string)$response->getBody());
}
public function testParsesResponseWithFoldedHeadersOnHttp10()
{
$res = "HTTP/1.0 200\r\nFoo: Bar\r\n Bam\r\n\r\nTest";
$response = Psr7\Message::parseResponse($res);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame('1.0', $response->getProtocolVersion());
$this->assertSame('Bar Bam', $response->getHeaderLine('Foo'));
$this->assertSame('Test', (string)$response->getBody());
}
public function testResponseParsingFailsWithFoldedHeadersOnHttp11()
{
$this->expectExceptionGuzzle('InvalidArgumentException', 'Invalid header syntax: Obsolete line folding');
Psr7\Message::parseResponse("HTTP/1.1 200\r\nFoo: Bar\r\n Biz: Bam\r\nBaz: Qux\r\n\r\nTest");
}
public function testParsesResponseWhenHeaderDelimiterIsOnlyALineFeed()
{
$res = "HTTP/1.0 200\nFoo: Bar\nBaz: Bam\n\nTest\n\nOtherTest";
$response = Psr7\Message::parseResponse($res);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', $response->getReasonPhrase());
$this->assertSame('1.0', $response->getProtocolVersion());
$this->assertSame('Bar', $response->getHeaderLine('Foo'));
$this->assertSame('Bam', $response->getHeaderLine('Baz'));
$this->assertSame("Test\n\nOtherTest", (string)$response->getBody());
}
public function testResponseParsingFailsWithoutHeaderDelimiter()
{
$this->expectExceptionGuzzle('InvalidArgumentException', 'Invalid message: Missing header delimiter');
Psr7\Message::parseResponse("HTTP/1.0 200\r\nFoo: Bar\r\n Baz: Bam\r\nBaz: Qux\r\n");
}
public function testValidatesResponseMessages()
{
$this->expectExceptionGuzzle('InvalidArgumentException');
Psr7\Message::parseResponse("GET / HTTP/1.1\r\n\r\n");
}
public function testMessageBodySummaryWithSmallBody()
{
$message = new Psr7\Response(200, [], 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
$this->assertSame('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', Psr7\Message::bodySummary($message));
}
public function testMessageBodySummaryWithLargeBody()
{
$message = new Psr7\Response(200, [], 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
$this->assertSame('Lorem ipsu (truncated...)', Psr7\Message::bodySummary($message, 10));
}
public function testMessageBodySummaryWithSpecialUTF8Characters()
{
$message = new Psr7\Response(200, [], '’é€௵ဪ‱');
self::assertSame('’é€௵ဪ‱', Psr7\Message::bodySummary($message));
}
public function testMessageBodySummaryWithEmptyBody()
{
$message = new Psr7\Response(200, [], '');
$this->assertNull(Psr7\Message::bodySummary($message));
}
public function testGetResponseBodySummaryOfNonReadableStream()
{
$this->assertNull(Psr7\Message::bodySummary(new Psr7\Response(500, [], new ReadSeekOnlyStream())));
}
}
|