generated from yii2-extensions/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
refactor(tests): Add comprehensive tests for getServerPort() method in Request class to ensure correct handling of forwarded ports and server parameters.
#84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a529e4a
refactor(tests): Add comprehensive tests for `getServerPort()` method…
terabytesoftw 9ddb4d2
Apply fixes from StyleCI
StyleCIBot 412969e
refactor(Request): Improve `getServerPort()` method by using local he…
terabytesoftw 521a4f8
refactor(tests): Add test for handling comma-separated ports in `getS…
terabytesoftw 34108e8
refactor(tests): Remove redundant server port tests and add data prov…
terabytesoftw db07386
refactor(tests): Remove redundant test for server port in `ServerPara…
terabytesoftw 9ca786a
refactor(tests): Add test case for integer return value of 'getServer…
terabytesoftw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,182 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yii2\extensions\psrbridge\tests\provider; | ||
|
|
||
| final class ServerParamsPsr7Provider | ||
| { | ||
| /** | ||
| * @phpstan-return array< | ||
| * string, | ||
| * array{ | ||
| * array<string, mixed>, | ||
| * array<string, mixed>, | ||
| * array<string, array<int, string>|int|string>, | ||
| * array<string, mixed>, | ||
| * int|null, | ||
| * string, | ||
| * } | ||
| * > | ||
| */ | ||
| public static function serverPortCases(): array | ||
| { | ||
| return [ | ||
| 'Forwarded port when request from trusted proxy' => [ | ||
| [ | ||
| 'portHeaders' => ['X-Forwarded-Port'], | ||
| 'trustedHosts' => ['10.0.0.0/24'], // trust this subnet | ||
| ], | ||
| ['REMOTE_ADDR' => '10.0.0.1'], | ||
| ['X-Forwarded-Port' => '443'], | ||
| [ | ||
| 'SERVER_PORT' => '8080', | ||
| 'REMOTE_ADDR' => '10.0.0.1', | ||
| ], | ||
| 443, | ||
| "'getServerPort()' should return forwarded port when request comes from trusted proxy.", | ||
| ], | ||
| 'Ignore forwarded port when request from untrusted host' => [ | ||
| [ | ||
| 'portHeaders' => ['X-Forwarded-Port'], | ||
| 'secureHeaders' => ['X-Forwarded-Port'], | ||
| 'trustedHosts' => ['10.0.0.0/24'], // only trust this subnet | ||
| ], | ||
| ['REMOTE_ADDR' => '192.168.1.100'], | ||
| ['X-Forwarded-Port' => '443'], | ||
| [ | ||
| 'REMOTE_ADDR' => '192.168.1.100', | ||
| 'SERVER_PORT' => '8080', | ||
| ], | ||
| 8080, | ||
| "'getServerPort()' should ignore forwarded port header from untrusted hosts and use 'SERVER_PORT'.", | ||
| ], | ||
| 'Null when PSR-7 request server port is empty array' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['SERVER_PORT' => []], | ||
| null, | ||
| "'SERVER_PORT' should return 'null' from PSR-7 'serverParams' when adapter is set but 'SERVER_PORT' " . | ||
| 'is an empty array.', | ||
| ], | ||
| 'Null when PSR-7 request server port is null' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['SERVER_PORT' => null], | ||
| null, | ||
| "'SERVER_PORT' should return 'null' from PSR-7 'serverParams' when adapter is set but 'SERVER_PORT' " . | ||
| "is 'null'.", | ||
| ], | ||
| 'Null when PSR-7 request server port is not present' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['HTTP_HOST' => 'example.com'], | ||
| null, | ||
| "'SERVER_PORT' should return 'null' from PSR-7 'serverParams' when adapter is set but 'SERVER_PORT' " . | ||
| 'is not present.', | ||
| ], | ||
| 'Null when PSR-7 request server port is not string' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['SERVER_PORT' => ['invalid' => 'array']], | ||
| null, | ||
| "'SERVER_PORT' should return 'null' from PSR-7 'serverParams' when adapter is set but 'SERVER_PORT' " . | ||
| 'is not a string.', | ||
| ], | ||
| 'Server port as integer when PSR-7 server port.' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['SERVER_PORT' => '443'], | ||
| 443, | ||
| "'getServerPort()' should return integer value when 'SERVER_PORT' is a numeric string.", | ||
| ], | ||
| 'Server port as integer when PSR-7 server port is numeric string' => [ | ||
| [], | ||
| [], | ||
| [], | ||
| ['SERVER_PORT' => '443'], | ||
| 443, | ||
| "'getServerPort()' should return integer value when 'SERVER_PORT' is a numeric string.", | ||
| ], | ||
| 'Server port from comma separated forwarded header' => [ | ||
| [ | ||
| 'portHeaders' => ['X-Forwarded-Port'], | ||
| 'secureHeaders' => ['X-Forwarded-Port'], | ||
| 'trustedHosts' => ['127.0.0.1'], | ||
| ], | ||
| ['REMOTE_ADDR' => '127.0.0.1'], | ||
| ['X-Forwarded-Port' => '9443, 7443'], | ||
| [ | ||
| 'REMOTE_ADDR' => '127.0.0.1', | ||
| 'SERVER_PORT' => '80', | ||
| ], | ||
| 9443, | ||
| "'getServerPort()' should return the first port from a comma-separated 'X-Forwarded-Port' header.", | ||
| ], | ||
| 'Server port from first valid forwarded header when multiple configured' => [ | ||
| [ | ||
| 'portHeaders' => [ | ||
| 'X-Custom-Port', | ||
| 'X-Forwarded-Port', | ||
| 'X-Real-Port', | ||
| ], | ||
| 'secureHeaders' => [ | ||
| 'X-Custom-Port', | ||
| 'X-Forwarded-For', | ||
| 'X-Forwarded-Host', | ||
| 'X-Forwarded-Port', | ||
| 'X-Forwarded-Proto', | ||
| 'X-Real-Port', | ||
| ], | ||
| 'trustedHosts' => ['127.0.0.1'], | ||
| ], | ||
| ['REMOTE_ADDR' => '127.0.0.1'], | ||
| [ | ||
| 'X-Custom-Port' => '', | ||
| 'X-Forwarded-Port' => '9443', | ||
| 'X-Real-Port' => '7443', | ||
| ], | ||
| [ | ||
| 'REMOTE_ADDR' => '127.0.0.1', | ||
| 'SERVER_PORT' => '80', | ||
| ], | ||
| 9443, | ||
| "'getServerPort()' should return the port from the first valid forwarded header in the configured " . | ||
| 'list.', | ||
| ], | ||
| 'Server port from forwarded header when adapter is set' => [ | ||
| [ | ||
| 'portHeaders' => ['X-Forwarded-Port'], | ||
| 'trustedHosts' => ['127.0.0.1'], | ||
| ], | ||
| ['REMOTE_ADDR' => '127.0.0.1'], | ||
| ['X-Forwarded-Port' => '443'], | ||
| [ | ||
| 'REMOTE_ADDR' => '127.0.0.1', | ||
| 'SERVER_PORT' => '8080', | ||
| ], | ||
| 443, | ||
| "'getServerPort()' should return the port from 'X-Forwarded-Port' header when present, ignoring " . | ||
| "'SERVER_PORT' from PSR-7 'serverParams'.", | ||
| ], | ||
| 'Server port from PSR-7 request when adapter is set and server port present' => [ | ||
| [ | ||
| 'portHeaders' => [ | ||
| 'X-Custom-Port', | ||
| 'X-Forwarded-Port', | ||
| ], | ||
| ], | ||
| [], | ||
| ['X-Custom-Port' => ''], | ||
| ['SERVER_PORT' => '3000'], | ||
| 3000, | ||
| "'getServerPort()' should fallback to 'SERVER_PORT' when all forwarded headers are 'null' or missing.", | ||
| ], | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.