Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 138be6e

Browse files
authored
[2.x] Add event listener (#450)
* Test with append/prepend middleware * Add eventlistener
1 parent b50ffb7 commit 138be6e

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

src/HandleCors.php

+27-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Asm89\Stack\CorsService;
7+
use Illuminate\Foundation\Http\Events\RequestHandled;
78
use Illuminate\Http\Request;
89
use Illuminate\Contracts\Container\Container;
910
use Symfony\Component\HttpFoundation\Response;
@@ -15,7 +16,7 @@ class HandleCors
1516

1617
/** @var \Illuminate\Contracts\Container\Container $container */
1718
protected $container;
18-
19+
1920
public function __construct(CorsService $cors, Container $container)
2021
{
2122
$this->cors = $cors;
@@ -45,16 +46,38 @@ public function handle($request, Closure $next)
4546
return $response;
4647
}
4748

49+
// Add the headers on the Request Handled event as fallback in case of exceptions
50+
if (class_exists(RequestHandled::class) && $this->container->bound('events')) {
51+
$this->container->make('events')->listen(RequestHandled::class, function (RequestHandled $event) {
52+
$this->addHeaders($event->request, $event->response);
53+
});
54+
}
55+
4856
// Handle the request
4957
$response = $next($request);
5058

51-
// For OPTIONS (but not Preflight) vary the Request-Method header
5259
if ($request->getMethod() === 'OPTIONS') {
5360
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
5461
}
5562

56-
// Add the CORS headers to the Response
57-
return $this->cors->addActualRequestHeaders($response, $request);
63+
return $this->addHeaders($request, $response);
64+
}
65+
66+
/**
67+
* Add the headers to the Response, if they don't exist yet.
68+
*
69+
* @param Request $request
70+
* @param Response $response
71+
* @return Response
72+
*/
73+
protected function addHeaders(Request $request, Response $response): Response
74+
{
75+
if (! $response->headers->has('Access-Control-Allow-Origin')) {
76+
// Add the CORS headers to the Response
77+
$response = $this->cors->addActualRequestHeaders($response, $request);
78+
}
79+
80+
return $response;
5881
}
5982

6083
/**

tests/BrowserTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,34 @@ public function testFetchWildcard()
141141
$this->assertFalse(File::exists(__DIR__ .'/Browser/invalid.flag'));
142142
}
143143

144+
public function testPushMiddleware()
145+
{
146+
$this->tweakApplication(function ($app) {
147+
// Add the middleware
148+
/** @var Kernel $kernel */
149+
$kernel = $app->make(Kernel::class);
150+
$kernel->pushMiddleware(new class {
151+
public function handle($request, \Closure $next)
152+
{
153+
if ($request->is('protected')) {
154+
return response()->json(['message' => 'Authorization Required'], 401);
155+
}
156+
return $next($request);
157+
}
158+
});
159+
});
160+
161+
File::delete(__DIR__ .'/Browser/invalid.flag');
162+
163+
$this->browse(function ($browser) {
164+
$browser->visit('js/middleware.html')
165+
->waitForText('passes: 1')
166+
->assertSee('passes: 1');
167+
});
168+
169+
$this->assertFalse(File::exists(__DIR__ .'/Browser/invalid.flag'));
170+
}
171+
144172
public function testFetchInvalid()
145173
{
146174
$this->tweakApplication(function ($app) {

tests/js/middleware.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Mocha Tests</title>
5+
<link rel="stylesheet" href="mocha.css" />
6+
</head>
7+
<body>
8+
<div id="mocha"></div>
9+
<script src="expect.js"></script>
10+
<script src="mocha.js"></script>
11+
<script>mocha.setup('bdd')</script>
12+
<script src="test.middleware.js"></script>
13+
<script>
14+
mocha.checkLeaks();
15+
mocha.run();
16+
</script>
17+
</body>
18+
</html>

tests/js/test.middleware.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(function() {
2+
var CORS_SERVER;
3+
4+
CORS_SERVER = 'localhost:9292';
5+
6+
describe('CORS-INVALID', function() {
7+
return it('should allow access to invalid auth resource', function(done) {
8+
return fetch(`http://${CORS_SERVER}/protected`, {
9+
method: 'GET',
10+
mode: 'cors'
11+
}).then((response) => {
12+
expect(response.status).to.eql(401);
13+
return done();
14+
})
15+
});
16+
});
17+
18+
}).call(this);

0 commit comments

Comments
 (0)