-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from krakphp/5-mountable-middleware
Mountable Middleware #5
- Loading branch information
Showing
8 changed files
with
188 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
// php -S localhost:8000 example/auth.php | ||
use Krak\Mw\Http; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$app = new Http\App(); | ||
$app->with(Http\Package\std()); | ||
|
||
$app->get('/', function() { | ||
return 'Home'; | ||
}); | ||
|
||
$api = new Http\App(); | ||
$api->with(Http\Package\std()); | ||
$api->with(Http\Package\rest()); | ||
$api->get('/users', function() { | ||
return [ | ||
['id' => 1], | ||
['id' => 2] | ||
]; | ||
}); | ||
$api->mount('/users', function($req, $next) use ($api) { | ||
// basic auth with user `foo` and password `bar` | ||
$rf = http\jsonResponseFactory($api['response_factory']); | ||
|
||
if (!$req->hasHeader('Authorization')) { | ||
return $rf(401, ['WWW-Authenticate' => 'Basic realm="/"'], ['code' => 'unauthorized']); | ||
} | ||
if ($req->getHeader('Authorization')[0] != 'Basic Zm9vOmJhcg==') { | ||
return $rf(403, [], ['code' => 'forbidden']); | ||
} | ||
|
||
return $next($req); | ||
}); | ||
|
||
$app->mount('/api', $api); | ||
|
||
$app->serve(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Krak\Mw\Http, | ||
GuzzleHttp\Psr7\ServerRequest; | ||
|
||
describe('->mount', function() { | ||
it('mounts a middleware onto the app', function() { | ||
$app = new Http\App(); | ||
$app->with(Http\Package\std()); | ||
$app->mount('/a', function() { | ||
return 1; | ||
}); | ||
$app->get('/a/1', function() { assert(false); }); | ||
$req = new ServerRequest('GET', '/a/2'); | ||
$res = $app($req, function() {}); | ||
assert($app($req, function() {}) == 1); | ||
}); | ||
it('allows for recursive mounts', function() { | ||
$app1 = new Http\App(); | ||
$app1->with(Http\Package\std()); | ||
$app2 = new Http\App(); | ||
$app2->with(Http\Package\std()); | ||
$app3 = new Http\App(); | ||
$app3->with(Http\Package\std()); | ||
|
||
$app3->mount('/c', function() { | ||
return 4; | ||
}); | ||
$app1->mount('/a', $app2); | ||
$app2->mount('/b', $app3); | ||
|
||
$app3->get('/c/1', function() { assert(false); }); | ||
$req = new ServerRequest('GET', '/a/b/c/d'); | ||
assert($app1($req, function() {}) == 4); | ||
}); | ||
it('allows for recursive mounted routes', function() { | ||
$app1 = new Http\App(); | ||
$app1->with(Http\Package\std()); | ||
$app2 = new Http\App(); | ||
$app2->with(Http\Package\std()); | ||
$app3 = new Http\App(); | ||
$app3->with(Http\Package\std()); | ||
|
||
$app2->mount('/b', $app3); | ||
$app1->mount('/a', $app2); | ||
|
||
$app3->get('/c/d', function() { return 2; }); | ||
// allow the return of 2 instead of converting into a response | ||
$app3['stacks.marshal_response']->push(function($res) { return $res; }); | ||
|
||
$req = new ServerRequest('GET', '/a/b/c/d'); | ||
assert($app1($req, function() {}) == 2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters