Inheritance: extends Illuminate\Http\Response
Example #1
0
 public function testNonCastableObjectsSetAsOriginalContent()
 {
     $object = new stdClass();
     $object->id = 'test';
     $response = new Response($object);
     $this->assertNull($response->getContent());
     $this->assertSame($object, $response->getOriginalContent());
 }
 public function testChangingResponseHeadersWithEvents()
 {
     $this->events->listen('Dingo\\Api\\Event\\ResponseIsMorphing', function ($event) {
         $event->response->headers->set('x-foo', 'bar');
     });
     Response::addFormatter('json', new Json());
     $response = new Response(['foo' => 'bar']);
     $this->assertEquals('bar', $response->morph('json')->headers->get('x-foo'));
     $this->events->forget('Dingo\\Api\\Event\\ResponseIsMorphing');
 }
Example #3
0
 /**
  * Override parent method
  *
  * @override
  *
  * @param null     $location
  * @param \Closure $data
  * @return Response
  *
  */
 public function created($location = null, \Closure $data = null)
 {
     list($item, $transformer, $headers) = $data();
     $class = get_class($item);
     $binding = $this->transformer->register($class, $transformer);
     $response = new Response($item, 201, $headers, $binding);
     if (!is_null($location)) {
         $response->header('Location', $location);
     }
     return $response;
 }
Example #4
0
 /**
  * Boot the service provider.
  *
  * @return void
  */
 public function boot()
 {
     $this->setupConfig();
     Http\Response::setFormatters($this->prepareConfigValues($this->app['config']['api.formats']));
     Http\Response::setTransformer($this->app['api.transformer']);
     Http\Response::setEventDispatcher($this->app['events']);
     $this->app->rebinding('api.routes', function ($app, $routes) {
         $app['api.url']->setRouteCollections($routes);
     });
 }
 public function setUp()
 {
     $this->auth = m::mock('Dingo\\Api\\Auth\\Shield');
     $this->router = new Router(new EventsDispatcher());
     $this->router->setDefaultVersion('v1');
     $this->router->setVendor('testing');
     $this->dispatcher = new Dispatcher(new Request(), new UrlGenerator(new RouteCollection(), new Request()), $this->router, $this->auth);
     Response::setFormatters(['json' => new JsonResponseFormat()]);
     Response::setTransformer(m::mock('Dingo\\Api\\Transformer\\Transformer')->shouldReceive('transformableResponse')->andReturn(false)->getMock()->shouldReceive('setRequest')->getMock());
 }
Example #6
0
 public function setUp()
 {
     $config = new Properties('v1', null, null, 'test');
     $this->container = new Container();
     $this->container['request'] = Request::create('/', 'GET');
     $url = new UrlGenerator(new RouteCollection(), $this->container['request']);
     $this->router = new Router(new EventDispatcher(), $config);
     $this->auth = new Authenticator($this->router, $this->container, []);
     $this->dispatcher = new Dispatcher($this->container, new Filesystem(), $url, $this->router, $this->auth, $config);
     Response::setFormatters(['json' => new JsonResponseFormat()]);
 }
 /**
  * Boot the service provider.
  *
  * @return void
  */
 public function boot()
 {
     $this->setupConfig();
     Http\Response::setFormatters($this->prepareConfigValues($this->app['config']['api.formats']));
     Http\Response::setTransformer($this->app['api.transformer']);
     Http\Response::setEventDispatcher($this->app['events']);
     $config = $this->app['config']['api'];
     Http\Request::setAcceptParser(new Http\Parser\Accept($config['standardsTree'], $config['subtype'], $config['version'], $config['defaultFormat']));
     $this->app->rebinding('api.routes', function ($app, $routes) {
         $app['api.url']->setRouteCollections($routes);
     });
 }
 public function setUp()
 {
     $this->container = new Container();
     $this->container['Illuminate\\Container\\Container'] = $this->container;
     $this->container['api.auth'] = new MiddlewareStub();
     $this->container['api.limiting'] = new MiddlewareStub();
     $this->container['request'] = new Http\Request();
     $this->adapter = $this->getAdapterInstance();
     $this->exception = m::mock('Dingo\\Api\\Exception\\Handler');
     $this->router = new Router($this->adapter, new Http\Parser\Accept('vnd', 'api', 'v1', 'json'), $this->exception, $this->container, null, null);
     Http\Response::setFormatters(['json' => new Http\Response\Format\Json()]);
 }
