-
Notifications
You must be signed in to change notification settings - Fork 3
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 #18 from edersoares/proxy
Proxy
- Loading branch information
Showing
6 changed files
with
190 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
use Dex\Laravel\Frontier\Frontier; | ||
|
||
Frontier::addFromConfig('frontier.frontier'); | ||
Frontier::addFromConfig('frontier.proxy'); |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dex\Laravel\Frontier; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class FrontendProxyController | ||
{ | ||
public function __construct(protected Request $request) | ||
{ | ||
} | ||
|
||
public function __invoke($uri, $config): Response | ||
{ | ||
$accept = $this->request->header('accept', '*/*'); | ||
$url = trim($config['url'], '/') . '/' . trim($uri, '/'); | ||
|
||
$response = Http::withHeaders([ | ||
'Accept' => $accept, | ||
])->get($url); | ||
|
||
$content = $response->body(); | ||
$contextType = $response->header('content-type'); | ||
|
||
if ($config['replaces']) { | ||
$content = str_replace( | ||
array_keys($config['replaces']), | ||
array_values($config['replaces']), | ||
$content | ||
); | ||
} | ||
|
||
return new Response($content, headers: [ | ||
'content-type' => $contextType, | ||
]); | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dex\Laravel\Frontier\Tests; | ||
|
||
use Dex\Laravel\Frontier\Frontier; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
beforeEach(fn () => Frontier::add([ | ||
'type' => 'proxy', | ||
'host' => 'frontier.test', | ||
'rules' => [ | ||
'/favicon.ico:exact', | ||
'/exact/replace:exact:replace(/exact/replace)', | ||
'/all:replace(Replace,is amazing!)', | ||
'/web', | ||
], | ||
])); | ||
|
||
test('proxy exact route', function () { | ||
Http::fake([ | ||
'frontier.test/favicon.ico' => Http::response('Frontier Favicon'), | ||
]); | ||
|
||
$this->get('favicon.ico') | ||
->assertContent('Frontier Favicon') | ||
->assertOk(); | ||
|
||
$this->get('favicon.icon') | ||
->assertNotFound(); | ||
|
||
$this->get('favicon.ico/ico') | ||
->assertNotFound(); | ||
}); | ||
|
||
test('proxy all routes', function () { | ||
Http::fake([ | ||
'frontier.test/web/*' => Http::response('Frontier Favicon'), | ||
]); | ||
|
||
$this->get('/web') | ||
->assertOk(); | ||
|
||
$this->get('/web/one') | ||
->assertOk(); | ||
|
||
$this->get('web/two') | ||
->assertOk(); | ||
}); | ||
|
||
test('proxy exact and replace', function () { | ||
Http::fake([ | ||
'frontier.test/exact/replace' => Http::response('Running: /exact/replace'), | ||
]); | ||
|
||
$this->get('/exact/replace') | ||
->assertContent('Running: frontier.test/exact/replace') | ||
->assertOk(); | ||
|
||
$this->get('exact/replace/more') | ||
->assertNotFound(); | ||
}); | ||
|
||
test('proxy all routes and replace', function () { | ||
Http::fake([ | ||
'frontier.test/all/*' => Http::response('Frontier Replace'), | ||
]); | ||
|
||
$this->get('/all') | ||
->assertContent('Frontier is amazing!') | ||
->assertOk(); | ||
|
||
$this->get('/all/one') | ||
->assertContent('Frontier is amazing!') | ||
->assertOk(); | ||
|
||
$this->get('all/two') | ||
->assertContent('Frontier is amazing!') | ||
->assertOk(); | ||
}); |