/** * Method to test getStatusCode(). * * @return void * * @covers Windwalker\Http\Response::getStatusCode * @covers Windwalker\Http\Response::withStatus */ public function testWithAndGetStatusCode() { $this->assertEquals(200, $this->instance->getStatusCode()); $res = $this->instance->withStatus(403); $this->assertNotSame($res, $this->instance); $this->assertEquals(403, $res->getStatusCode()); $res = $res->withStatus(500, 'Unknown error'); $this->assertEquals(500, $res->getStatusCode()); $this->assertEquals('Unknown error', $res->getReasonPhrase()); }
/** * 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; }