public function testResponse() { $response = new Response(); $response->setHeader('Content-Type', 'text/plain')->setBody('hello')->setCode(200); $this->assertEquals('text/plain', $response->getHeader('Content-Type')); $this->assertEquals('hello', $response->getBody()); $this->assertEquals(200, $response->getCode()); $this->assertEquals("HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\nhello", (string) $response); }
/** * @param $output * @param string $errorOutput * * @return Response */ protected function parseOutput($output, $errorOutput = '') { $response = new Response(); list($header, $body) = explode("\r\n\r\n", $output, 2); $response->setBody($body); $response->setError($errorOutput); $header = new Text($header); $cookies = []; foreach ($header->lines() as $line) { list($name, $value) = explode(':', $line, 2); if ('Set-Cookie' == $name) { $cookies[] = trim($value); } else { $response->setHeader(trim($name), trim($value)); } } if (count($cookies)) { $response->setHeader('Set-Cookie', $cookies); } return $response; }
/** * @param int $code * @param \Exception $e * * @return Response */ protected function handleError($code, \Exception $e) { $path = __DIR__ . sprintf('/Resources/html/%d.html', $code); if (file_exists($path)) { $response = new Response(file_get_contents($path), $code); } else { $response = new Response(sprintf('<html><body><h1>Error %d</h1><p>%s</p></body></html>', $code, $e->getMessage()), $code); } return $response->setError($e->getMessage()); }