Example #9
0
 /**
  * Boot the service provider.
  *
  * @return void
  */
 public function boot()
 {
     $this->setupConfig();
     $this->app->singleton('Illuminate\\Contracts\\Debug\\ExceptionHandler', function ($app) {
         return $app['api.exception'];
     });
     Http\Response::setFormatters($this->prepareConfigValues($this->app['config']['api.formats']));
     Http\Response::setTransformer($this->app['api.transformer']);
     $this->app->rebinding('api.routes', function ($app, $routes) {
         $app['api.url']->setRouteCollections($routes);
     });
 }
 /**
  * Boot the response formats.
  *
  * @return array
  */
 protected function bootResponseFormats()
 {
     $formats = [];
     foreach ($this->app['config']['api::formats'] as $key => $format) {
         if (is_callable($format)) {
             $format = call_user_func($format, $this->app);
         }
         $formats[$key] = $format;
     }
     if (empty($formats)) {
         throw new RuntimeException('No registered response formats.');
     }
     ApiResponse::setFormatters($formats);
 }
 public function setUp()
 {
     $this->exceptionHandler = m::mock('Dingo\\Api\\ExceptionHandler');
     $this->router = new Router(new Dispatcher());
     $this->router->setExceptionHandler($this->exceptionHandler);
     $this->router->setDefaultVersion('v1');
     $this->router->setVendor('testing');
     $this->reviser = $this->router->getControllerReviser();
     Response::setFormatters(['json' => new JsonResponseFormat()]);
     $transformer = m::mock('Dingo\\Api\\Transformer\\Transformer');
     $transformer->shouldReceive('transformableResponse')->andReturn(false);
     $transformer->shouldReceive('setRequest');
     Response::setTransformer($transformer);
 }
 public function setUp()
 {
     $this->container = new Container();
     $this->container['request'] = Request::create('/', 'GET');
     $this->container['api.auth'] = new MiddlewareStub();
     $this->container['api.limiting'] = new MiddlewareStub();
     $this->transformerFactory = new TransformerFactory($this->container, new TransformerStub());
     $this->adapter = new RoutingAdapterStub();
     $this->exception = m::mock('Dingo\\Api\\Exception\\Handler');
     $this->router = new Router($this->adapter, new Http\Parser\Accept('vnd', 'api', 'v1', 'json'), $this->exception, $this->container, null, null);
     $this->auth = new Auth($this->router, $this->container, []);
     $this->dispatcher = new Dispatcher($this->container, new Filesystem(), $this->router, $this->auth);
     $this->dispatcher->setSubtype('api');
     $this->dispatcher->setStandardsTree('vnd');
     $this->dispatcher->setDefaultVersion('v1');
     $this->dispatcher->setDefaultFormat('json');
     Http\Response::setFormatters(['json' => new Http\Response\Format\Json()]);
     Http\Response::setTransformer($this->transformerFactory);
 }
Example #13
0
 /**
  * Handle an exception thrown during dispatching of an API request.
  *
  * @param \Exception $exception
  *
  * @throws \Exception
  *
  * @return \Dingo\Api\Http\Response
  */
 public function handle(Exception $exception)
 {
     if ($this->handler->willHandle($exception)) {
         $response = $this->handler->handle($exception);
         return Response::makeFromExisting($response);
     } elseif (!$exception instanceof HttpExceptionInterface) {
         throw $exception;
     }
     if (!($message = $exception->getMessage())) {
         $message = sprintf('%d %s', $exception->getStatusCode(), Response::$statusTexts[$exception->getStatusCode()]);
     }
     $response = ['message' => $message, 'status_code' => $exception->getStatusCode()];
     if ($exception instanceof ResourceException && $exception->hasErrors()) {
         $response['errors'] = $exception->getErrors();
     }
     if ($code = $exception->getCode()) {
         $response['code'] = $code;
     }
     if ($this->debug) {
         $response['debug'] = ['line' => $exception->getLine(), 'file' => $exception->getFile(), 'class' => get_class($exception), 'trace' => explode("\n", $exception->getTraceAsString())];
     }
     return new Response($response, $exception->getStatusCode(), $exception->getHeaders());
 }
 public function testRouterHandlesCustomEtag()
 {
     $this->router->api(['version' => 'v1'], function () {
         $this->router->get('foo', function () {
             $response = new Response('bar');
             $response->setEtag('custom-etag');
             return $response;
         });
     });
     $request = Request::create('foo', 'GET');
     $request->headers->set('If-None-Match', '"custom-etag"', true);
     $request->headers->set('accept', 'application/vnd.testing.v1+json');
     $this->router->setConditionalRequest(true);
     $response = $this->router->dispatch($request);
     $this->assertEquals(304, $response->getStatusCode());
     $this->assertEquals('"custom-etag"', $response->getETag());
     $this->assertEquals(null, $response->getContent());
 }
