Skip to content

Commit bae4b22

Browse files
authored
Test API request (#1667)
1 parent 48266da commit bae4b22

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tests/DebugbarBrowserTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function getEnvironmentSetUp($app)
2828

2929
$this->addWebRoutes($router);
3030
$this->addApiRoutes($router);
31+
$this->addViewPaths();
3132

3233
$kernel = app(\Illuminate\Contracts\Http\Kernel::class);
3334
$kernel->pushMiddleware(\Illuminate\Session\Middleware\StartSession::class);
@@ -58,6 +59,12 @@ protected function addWebRoutes(Router $router)
5859
return '<html><head></head><body>HTMLPONG</body></html>';
5960
}
6061
]);
62+
63+
$router->get('web/ajax', [
64+
'uses' => function () {
65+
return view('ajax');
66+
}
67+
]);
6168
}
6269

6370
/**
@@ -72,6 +79,11 @@ protected function addApiRoutes(Router $router)
7279
]);
7380
}
7481

82+
protected function addViewPaths()
83+
{
84+
config(['view.paths' => array_merge(config('view.paths'), [__DIR__ . '/resources/views'])]);
85+
}
86+
7587
public function testItStacksOnRedirect()
7688
{
7789
$this->browse(function (Browser $browser) {
@@ -114,4 +126,16 @@ public function testItDoesntInjectOnJson()
114126
->assertDontSee('GET api/ping');
115127
});
116128
}
129+
130+
public function testItCapturesAjaxRequests()
131+
{
132+
$this->browse(function (Browser $browser) {
133+
$browser->visit('web/ajax')
134+
->waitFor('.phpdebugbar')
135+
->assertSee('GET web/ajax')
136+
->click('#ajax-link')
137+
->waitForTextIn('#result', 'pong')
138+
->assertSee('GET api/ping');
139+
});
140+
}
117141
}

tests/resources/views/ajax.blade.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<body>
3+
4+
<a href="#" id="ajax-link" onclick="loadAjax();return false;">Click me</a>
5+
<div id="result">Waiting..</div>
6+
7+
<script>
8+
async function loadAjax() {
9+
try {
10+
const response = await fetch('/api/ping', {headers: {'X-Requested-With': 'XMLHttpRequest'}});
11+
if (!response.ok) {
12+
throw new Error(`Response status: ${response.status}`);
13+
}
14+
15+
const json = await response.json();
16+
17+
document.getElementById('result').innerText = json.status;
18+
} catch (error) {
19+
console.error(error.message);
20+
}
21+
}
22+
23+
</script>
24+
</body>

0 commit comments

Comments
 (0)