Skip to content

Commit

Permalink
Merge pull request #12 from julienbourdeau/feat/ignore-options
Browse files Browse the repository at this point in the history
Ignore OPTIONS http calls
  • Loading branch information
julienbourdeau committed Oct 28, 2019
2 parents 8b9faf9 + 258a673 commit 0fc2f6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/Listeners/LogRouteUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public function handle($event)

protected function shouldLogUsage($event)
{
$status_code = $event->response->getStatusCode();
if ($event->request->isMethod('options')) {
return false;
}

$status_code = $event->response->getStatusCode();
if ($status_code >= 400 || $status_code < 200) {
return;
return false;
}

$route = $event->request->route();
Expand Down
29 changes: 20 additions & 9 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class IntegrationTest extends BaseIntegrationTestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

Route::get('/', function () {
return 'It works!';
});
}

/** @test */
public function itCanCreateRouteUsageEntry()
{
Expand All @@ -31,9 +40,6 @@ public function itCanCreateRouteUsageEntry()
/** @test */
public function itSavesRouteUsageEntryIfResponseIsValid()
{
Route::get('/', function () {
return 'It works!';
});

$response = $this->get('/');
$response->assertStatus(200);
Expand All @@ -49,25 +55,31 @@ public function itSavesRouteUsageEntryIfResponseIsValid()
public function itUpdatesUpdatedatAttribute()
{
RouteUsage::create([
'identifier' => ($id = sha1('GET'.'/'.'[Closure]'.'200')),
'identifier' => ($id = sha1('GET' . '/' . '[Closure]' . '200')),
'method' => 'GET',
'path' => '/',
'status_code' => 200,
'updated_at' => ($now = now()->subYear(1)),
'created_at' => $created_at = $now->format('Y-m-d H:i:s'),
]);

Route::get('/', function () {
return 'It works!';
});

$response = $this->get('/');
$response->assertStatus(200);

$route = RouteUsage::where('identifier', $id)->first();
$this->assertGreaterThan(time() - 120, $route->updated_at->getTimestamp());
$this->assertEquals($created_at, $route->created_at);
}

/** @test */
public function itIgnoresOptionsHttpRequest()
{
$response = $this->call('OPTIONS', '/');
$response->assertStatus(200);

$this->assertEquals(0, RouteUsage::count());
}

/** @test */
public function itIgnoresRoutesBasedOnConfig()
{
Expand All @@ -77,7 +89,6 @@ public function itIgnoresRoutesBasedOnConfig()
'uri' => '/ignore/'
]
]]);
Route::get('/', function () { return 'home'; });
Route::get('/ok', function () { return 'OK'; })->name('nope.index');
Route::get('/ignore', function () { return 'KO'; });

Expand Down

0 comments on commit 0fc2f6d

Please sign in to comment.