Beispiel #1
0
 /**
  * Create a response instance from the given value.
  * It also tries to apply the correct status code based on the method.
  *
  * @param  \Symfony\Component\HttpFoundation\Request  $request
  * @param  mixed  $response
  * @return \Illuminate\Http\Response
  */
 protected function prepareResponse($request, $response)
 {
     if (!$response instanceof SymfonyResponse) {
         $status_code = 200;
         if (array_key_exists($request->getMethod(), static::$verbs_status_codes)) {
             $status_code = static::$verbs_status_codes[$request->getMethod()];
         }
         $response = new Response($response, $status_code);
     }
     return $response->prepare($request);
 }
Beispiel #2
0
 /**
  * Create a response WITH pagination data from an existing response
  * 
  * @param  \Symfony\Component\HttpFoundation\Response $response
  * @param  mixed $count
  * @param  mixed $limit
  * @param  mixed $offset
  * @return \Peakfijn\GetSomeRest\Http\Response
  */
 public static function makeFromResponse(SymfonyResponse $response, $count = 0, $limit = 0, $offset = 0)
 {
     $response = Response::makeFromExisting($response);
     $response->setPagination(new static($count, $limit, $offset));
     return $response;
 }
Beispiel #3
0
 /**
  * Get a proper message from the response, or status code.
  * 
  * @param  Response $response
  * @return string
  */
 protected function getMessage(Response $response)
 {
     $message = null;
     if ($response->hasException()) {
         $message = $response->getException()->getMessage();
     }
     if (is_string($response->getOriginalContent())) {
         $message = $response->getOriginalContent();
     }
     if (empty($message)) {
         $message = strtolower($response->getStatusText());
     }
     return $message;
 }
 /**
  * When an exception is thrown, the Router will try and make a response from it.
  * It should also be attached to the response itself.
  * 
  * @return void
  */
 public function testCreatesInstanceFromExistingExceptionAlsoAddsExceptionToResponse()
 {
     $exception = new NotFoundResourceException('Test');
     $response = Response::makeFromException($exception);
     $this->assertTrue($response->hasException());
 }
 /**
  * Get the mutated content
  * 
  * @param  \Peakfijn\GetSomeRest\Http\Response $response
  * @param  \Illuminate\Http\Request $request
  * @return array
  */
 public function getContent(Response $response, Request $request)
 {
     return $this->toArray($response->getOriginalContent());
 }
Beispiel #6
0
 /**
  * Test if the http status code is used from the predefined response.
  * 
  * @return void
  */
 public function testResponsePreparalSetsStatusCodeOfGivenResponseObject()
 {
     $request = Request::create('/v1/foo', 'GET');
     $response = Response::create('test', 204);
     $prepared = $this->invokeMethod($this->router, 'prepareResponse', [$request, $response]);
     $this->assertEquals($response->getStatusCode(), $prepared->getStatusCode());
 }