1+ <?php
2+
3+ namespace Tests \Feature \Http \Middleware ;
4+
5+ use Tests \TestCase ;
6+ use ProcessMaker \Http \Middleware \TrustHosts ;
7+ use Illuminate \Http \Request ;
8+ use Illuminate \Support \Facades \Config ;
9+
10+ class TrustHostsTest extends TestCase
11+ {
12+ protected $ middleware ;
13+
14+ protected function setUp (): void
15+ {
16+ parent ::setUp ();
17+ $ this ->middleware = new TrustHosts ($ this ->app );
18+ }
19+
20+ public function test_valid_trusted_host ()
21+ {
22+ // Set app URL for testing
23+ Config::set ('app.url ' , 'https://example.processmaker.net ' );
24+
25+ $ request = Request::create ('https://subdomain.example.processmaker.net ' );
26+ $ request ->headers ->set ('X-Forwarded-Host ' , 'subdomain.example.processmaker.net ' );
27+
28+ $ response = $ this ->middleware ->handle ($ request , function ($ req ) {
29+ return response ()->json (['status ' => 'success ' ]);
30+ });
31+
32+ $ this ->assertEquals (200 , $ response ->status ());
33+ }
34+
35+ public function test_invalid_trusted_host ()
36+ {
37+ // Set app URL for testing
38+ Config::set ('app.url ' , 'https://example.processmaker.net ' );
39+
40+ $ request = Request::create ('https://malicious-site.com ' );
41+ $ request ->headers ->set ('X-Forwarded-Host ' , 'malicious-site.com ' );
42+
43+ $ this ->expectException (\Symfony \Component \HttpKernel \Exception \HttpException::class);
44+ $ this ->expectExceptionMessage ('Invalid Host Header ' );
45+
46+ $ this ->middleware ->handle ($ request , function ($ req ) {
47+ return response ()->json (['status ' => 'success ' ]);
48+ });
49+ }
50+
51+ public function test_missing_forwarded_host ()
52+ {
53+ $ request = Request::create ('https://example.processmaker.net ' );
54+
55+ $ response = $ this ->middleware ->handle ($ request , function ($ req ) {
56+ return response ()->json (['status ' => 'success ' ]);
57+ });
58+
59+ $ this ->assertEquals (200 , $ response ->status ());
60+ }
61+ }
0 commit comments