示例#1
0
 protected function createRequest($uri, $method, array $headers = [])
 {
     $request = Request::create($uri, $method);
     foreach ($headers as $key => $value) {
         $request->headers->set($key, $value);
     }
     return $request;
 }
示例#2
0
 public function testControllerOptionsMergeAndOverrideRouteOptions()
 {
     $request = Request::create('foo', 'GET');
     $route = new Route($this->adapter, $this->container, $request, ['uri' => 'foo', 'methods' => ['GET', 'HEAD'], 'action' => ['scopes' => ['foo', 'bar'], 'providers' => ['foo'], 'limit' => 5, 'expires' => 10, 'throttle' => 'Foo', 'version' => ['v1'], 'conditionalRequest' => false, 'uses' => 'Dingo\\Api\\Tests\\Stubs\\RoutingControllerStub@index']]);
     $this->assertEquals(['foo', 'bar', 'baz', 'bing'], $route->scopes(), 'Route did not setup scopes correctly.');
     $this->assertEquals(['foo', 'red', 'black'], $route->getAuthProviders(), 'Route did not setup authentication providers correctly.');
     $this->assertEquals(10, $route->getRateLimit(), 'Route did not setup rate limit correctly.');
     $this->assertEquals(20, $route->getRateExpiration(), 'Route did not setup rate limit expiration correctly.');
     $this->assertTrue($route->hasThrottle(), 'Route did not setup throttle correctly.');
     $this->assertInstanceOf('Dingo\\Api\\Tests\\Stubs\\BasicThrottleStub', $route->getThrottle(), 'Route did not setup throttle correctly.');
 }
示例#3
0
 public function testProvidersAreFilteredWhenSpecificProviderIsRequested()
 {
     $this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route = m::mock('Dingo\\Api\\Routing\\Route'));
     $this->router->shouldReceive('getCurrentRequest')->once()->andReturn($request = Request::create('foo', 'GET'));
     $provider = m::mock('Dingo\\Api\\Auth\\Provider\\Provider');
     $provider->shouldReceive('authenticate')->once()->with($request, $route)->andReturn((object) ['id' => 1]);
     $this->auth->extend('one', m::mock('Dingo\\Api\\Auth\\Provider\\Provider'));
     $this->auth->extend('two', $provider);
     $this->auth->authenticate(['two']);
     $this->assertEquals($provider, $this->auth->getProviderUsed());
 }
示例#4
0
 /**
  * @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
  */
 public function testAuthenticationFailsAndExceptionIsThrown()
 {
     $exception = new UnauthorizedHttpException('test');
     $request = Request::create('test', 'GET');
     $route = new Route($this->adapter, $this->container, $request, ['uri' => '/test', 'action' => ['providers' => []]]);
     $this->auth->shouldReceive('check')->once()->with(false)->andReturn(false);
     $this->auth->shouldReceive('authenticate')->once()->with([])->andThrow($exception);
     $this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
     $this->middleware->handle($request, function () {
         //
     });
 }
 public function testAdapterDispatchesRequestsThroughRouter()
 {
     $this->container['request'] = Http\Request::create('/foo', 'GET');
     $this->router->version('v1', function () {
         $this->router->get('foo', function () {
             return 'foo';
         });
     });
     $response = $this->router->dispatch($this->container['request']);
     $this->assertEquals('foo', $response->getContent());
 }
示例#6
0
 public function testExceptionsHandledByRenderAreReroutedThroughHandler()
 {
     $request = ApiRequest::create('foo', 'GET');
     $this->log->shouldReceive('error')->once()->with($exception = new HttpException(404));
     $response = $this->exceptionHandler->render($request, $exception);
     $this->assertEquals('{"message":"404 Not Found","status_code":404}', $response->getContent());
 }
示例#7
0
 public function testGettingTheRemainingLimit()
 {
     $this->limiter->extend(new ThrottleStub(['limit' => 10, 'expires' => 200]));
     $this->limiter->rateLimitRequest(Request::create('test', 'GET'));
     $this->assertEquals(9, $this->limiter->getRemainingLimit());
 }
 public function testRateLimitingWithRouteThrottle()
 {
     $request = Request::create('test', 'GET');
     $route = m::mock('Dingo\\Api\\Routing\\Route');
     $route->shouldReceive('hasThrottle')->once()->andReturn(true);
     $route->shouldReceive('getThrottle')->once()->andReturn(new ThrottleStub(['limit' => 10, 'expires' => 20]));
     $route->shouldReceive('getRateLimit')->once()->andReturn(0);
     $route->shouldReceive('getRateExpiration')->once()->andReturn(0);
     $this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
     $response = $this->middleware->handle($request, function ($request) {
         return new Response('foo');
     });
     $this->assertArrayHasKey('x-ratelimit-limit', $response->headers->all());
     $this->assertArrayHasKey('x-ratelimit-remaining', $response->headers->all());
     $this->assertArrayHasKey('x-ratelimit-reset', $response->headers->all());
     $this->assertEquals(9, $response->headers->get('x-ratelimit-remaining'));
     $this->assertEquals(10, $response->headers->get('x-ratelimit-limit'));
 }