Example #1
0
 public function onLoad()
 {
     parent::onLoad();
     // get request ip
     $remoteIp = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
     // load app and user
     $this->app = $this->getApp($this->appId);
     $this->user = $this->getUser($this->userId);
     // check rate limit
     if ($this->rateService->hasExceeded($remoteIp, $this->context->get('fusio.routeId'), $this->app)) {
         throw new StatusCode\ClientErrorException('Rate limit exceeded', 429);
     }
     // log request
     $this->logId = $this->apiLogger->log($this->context->get('fusio.routeId'), $this->appId, $this->userId, $remoteIp, $this->request);
 }
Example #2
0
 public function testLog()
 {
     $body = $this->getMockBuilder('Psr\\Http\\Message\\StreamInterface')->getMock();
     $request = $this->getMockBuilder('PSX\\Http\\RequestInterface')->getMock();
     $request->expects($this->once())->method('getHeaders')->will($this->returnValue(['Content-Type' => ['application/json']]));
     $request->expects($this->once())->method('getBody')->will($this->returnValue($body));
     $connection = $this->getMockBuilder('Doctrine\\DBAL\\Connection')->disableOriginalConstructor()->getMock();
     $connection->expects($this->once())->method('insert')->with($this->equalTo('fusio_log'), $this->callback(function ($args) {
         $args['date'] = substr($args['date'], 0, 16);
         $expect = ['routeId' => 1, 'appId' => 1, 'userId' => 1, 'ip' => '127.0.0.1', 'userAgent' => null, 'method' => null, 'path' => null, 'header' => 'Content-Type: application/json', 'body' => null, 'date' => date('Y-m-d H:i')];
         $this->assertEquals($expect, $args);
         return true;
     }));
     $connection->expects($this->once())->method('lastInsertId')->will($this->returnValue(1));
     $logger = new Logger($connection);
     $logId = $logger->log(1, 1, 1, '127.0.0.1', $request);
     $this->assertEquals(1, $logId);
 }