Example #15
0
 /**
  * Respond with a no content response.
  *
  * @return \Dingo\Api\Http\Response
  */
 public function noContent()
 {
     $response = new Response(null);
     return $response->setStatusCode(204);
 }
Example #16
0
 public function tearDown()
 {
     Mockery::close();
     Response::setFormatters([]);
 }
Example #17
0
 public function testRouterHandlesCustomEtag()
 {
     $this->router->version('v1', ['conditional_request' => true], function () {
         $this->router->get('foo', function () {
             $response = new Http\Response('bar');
             $response->setEtag('custom-etag');
             return $response;
         });
     });
     $response = $this->router->dispatch($this->createRequest('foo', 'GET', ['if-none-match' => '"custom-etag"', 'accept' => 'application/vnd.api.v1+json']));
     $this->assertEquals(304, $response->getStatusCode());
     $this->assertEquals('"custom-etag"', $response->getETag());
     $this->assertEquals(null, $response->getContent());
 }
Example #18
0
 /**
  * {@inheritDoc}
  */
 protected function prepareResponse($request, $response)
 {
     $response = parent::prepareResponse($request, $response);
     if ($this->requestTargettingApi($request)) {
         if ($response instanceof IlluminateResponse) {
             $response = ApiResponse::makeFromExisting($response);
         }
         if ($response->isSuccessful() && $this->getConditionalRequest()) {
             if (!$response->headers->has('ETag')) {
                 $response->setEtag(md5($response->getContent()));
             }
             $response->isNotModified($request);
         }
     }
     return $response;
 }
Example #19
0
 /**
  * Build the response.
  *
  * @return \Illuminate\Http\Response
  */
 public function build()
 {
     $response = new Response($this->response, $this->statusCode, $this->headers);
     foreach ($this->cookies as $cookie) {
         if ($cookie instanceof Cookie) {
             $response->withCookie($cookie);
         }
     }
     return $response;
 }
Example #20
0
File: Router.php Project: jrean/api
 /**
  * Prepare a response by transforming and formatting it correctly.
  *
  * @param mixed                   $response
  * @param \Dingo\Api\Http\Request $request
  * @param string                  $format
  * @param bool                    $raw
  *
  * @return \Dingo\Api\Http\Response
  */
 protected function prepareResponse($response, Request $request, $format)
 {
     if ($response instanceof IlluminateResponse) {
         $response = Response::makeFromExisting($response);
     }
     if ($response instanceof Response) {
         // If we try and get a formatter that does not exist we'll let the exception
         // handler deal with it. At worst we'll get a generic JSON response that
         // a consumer can hopefully deal with. Ideally they won't be using
         // an unsupported format.
         try {
             $response->getFormatter($format)->setResponse($response)->setRequest($request);
         } catch (NotAcceptableHttpException $exception) {
             return $this->exception->handle($exception);
         }
         $response = $response->morph($format);
     }
     if ($response->isSuccessful() && $this->requestIsConditional()) {
         if (!$response->headers->has('ETag')) {
             $response->setEtag(md5($response->getContent()));
         }
         $response->isNotModified($request);
     }
     return $response;
 }
 /**
  * Authenticate the request for the given route.
  *
  * @param  \Symfony\Component\HttpFoundation\Request  $request
  * @param  \Illuminate\Routing\Route  $route
  * @return null|\Dingo\Api\Http\Response
  * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
  */
 protected function authenticate(Request $request, Route $route)
 {
     try {
         $this->auth->authenticate($request, $route);
     } catch (UnauthorizedHttpException $exception) {
         $response = $this->router->handleException($exception);
         list($version, $format) = $this->router->parseAcceptHeader($request);
         return Response::makeFromExisting($response)->morph($format);
     }
 }
 /**
  * @expectedException \RuntimeException
  */
 public function testGettingUnregisteredFormatterThrowsException()
 {
     Response::getFormatter('test');
 }
Example #23
0
 protected function setResponseStaticInstances()
 {
     Response::setFormatters($this->config('formats'));
     Response::setTransformer($this->app['api.transformer']);
     Response::setEventDispatcher($this->app['events']);
 }
Example #24
0
 /**
  * Boot the service provider.
  *
  * @return void
  */
 public function boot()
 {
     $this->setupContainerBindings();
     Response::setFormatters($this->prepareConfigInstances($this->app['config']['api::formats']));
     Response::setTransformer($this->app['api.transformer']);
 }
Example #25
0
 /**
  * @param $data
  * @param int $statusCode
  *
  * @return mixed
  */
 protected function respond($data, $statusCode = 200)
 {
     $response = new Response($data);
     return $response->setStatusCode($statusCode);
 }
 public function tearDown()
 {
     Mockery::close();
     EloquentModelStub::$snakeAttributes = true;
     Response::setFormatters([]);
 }