public function testConstruct() { // Test no params $res = new Response(); $this->assertInstanceOf('Windwalker\\Http\\Stream\\Stream', $res->getBody()); $this->assertEquals('php://memory', $res->getBody()->getMetadata('uri')); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals(array(), $res->getHeaders()); // Test with params $body = fopen($tmpfile = tempnam(sys_get_temp_dir(), 'windwalker'), 'wb+'); $headers = array('X-Foo' => array('Flower', 'Sakura'), 'Content-Type' => 'application/json'); $res = new Response($body, 404, $headers); $this->assertInstanceOf('Windwalker\\Http\\Stream\\Stream', $res->getBody()); $this->assertEquals($tmpfile, $res->getBody()->getMetadata('uri')); $this->assertEquals(array('Flower', 'Sakura'), $res->getHeader('x-foo')); $this->assertEquals(array('application/json'), $res->getHeader('content-type')); fclose($body); // Test with object params $body = new Stream(); $res = new Response($body); $this->assertSame($body, $res->getBody()); }
/** * Method to get a response object from a server response. * * @param string $content The complete server response, including headers * as a string if the response has no errors. * @param array $info The cURL request information. * * @return Response * * @since 2.0 * @throws \UnexpectedValueException */ protected function getResponse($content, $info) { // Create the response object. $return = new Response(); // Get the number of redirects that occurred. $redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0; /* * Split the response into headers and body. If cURL encountered redirects, the headers for the redirected requests will * also be included. So we split the response into header + body + the number of redirects and only use the last two * sections which should be the last set of headers and the actual body. */ $response = explode("\r\n\r\n", $content, 2 + $redirects); // Set the body for the response. $return->getBody()->write(array_pop($response)); $return->getBody()->rewind(); // Get the last set of response headers as an array. $headers = explode("\r\n", array_pop($response)); // Get the response code from the first offset of the response headers. preg_match('/[0-9]{3}/', array_shift($headers), $matches); $code = count($matches) ? $matches[0] : null; if (is_numeric($code)) { $return = $return->withStatus($code); } else { throw new \UnexpectedValueException('No HTTP response code found.'); } // Add the response headers to the response object. foreach ($headers as $header) { $pos = strpos($header, ':'); $return = $return->withHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1))); } return $return; }
/** * Method to get a response object from a server response. * * @param array $headers The response headers as an array. * @param string $body The response body as a string. * * @return Response * * @since 2.1 * @throws \UnexpectedValueException */ protected function getResponse(array $headers, $body) { // Create the response object. $return = new Response(); // Set the body for the response. $return->getBody()->write($body); $return->getBody()->rewind(); // Get the response code from the first offset of the response headers. preg_match('/[0-9]{3}/', array_shift($headers), $matches); $code = $matches[0]; if (is_numeric($code)) { $return = $return->withStatus($code); } else { throw new \UnexpectedValueException('No HTTP response code found.'); } // Add the response headers to the response object. foreach ($headers as $header) { $pos = strpos($header, ':'); $return = $return->withHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1))); } return $return; }