* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class RequestParserTest extends TestCase { public function testParseStrictMode() { $request = 'GET /foobar?foo=bar#fragment HTTP/1.1' . Http::NEW_LINE; $request.= 'Content-Type: text/plain' . Http::NEW_LINE; $request.= 'User-Agent: psx' . Http::NEW_LINE; $request.= Http::NEW_LINE; $request.= 'Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.'; $parser = new RequestParser(new Url('http://localhost.com'), RequestParser::MODE_STRICT); $request = $parser->parse($request); $this->assertInstanceOf(RequestInterface::class, $request); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('http://localhost.com/foobar?foo=bar#fragment', $request->getUri()->toString()); $this->assertEquals('HTTP/1.1', $request->getProtocolVersion()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'user-agent' => ['psx'], ), $request->getHeaders()); $this->assertEquals('Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.', $request->getBody()); } public function testParseLooseMode() { $parser = new RequestParser(new Url('http://localhost.com'), RequestParser::MODE_LOOSE); $seperators = array("\r\n", "\n", "\r"); foreach ($seperators as $newline) { $request = 'GET /foobar?foo=bar#fragment HTTP/1.1' . $newline; $request.= 'Content-Type: text/plain' . $newline; $request.= 'User-Agent: psx' . $newline; $request.= $newline; $request.= 'Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.'; $request = $parser->parse($request); $this->assertInstanceOf(RequestInterface::class, $request); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('http://localhost.com/foobar?foo=bar#fragment', $request->getUri()->toString()); $this->assertEquals('HTTP/1.1', $request->getProtocolVersion()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'user-agent' => ['psx'], ), $request->getHeaders()); $this->assertEquals('Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.', $request->getBody()); } } public function testParseNoBaseUrl() { $request = 'GET /foobar?foo=bar#fragment HTTP/1.1' . Http::NEW_LINE; $request.= 'Content-Type: text/plain' . Http::NEW_LINE; $request.= 'User-Agent: psx' . Http::NEW_LINE; $request.= Http::NEW_LINE; $request.= 'Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.'; $parser = new RequestParser(); $request = $parser->parse($request); $this->assertInstanceOf(RequestInterface::class, $request); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('/foobar?foo=bar#fragment', $request->getUri()->toString()); $this->assertEquals('HTTP/1.1', $request->getProtocolVersion()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'user-agent' => ['psx'], ), $request->getHeaders()); $this->assertEquals('Google is built by a large team of engineers, designers, researchers, robots, and others in many different sites across the globe. It is updated continuously, and built with more tools and technologies than we can shake a stick at. If you\'d like to help us out, see google.com/jobs.', $request->getBody()); } public function testParseInvalidStatusLine() { $this->expectException(ParseException::class); $request = 'foobar' . Http::NEW_LINE; $request.= 'Vary: Accept-Encoding' . Http::NEW_LINE; $parser = new RequestParser(new Url('http://localhost.com'), RequestParser::MODE_STRICT); $parser->parse($request); } public function testParseEmpty() { $this->expectException(\InvalidArgumentException::class); $request = ''; $parser = new RequestParser(new Url('http://localhost.com'), RequestParser::MODE_STRICT); $parser->parse($request); } public function testParseNoLineEnding() { $this->expectException(ParseException::class); $request = 'GET /foobar?foo=bar#fragment HTTP/1.1'; $request.= 'Vary: Accept-Encoding'; $parser = new RequestParser(new Url('http://localhost.com'), RequestParser::MODE_STRICT); $parser->parse($request); } public function testParseInvalidMode() { $this->expectException(\InvalidArgumentException::class); $request = 'GET /foobar?foo=bar#fragment HTTP/1.1' . Http::NEW_LINE; $request.= 'Content-Type: text/plain' . Http::NEW_LINE; $parser = new RequestParser(new Url('http://localhost.com'), 'foo'); $parser->parse($request); } public function testBuildStatusLine() { $request = new Request(new Url('http://127.0.0.1'), 'GET'); $this->assertEquals('GET / HTTP/1.1', RequestParser::buildStatusLine($request)); } public function testBuildStatusLineNoTarget() { $this->expectException(\RuntimeException::class); $request = new Request(new Url('http://127.0.0.1'), 'GET'); $request->setRequestTarget(''); RequestParser::buildStatusLine($request); } public function testConvert() { $httpRequest = 'GET /foo/bar?foo=bar#test HTTP/1.1' . Http::NEW_LINE; $httpRequest.= 'Content-type: text/html; charset=UTF-8' . Http::NEW_LINE; $httpRequest.= Http::NEW_LINE; $httpRequest.= 'foobar'; $request = RequestParser::convert($httpRequest, new Url('http://psx.dev')); $this->assertEquals('http://psx.dev/foo/bar?foo=bar#test', $request->getUri()->toString()); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('HTTP/1.1', $request->getProtocolVersion()); $this->assertEquals('text/html; charset=UTF-8', (string) $request->getHeader('Content-Type')); $this->assertEquals('foobar', $request->getBody()); } } __halt_compiler();----SIGNATURE:----HHSLuxREnUaLx8YzeuAxLEGpzKY/xP8Zkij+Vu/bFMRTQ3KiJPB8nNeyAe0C60U5JTLTmpzA6qk8yQkPzFvkmnITIt/qu8+TQF5M1v+cOGcH9VxEdf/1krNR4noJAEFGEtTeRJ7audF7p+dEdq8aOyyPWBuARIA5aIRe8dLqd/i+3jjxCQhS7yhEBvhsNPJlmdFrADt3NdxB2Do/3nr9iUbCk5OQh8zOxwZKEoBqb3QysTGaWtARGwoIdax6L56mqCfCGrHKDN2sfE1cMkwW0+4br21Wb5SfJE6Cv7vsTuuAHYv1d8llO9uZ3LX1NMrXVtE/8ujROXfE56a+hvFYW2i99mwvbm/Us65vyVcgYQCFeNLzU1ZViPzipbt3+Er/G7BQbaJvRw3Bv5upgYt6HtpAGICkMRGq03rqzPCxShYqqcC8Nk1w03d2NonLfKY3q3ZmBJ0fxuNYoSN/d40TxJR5pDyGgEAfyOQxBm2TtzTsCS2lcaclPTvGkwt5Yrtqzd8TSX0eEJP2CHh8OP5ZX2IiL29f6sX9Ujmr0sFTWGAMV8Z4i4+UA8v1vQgdA/LTwMScfebPGinmFnUvfOv4Z4ZARLeT2jH47g49cbU3sbr7EEiZvlQQQJk5FBny8wpGB0ah3Uljej25sNuvQElCm+ZZZBT7KV1p8IPJjRkMD5Y=----ATTACHMENT:----OTU1NTM4ODM0NTk4MjYxOCA0MTkzOTQxMTE3OTI1MTIgNjAwNzI0NTI5MzU1MzQ0Mg==