public function setUp() { $this->app = m::mock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $this->container = m::mock('Illuminate\\Container\\Container'); $this->router = m::mock('Dingo\\Api\\Routing\\Router'); $this->collection = m::mock('Dingo\\Api\\Routing\\ApiRouteCollection'); $this->container->shouldReceive('boot')->atLeast()->once(); $this->router->shouldReceive('requestTargettingApi')->andReturn(true); $this->middleware = new Authentication($this->app, $this->container); Dingo\Api\Http\Response::setTransformer(m::mock('Dingo\\Api\\Transformer\\Transformer')->shouldReceive('transformableResponse')->andReturn(false)->getMock()); }
public function testForbiddenResponseIsReturnedWhenRateLimitIsExceeded() { $request = Request::create('/', 'GET'); $this->auth->shouldReceive('check')->once()->andReturn(false); $this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => ['unauthenticated' => ['limit' => 1, 'reset' => 1]]])); $this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router); $this->container->shouldReceive('make')->once()->with('cache')->andReturn($this->cache); $this->router->shouldReceive('parseAcceptHeader')->once()->with($request)->andReturn(['v1', 'json']); $this->router->shouldReceive('requestTargettingApi')->once()->with($request)->andReturn(true); $ip = $request->getClientIp(); $this->cache->shouldReceive('add')->once()->with('dingo:api:requests:' . $ip, 0, 1); $this->cache->shouldReceive('add')->once(); $this->cache->shouldReceive('increment')->once()->with('dingo:api:requests:' . $ip); $this->cache->shouldReceive('get')->twice()->with('dingo:api:requests:' . $ip)->andReturn(2); $this->cache->shouldReceive('get')->once()->with('dingo:api:reset:' . $ip); Dingo\Api\Http\Response::setTransformer(m::mock('Dingo\\Api\\Transformer\\Transformer')->shouldReceive('transformableResponse')->andReturn(false)->getMock()); Dingo\Api\Http\Response::setFormatters(['json' => new Dingo\Api\Http\ResponseFormat\JsonResponseFormat()]); $response = $this->middleware->handle($request); $this->assertEquals(1, $response->headers->get('X-RateLimit-Limit')); $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining')); $this->assertEquals('{"message":"API rate limit has been exceeded."}', $response->getContent()); $this->assertEquals(403, $response->getStatusCode()); }