* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class ResponseParserTest extends TestCase { public function testParseStrictMode() { $response = 'HTTP/1.1 200 OK' . Http::NEW_LINE; $response.= 'Vary: Accept-Encoding' . Http::NEW_LINE; $response.= 'Content-Type: text/plain' . Http::NEW_LINE; $response.= 'Last-Modified: Mon, 02 Apr 2012 02:13:37 GMT' . Http::NEW_LINE; $response.= 'Date: Sat, 07 Dec 2013 13:27:33 GMT' . Http::NEW_LINE; $response.= 'Expires: Sat, 07 Dec 2013 13:27:33 GMT' . Http::NEW_LINE; $response.= 'Cache-Control: public, max-age=0' . Http::NEW_LINE; $response.= 'X-Content-Type-Options: nosniff' . Http::NEW_LINE; $response.= 'Server: sffe' . Http::NEW_LINE; $response.= 'X-XSS-Protection: 1; mode=block' . Http::NEW_LINE; $response.= 'Alternate-Protocol: 80:quic' . Http::NEW_LINE; $response.= 'Transfer-Encoding: chunked' . Http::NEW_LINE; $response.= Http::NEW_LINE; $response.= '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 ResponseParser(ResponseParser::MODE_STRICT); $response = $parser->parse($response); $this->assertInstanceOf('PSX\Http\Response', $response); $this->assertEquals('HTTP/1.1', $response->getProtocolVersion()); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'date' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'vary' => ['Accept-Encoding'], 'last-modified' => ['Mon, 02 Apr 2012 02:13:37 GMT'], 'expires' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'cache-control' => ['public, max-age=0'], 'x-content-type-options' => ['nosniff'], 'server' => ['sffe'], 'x-xss-protection' => ['1; mode=block'], 'alternate-protocol' => ['80:quic'], 'transfer-encoding' => ['chunked'], ), $response->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.', $response->getBody()); } public function testParseLooseMode() { $parser = new ResponseParser(ResponseParser::MODE_LOOSE); $seperators = array("\r\n", "\n", "\r"); foreach ($seperators as $newline) { $response = 'HTTP/1.1 200 OK' . $newline; $response.= 'Vary: Accept-Encoding' . $newline; $response.= 'Content-Type: text/plain' . $newline; $response.= 'Last-Modified: Mon, 02 Apr 2012 02:13:37 GMT' . $newline; $response.= 'Date: Sat, 07 Dec 2013 13:27:33 GMT' . $newline; $response.= 'Expires: Sat, 07 Dec 2013 13:27:33 GMT' . $newline; $response.= 'Cache-Control: public, max-age=0' . $newline; $response.= 'X-Content-Type-Options: nosniff' . $newline; $response.= 'Server: sffe' . $newline; $response.= 'X-XSS-Protection: 1; mode=block' . $newline; $response.= 'Alternate-Protocol: 80:quic' . $newline; $response.= 'Transfer-Encoding: chunked' . $newline; $response.= $newline; $response.= '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.'; $response = $parser->parse($response); $this->assertInstanceOf('PSX\Http\Response', $response); $this->assertEquals('HTTP/1.1', $response->getProtocolVersion()); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'date' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'vary' => ['Accept-Encoding'], 'last-modified' => ['Mon, 02 Apr 2012 02:13:37 GMT'], 'expires' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'cache-control' => ['public, max-age=0'], 'x-content-type-options' => ['nosniff'], 'server' => ['sffe'], 'x-xss-protection' => ['1; mode=block'], 'alternate-protocol' => ['80:quic'], 'transfer-encoding' => ['chunked'], ), $response->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.', $response->getBody()); } } public function testParseInvalidStatusLine() { $this->expectException(ParseException::class); $response = 'foobar' . Http::NEW_LINE; $response.= 'Vary: Accept-Encoding' . Http::NEW_LINE; $parser = new ResponseParser(ResponseParser::MODE_STRICT); $parser->parse($response); } public function testParseEmpty() { $this->expectException(\InvalidArgumentException::class); $response = ''; $parser = new ResponseParser(ResponseParser::MODE_STRICT); $parser->parse($response); } public function testParseNoLineEnding() { $this->expectException(ParseException::class); $response = 'HTTP/1.1 200 OK'; $response.= 'Vary: Accept-Encoding'; $parser = new ResponseParser(ResponseParser::MODE_STRICT); $parser->parse($response); } public function testParseInvalidMode() { $this->expectException(\InvalidArgumentException::class); $response = 'HTTP/1.1 200 OK' . Http::NEW_LINE; $response.= 'Vary: Accept-Encoding' . Http::NEW_LINE; $parser = new ResponseParser('foo'); $parser->parse($response); } public function testBuildResponseFromHeader() { $response = ResponseParser::buildResponseFromHeader(array( 'HTTP/1.1 200 OK', 'Vary: Accept-Encoding', 'Content-Type: text/plain', 'Last-Modified: Mon, 02 Apr 2012 02:13:37 GMT', 'Date: Sat, 07 Dec 2013 13:27:33 GMT', 'Expires: Sat, 07 Dec 2013 13:27:33 GMT', 'Cache-Control: public, max-age=0', 'X-Content-Type-Options: nosniff', 'Server: sffe', 'X-XSS-Protection: 1; mode=block', 'Alternate-Protocol: 80:quic', 'Transfer-Encoding: chunked', )); $this->assertInstanceOf('PSX\Http\Response', $response); $this->assertEquals('HTTP/1.1', $response->getProtocolVersion()); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(array( 'content-type' => ['text/plain'], 'date' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'vary' => ['Accept-Encoding'], 'last-modified' => ['Mon, 02 Apr 2012 02:13:37 GMT'], 'expires' => ['Sat, 07 Dec 2013 13:27:33 GMT'], 'cache-control' => ['public, max-age=0'], 'x-content-type-options' => ['nosniff'], 'server' => ['sffe'], 'x-xss-protection' => ['1; mode=block'], 'alternate-protocol' => ['80:quic'], 'transfer-encoding' => ['chunked'], ), $response->getHeaders()); } public function testBuildResponseFromHeaderInvalidStatusLine() { $this->expectException(ParseException::class); ResponseParser::buildResponseFromHeader(array( 'foobar', 'Vary: Accept-Encoding', )); } public function testBuildResponseFromHeaderEmpty() { $this->expectException(ParseException::class); ResponseParser::buildResponseFromHeader(array()); } public function testBuildResponseFromHeaderEmptyStatusLine() { $this->expectException(ParseException::class); ResponseParser::buildResponseFromHeader(array( '', 'Vary: Accept-Encoding', )); } public function testBuildStatusLine() { $response = new Response(200); $this->assertEquals('HTTP/1.1 200 OK', ResponseParser::buildStatusLine($response)); } public function testBuildStatusLineUnknownStausCode() { $this->expectException(\RuntimeException::class); $response = new Response(0); ResponseParser::buildStatusLine($response); } public function testBuildStatusLineUnknownStausCodeWithNoReason() { $this->expectException(\RuntimeException::class); $response = new Response(800); ResponseParser::buildStatusLine($response); } public function testBuildStatusLineUnknownStausCodeWithReason() { $response = new Response(); $response->setStatus(800, 'Foo'); $this->assertEquals('HTTP/1.1 800 Foo', ResponseParser::buildStatusLine($response)); } public function testConvert() { $httpResponse = 'HTTP/1.1 200 OK' . Http::NEW_LINE; $httpResponse.= 'Content-type: text/html; charset=UTF-8' . Http::NEW_LINE; $httpResponse.= Http::NEW_LINE; $httpResponse.= 'foobar'; $response = ResponseParser::convert($httpResponse); $this->assertEquals('HTTP/1.1', $response->getProtocolVersion()); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals('text/html; charset=UTF-8', (string) $response->getHeader('Content-Type')); $this->assertEquals('foobar', $response->getBody()); } } __halt_compiler();----SIGNATURE:----K6uS9G8bT95Fx2wA2atjqO8D+rhN9WWsEzDnU8+vVFx+MXMF9WfwziVF5wgdfCxP7WCC2Ao2XWOs+gFdVdDl5Srhv4o8QA6KmvbpQE92+TAWI+FiRhjgXh2ggmlxEJRRJeobPWEJFknwBFV5cc6lUAdk1et/3CgogLtG46iCNcM39PVSKdMp4XuWZgVziqsMOHwx+UEV1GbGX4yxM+NttnAkk+QVXw6dMfPXvrXMTHFGk8WTfq1nn9G2gVzIVEMvZuzzsar7MAM62W9B17oxlLde2TRWs8ypWiFxu+JmTAT7211MyMRYcKNQSeP7RMXhNSPUE97AoHAkNMot/DDpPGfDI66dr7dz6rxvNQkLql/HTQqPv2O/o7WUQhNqa8k9vN5JSGfnNEf529NJ7zCUZYSGjRue4FzqkwHmKQxiE32oLQS8IyaKgXsjAshrUXIPNxMKNbLhwTm4C69tjVVAykaVqM1DT6brYbeLHm3SVHrDTLDq6KMeSuUmxAs+b4gi/FTlS1SipvIPhPgl22R6h+E6uqG57gUdNJOhaaPgjWvw6v8yGjl6joL8dxYlQG52x7VhSy0NKy/zzsz7MeGn7RC6nSQTgSKm0q6luHyvebVR4rF6w1m2XPsSHUJUp6FY59x10pt6rgOL6DlKFyG4Mp/J5WRtFala7wRyu+9Mbyc=----ATTACHMENT:----NDkwNjgyMDY3NzU1NzEyNiAzMDY2ODAxODM5MTg5NzAgMTUyMTYwNjkzMzE3NTE4Mg==