Skip to content

Commit

Permalink
Merge branch 'juanpagfe-mock-https-request'
Browse files Browse the repository at this point in the history
Closes slimphp#42
  • Loading branch information
akrabat committed Nov 26, 2017
2 parents 2aabc4d + 3af4641 commit f0ed949
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ class Environment
*/
public static function mock(array $userData = [])
{
//Validates if default protocol is HTTPS to set default port 443
if ((isset($userData['HTTPS']) && $userData['HTTPS'] !== 'off') ||
((isset($userData['REQUEST_SCHEME']) && $userData['REQUEST_SCHEME'] === 'https'))) {
$defscheme = 'https';
$defport = 443;
} else {
$defscheme = 'http';
$defport = 80;
}

return array_merge([
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
'REQUEST_SCHEME' => $defscheme,
'SCRIPT_NAME' => '',
'REQUEST_URI' => '',
'QUERY_STRING' => '',
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => 80,
'SERVER_PORT' => $defport,
'HTTP_HOST' => 'localhost',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
Expand Down
28 changes: 28 additions & 0 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,32 @@ public function testMock()
$this->assertEquals('/foo/bar?abc=123', $env['REQUEST_URI']);
$this->assertEquals('localhost', $env['HTTP_HOST']);
}

/**
* Test environment from mock data with HTTPS
*/
public function testMockHttps()
{
$env = Environment::mock([
'HTTPS' => 'on'
]);

$this->assertInternalType('array', $env);
$this->assertEquals('on', $env['HTTPS']);
$this->assertEquals(443, $env['SERVER_PORT']);
}

/**
* Test environment from mock data with REQUEST_SCHEME
*/
public function testMockRequestScheme()
{
$env = Environment::mock([
'REQUEST_SCHEME' => 'https'
]);

$this->assertInternalType('array', $env);
$this->assertEquals('https', $env['REQUEST_SCHEME']);
$this->assertEquals(443, $env['SERVER_PORT']);
}
}

0 comments on commit f0ed949

Please sign in to comment.