public function test_it_builds_http_errors() { $request = new Request('POST', '/servers'); $response = new Response(400, [], stream_for('Invalid parameters')); $requestStr = trim($this->builder->str($request)); $responseStr = trim($this->builder->str($response)); $errorMessage = <<<EOT HTTP Error ~~~~~~~~~~ The remote server returned a "400 Bad Request" error for the following transaction: Request ~~~~~~~ {$requestStr} Response ~~~~~~~~ {$responseStr} Further information ~~~~~~~~~~~~~~~~~~~ Please ensure that your input values are valid and well-formed. Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues. EOT; $e = new BadResponseError($errorMessage); $e->setRequest($request); $e->setResponse($response); $this->assertEquals($e, $this->builder->httpError($request, $response)); }
/** * @expectedException \OpenStack\Common\Error\BadResponseError */ public function test_it_throws_exception_when_error() { $e = new BadResponseError(); $e->setRequest(new Request('HEAD', 'foo')); $e->setResponse(new Response(500)); $this->client->request('HEAD', 'foo', ['headers' => []])->shouldBeCalled()->willThrow($e); $this->assertFalse($this->service->containerExists('foo')); }
public function test_false_is_returned_for_non_existing_object() { $req = $this->setupMockRequest('HEAD', 'test/bar'); $res = new Response(404); $e = new BadResponseError(); $e->setRequest($req); $e->setResponse($res); $this->client->send(Argument::is($req))->willThrow($e); $this->assertFalse($this->container->objectExists('bar')); }
/** * Helper method responsible for constructing and returning {@see BadResponseError} exceptions. * * @param RequestInterface $request The faulty request * @param ResponseInterface $response The error-filled response * * @return BadResponseError */ public function httpError(RequestInterface $request, ResponseInterface $response) { $message = $this->header('HTTP Error'); $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", $response->getStatusCode(), $response->getReasonPhrase()); $message .= $this->header('Request'); $message .= trim($this->str($request)) . PHP_EOL . PHP_EOL; $message .= $this->header('Response'); $message .= trim($this->str($response)) . PHP_EOL . PHP_EOL; $message .= $this->header('Further information'); $message .= $this->getStatusCodeMessage($response->getStatusCode()); $message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; $e = new BadResponseError($message); $e->setRequest($request); $e->setResponse($response); return $e; }
/** * Helper method responsible for constructing and returning {@see BadResponseError} exceptions. * * @param RequestInterface $request The faulty request * @param ResponseInterface $response The error-filled response * * @return BadResponseError */ public function httpError(RequestInterface $request, ResponseInterface $response) { $message = $this->header('HTTP Error'); $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", $response->getStatusCode(), $response->getReasonPhrase()); $message .= $this->header('Request'); $message .= trim(str($request)) . PHP_EOL . PHP_EOL; $message .= $this->header('Response'); $message .= trim(str($response)) . PHP_EOL . PHP_EOL; $message .= $this->header('Further information'); // @codeCoverageIgnoreStart switch ($response->getStatusCode()) { case 400: $message .= "Please ensure that your input values are valid and well-formed. "; break; case 401: $message .= "Please ensure that your authentication credentials are valid. "; break; case 404: $message .= "Please ensure that the resource you're trying to access actually exists. "; break; case 500: $message .= "Please try this operation again once you know the remote server is operational. "; break; } // @codeCoverageIgnoreEnd $message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; $e = new BadResponseError($message); $e->setRequest($request); $e->setResponse($response); return $e; }
public function test_it_chunks_according_to_provided_segment_size() { /** @var \GuzzleHttp\Psr7\Stream $stream */ $stream = \GuzzleHttp\Psr7\stream_for(implode('', range('A', 'Z'))); $data = ['name' => 'object', 'stream' => $stream, 'segmentSize' => 10, 'segmentPrefix' => 'objectPrefix', 'segmentContainer' => 'segments']; // check container creation $e = new BadResponseError(); $e->setRequest(new Request('HEAD', 'segments')); $e->setResponse(new Response(404)); $this->client->request('HEAD', 'segments', ['headers' => []])->shouldBeCalled()->willThrow($e); $this->setupMock('PUT', 'segments', null, [], new Response(201)); $this->setupMock('PUT', 'segments/objectPrefix/1', $stream->read(10), [], new Response(201)); $this->setupMock('PUT', 'segments/objectPrefix/2', $stream->read(10), [], new Response(201)); $this->setupMock('PUT', 'segments/objectPrefix/3', $stream->read(10), [], new Response(201)); $this->setupMock('PUT', 'test/object', null, ['X-Object-Manifest' => 'segments/objectPrefix'], new Response(201)); $stream->rewind(); $this->container->createLargeObject($data); }
public function test_false_is_returned_for_non_existing_object() { $e = new BadResponseError(); $e->setRequest(new Request('HEAD', 'test/bar')); $e->setResponse(new Response(404)); $this->client->request('HEAD', 'test/bar', ['headers' => []])->shouldBeCalled()->willThrow($e); $this->assertFalse($this->container->objectExists('bar')); }
/** * @expectedException \OpenStack\Common\Error\BadResponseError */ public function test_other_exceptions_are_thrown() { $e = new BadResponseError(); $e->setRequest(new Request('HEAD', 'test/bar')); $e->setResponse(new Response(500)); $this->client->request('HEAD', 'test/bar', ['headers' => []])->shouldBeCalled()->willThrow($e); $this->container->objectExists('bar'); }