Skip to content

Commit

Permalink
Test API request (#1667)
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored Sep 11, 2024
1 parent 48266da commit bae4b22
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/DebugbarBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function getEnvironmentSetUp($app)

$this->addWebRoutes($router);
$this->addApiRoutes($router);
$this->addViewPaths();

$kernel = app(\Illuminate\Contracts\Http\Kernel::class);
$kernel->pushMiddleware(\Illuminate\Session\Middleware\StartSession::class);
Expand Down Expand Up @@ -58,6 +59,12 @@ protected function addWebRoutes(Router $router)
return '<html><head></head><body>HTMLPONG</body></html>';
}
]);

$router->get('web/ajax', [
'uses' => function () {
return view('ajax');
}
]);
}

/**
Expand All @@ -72,6 +79,11 @@ protected function addApiRoutes(Router $router)
]);
}

protected function addViewPaths()
{
config(['view.paths' => array_merge(config('view.paths'), [__DIR__ . '/resources/views'])]);
}

public function testItStacksOnRedirect()
{
$this->browse(function (Browser $browser) {
Expand Down Expand Up @@ -114,4 +126,16 @@ public function testItDoesntInjectOnJson()
->assertDontSee('GET api/ping');
});
}

public function testItCapturesAjaxRequests()
{
$this->browse(function (Browser $browser) {
$browser->visit('web/ajax')
->waitFor('.phpdebugbar')
->assertSee('GET web/ajax')
->click('#ajax-link')
->waitForTextIn('#result', 'pong')
->assertSee('GET api/ping');
});
}
}
24 changes: 24 additions & 0 deletions tests/resources/views/ajax.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<body>

<a href="#" id="ajax-link" onclick="loadAjax();return false;">Click me</a>
<div id="result">Waiting..</div>

<script>
async function loadAjax() {
try {
const response = await fetch('/api/ping', {headers: {'X-Requested-With': 'XMLHttpRequest'}});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const json = await response.json();
document.getElementById('result').innerText = json.status;
} catch (error) {
console.error(error.message);
}
}
</script>
</body>

0 comments on commit bae4b22

Please sign in to comment.