Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F4793080
RequestTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
RequestTest.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
Keruald\OmniTools\Tests\HTTP\Requests
;
use
Keruald\OmniTools\HTTP\Requests\Request
;
use
PHPUnit\Framework\TestCase
;
class
RequestTest
extends
TestCase
{
///
/// Tests
///
/**
* @covers \Keruald\OmniTools\HTTP\Requests\Request::getRemoteAddress
* @backupGlobals enabled
*/
public
function
testGetRemoteAddress
()
:
void
{
$this
->
assertEmpty
(
Request
::
getRemoteAddress
());
$_SERVER
=
[
'REMOTE_ADDR'
=>
'10.0.0.2'
,
];
$this
->
assertEquals
(
'10.0.0.2'
,
Request
::
getRemoteAddress
());
$_SERVER
+=
[
'HTTP_X_FORWARDED_FOR'
=>
'10.0.0.3'
,
'HTTP_CLIENT_IP'
=>
'10.0.0.4'
,
];
$this
->
assertEquals
(
'10.0.0.3'
,
Request
::
getRemoteAddress
(),
"HTTP_X_FORWARDED_FOR must be prioritized."
);
}
/**
* @covers \Keruald\OmniTools\HTTP\Requests\Request::getClientAddress
* @backupGlobals enabled
*/
public
function
testGetRemoteAddressWithSeveralAddresses
()
:
void
{
$_SERVER
=
[
'HTTP_X_FORWARDED_FOR'
=>
'10.0.0.2 10.0.0.3'
,
];
$this
->
assertEquals
(
'10.0.0.2'
,
Request
::
getRemoteAddress
(),
"HTTP_X_FORWARDED_FOR could contain more than one address, the client one is the first"
);
$_SERVER
=
[
'HTTP_X_FORWARDED_FOR'
=>
'10.0.0.2, 10.0.0.3'
,
];
$this
->
assertEquals
(
'10.0.0.2'
,
Request
::
getRemoteAddress
(),
"HTTP_X_FORWARDED_FOR could contain more than one address, the client one is the first"
);
}
/**
* @covers \Keruald\OmniTools\HTTP\Requests\Request::getAcceptedLanguages
* @backupGlobals enabled
*/
public
function
testGetAcceptedLanguages
()
:
void
{
$_SERVER
=
[
'HTTP_ACCEPT_LANGUAGE'
=>
'fr,en-US;q=0.7,en;q=0.3'
,
];
$this
->
assertEquals
(
[
"fr"
,
"en-US"
,
"en"
],
Request
::
getAcceptedLanguages
()
);
}
/**
* @backupGlobals enabled
* @dataProvider provideServerURLs
*/
public
function
testGetServerURL
(
array
$server
,
string
$url
)
:
void
{
$_SERVER
=
$server
;
$this
->
assertEquals
(
$url
,
Request
::
getServerURL
());
}
/**
* @backupGlobals enabled
* @dataProvider provideServerURLs
*/
public
function
testCreateLocalURL
(
array
$server
,
string
$url
)
:
void
{
$_SERVER
=
$server
;
$this
->
assertEquals
(
$url
.
"/"
,
Request
::
createLocalURL
()->
__toString
()
);
$this
->
assertEquals
(
$url
.
"/foo"
,
Request
::
createLocalURL
(
"foo"
)->
__toString
()
);
}
///
/// Data providers
///
public
function
provideServerURLs
()
:
iterable
{
yield
[[],
"http://localhost"
];
yield
[[
"UNRELATED"
=>
"ANYTHING"
],
"http://localhost"
];
yield
[
[
"SERVER_PORT"
=>
"80"
,
"SERVER_NAME"
=>
"acme.tld"
,
],
"http://acme.tld"
];
yield
[
[
"SERVER_PORT"
=>
"443"
,
"SERVER_NAME"
=>
"acme.tld"
,
],
"https://acme.tld"
];
yield
[
[
"SERVER_PORT"
=>
"80"
,
"SERVER_NAME"
=>
"acme.tld"
,
"HTTP_X_FORWARDED_PROTO"
=>
"https"
,
],
"https://acme.tld"
];
yield
[
[
"SERVER_PORT"
=>
"80"
,
"SERVER_NAME"
=>
"acme.tld"
,
"HTTP_FORWARDED"
=>
"for=192.0.2.43, for=
\"
[2001:db8:cafe::17]
\"
, proto=https, by=203.0.113.43"
,
],
"https://acme.tld"
];
yield
[
[
"SERVER_PORT"
=>
"8443"
,
"SERVER_NAME"
=>
"acme.tld"
,
"HTTPS"
=>
"on"
,
],
"https://acme.tld:8443"
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Feb 28, 21:39 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2436528
Default Alt Text
RequestTest.php (3 KB)
Attached To
Mode
rKOT Keruald OmniTools
Attached
Detach File
Event Timeline
Log In to Comment