Skip to content

Commit

Permalink
Refactor Request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codeguy committed Dec 26, 2015
1 parent 09e4924 commit 9347a93
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 187 deletions.
10 changes: 8 additions & 2 deletions src/FactoryDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ public function makeRequest(array $globals) : ServerRequestInterface
$method = $globals['REQUEST_METHOD'];
$uri = $this->makeUri($globals);
$headers = $this->makeHeaders($globals);
$cookies = []; // TODO: Should these be internal to headers?
$cookies = Cookies::parseHeader($headers->get('Cookie', []));
$body = $this->makeBody();
$files = []; // TODO: Create factory method for uploaded files
$request = new Request($method, $uri, $headers, $cookies, $globals, $body, $files);
if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])
) {
// parsed body must be $_POST
$request = $request->withParsedBody($_POST);
}

return new Request($method, $uri, $headers, $cookies, $globals, $body, $files);
return $request;
}

/**
Expand Down
83 changes: 0 additions & 83 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,89 +113,6 @@ public function __construct($scheme, $host, $port = null, $path = '/', $query =
$this->password = $password;
}

/**
* Create new Uri from string.
*
* @param string $uri Complete Uri string
* (i.e., https://user:pass@host:443/path?query).
*
* @return self
*/
public static function createFromString($uri)
{
if (!is_string($uri) && !method_exists($uri, '__toString')) {
throw new InvalidArgumentException('Uri must be a string');
}

$parts = parse_url($uri);
$scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
$user = isset($parts['user']) ? $parts['user'] : '';
$pass = isset($parts['pass']) ? $parts['pass'] : '';
$host = isset($parts['host']) ? $parts['host'] : '';
$port = isset($parts['port']) ? $parts['port'] : null;
$path = isset($parts['path']) ? $parts['path'] : '';
$query = isset($parts['query']) ? $parts['query'] : '';
$fragment = isset($parts['fragment']) ? $parts['fragment'] : '';

return new static($scheme, $host, $port, $path, $query, $fragment, $user, $pass);
}

/**
* Create new Uri from global server variables.
*
* @param array $globals
*
* @return self
*/
public static function createFromGlobals(array $globals)
{
$env = new Collection($globals);

// Scheme
$isSecure = $env->get('HTTPS');
$scheme = (empty($isSecure) || $isSecure === 'off') ? 'http' : 'https';

// Authority: Username and password
$username = $env->get('PHP_AUTH_USER', '');
$password = $env->get('PHP_AUTH_PW', '');

// Authority: Host
if ($env->has('HTTP_HOST')) {
$host = $env->get('HTTP_HOST');
} else {
$host = $env->get('SERVER_NAME');
}

// Authority: Port
$port = (int)$env->get('SERVER_PORT', 80);
if (preg_match('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/', $host, $matches)) {
$host = $matches[1];

if ($matches[2]) {
$port = (int) substr($matches[2], 1);
}
} else {
$pos = strpos($host, ':');
if ($pos !== false) {
$port = (int) substr($host, $pos + 1);
$host = strstr($host, ':', true);
}
}

// parse_url() requires a full URL. As we don't extract the domain name or scheme,
// we use a stand-in.
$requestUri = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_PATH);

// Query string
$queryString = $env->get('QUERY_STRING', '');

// Fragment
$fragment = '';

// Build Uri
return new static($scheme, $host, $port, $requestUri, $queryString, $fragment, $username, $password);
}

/********************************************************************************
* Scheme
*******************************************************************************/
Expand Down
133 changes: 55 additions & 78 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@
use Slim\Http\RequestBody;
use Slim\Http\UploadedFile;
use Slim\Http\Uri;
use Slim\Http\FactoryDefault;

class RequestTest extends \PHPUnit_Framework_TestCase
{
public function requestFactory()
public function requestFactory(array $customGlobals = [])
{
$env = Environment::mock();
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = Headers::createFromGlobals($env);
$cookies = [
'user' => 'john',
'id' => '123',
];
$serverParams = $env;
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromGlobals($env);
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
$env = Environment::mock(array_merge([
'HTTPS' => 1,
'HTTP_HOST' => 'example.com',
'SERVER_PORT' => 443,
'REQUEST_URI' => '/foo/bar?abc=123',
'QUERY_STRING' => 'abc=123',
'HTTP_COOKIE' => 'user=john;id=123'
], $customGlobals));

return $request;
return (new FactoryDefault())->makeRequest($env);
}

public function uriFactory(array $customGlobals = [])
{
$env = Environment::mock(array_merge([
'HTTPS' => 1,
'HTTP_HOST' => 'example.com',
'SERVER_PORT' => 443,
'REQUEST_URI' => '/foo/bar?abc=123',
'QUERY_STRING' => 'abc=123',
'HTTP_COOKIE' => 'user=john;id=123'
], $customGlobals));

return (new FactoryDefault())->makeUri($env);
}

public function testDisableSetter()
Expand Down Expand Up @@ -88,15 +100,12 @@ public function testWithMethodNull()
public function testCreateFromEnvironmentWithMultipart()
{
$_POST['foo'] = 'bar';

$env = Environment::mock([
$request = $this->requestFactory([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo',
'REQUEST_METHOD' => 'POST',
'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo'
]);

$request = Request::createFromGlobals($env);
unset($_POST);

$this->assertEquals(['foo' => 'bar'], $request->getParsedBody());
Expand All @@ -107,25 +116,7 @@ public function testCreateFromEnvironmentWithMultipart()
*/
public function testCreateRequestWithInvalidMethodString()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = new Headers();
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$request = new Request('FOO', $uri, $headers, $cookies, $serverParams, $body);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testCreateRequestWithInvalidMethodOther()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = new Headers();
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$request = new Request(10, $uri, $headers, $cookies, $serverParams, $body);
$this->requestFactory(['REQUEST_METHOD' => 'FOO']);
}

/*******************************************************************************
Expand Down Expand Up @@ -174,42 +165,32 @@ public function testWithRequestTargetThatHasSpaces()

public function testGetUri()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = new Headers();
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$request = $this->requestFactory();

$this->assertSame($uri, $request->getUri());
$this->assertInstanceOf('\Psr\Http\Message\UriInterface', $request->getUri());
}

public function testWithUri()
{
// Uris
$uri1 = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$uri2 = Uri::createFromString('https://example2.com:443/test?xyz=123');

// Request
$headers = new Headers();
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$request = new Request('GET', $uri1, $headers, $cookies, $serverParams, $body);
$clone = $request->withUri($uri2);
$globals = [
'HTTPS' => 1,
'HTTP_HOST' => 'example2.com',
'SERVER_PORT' => 443,
'REQUEST_URI' => '/test?xyz=123',
'QUERY_STRING' => 'xyz=123'
];
$uriCustom = $this->uriFactory($globals);
$request = $this->requestFactory($globals);
$request = $request->withUri($uriCustom);

$this->assertAttributeSame($uri2, 'uri', $clone);
$this->assertAttributeSame($uriCustom, 'uri', $request);
}

public function testGetContentType()
{
$headers = new Headers([
'Content-Type' => ['application/json;charset=utf8'],
$request = $this->requestFactory([
'HTTP_CONTENT_TYPE' => 'application/json;charset=utf8'
]);
$request = $this->requestFactory();
$headersProp = new ReflectionProperty($request, 'headers');
$headersProp->setAccessible(true);
$headersProp->setValue($request, $headers);

$this->assertEquals('application/json;charset=utf8', $request->getContentType());
}
Expand All @@ -223,13 +204,9 @@ public function testGetContentTypeEmpty()

public function testGetMediaType()
{
$headers = new Headers([
'Content-Type' => ['application/json;charset=utf8'],
$request = $this->requestFactory([
'HTTP_CONTENT_TYPE' => 'application/json;charset=utf8'
]);
$request = $this->requestFactory();
$headersProp = new ReflectionProperty($request, 'headers');
$headersProp->setAccessible(true);
$headersProp->setValue($request, $headers);

$this->assertEquals('application/json', $request->getMediaType());
}
Expand Down Expand Up @@ -325,7 +302,9 @@ public function testWithUploadedFiles()
* Server Params
******************************************************************************/

public function testGetServerParams()
// TODO: Update this test

/*public function testGetServerParams()
{
$mockEnv = Environment::mock();
$request = $this->requestFactory();
Expand All @@ -346,12 +325,14 @@ public function testGetServerParams()
);
}
}
}
}*/

/*******************************************************************************
* File Params
******************************************************************************/

// TODO: Write file params tests

/*******************************************************************************
* Attributes
******************************************************************************/
Expand Down Expand Up @@ -486,18 +467,14 @@ public function testGetParsedBodyWhenBodyDoesNotExist()
/**
* @expectedException \RuntimeException
*/
public function testGetParsedBodyAsArray()
public function testGetParsedBodyWithParseError()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = new Headers([
'Content-Type' => 'application/json;charset=utf8',
$request = $this->requestFactory([
'HTTP_CONTENT_TYPE' => 'application/json;charset=utf8'
]);
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$body->write('{"foo": "bar"}');
$body = $request->getBody();
$body->write('{"foo":"bar"}');
$body->rewind();
$request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body);
$request->registerMediaTypeParser('application/json', function ($input) {
return 10; // <-- Return invalid body value
});
Expand Down
24 changes: 0 additions & 24 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,30 +389,6 @@ public function testToString()
$this->assertEquals('https://josh:[email protected]/foo/?abc=123', (string)$uri);
}

/**
* @covers Slim\Http\Uri::createFromString
*/
public function testCreateFromString()
{
$uri = Uri::createFromString('https://example.com:8080/foo/bar?abc=123');

$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
}

/**
* @covers Slim\Http\Uri::createFromString
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri must be a string
*/
public function testCreateFromStringWithInvalidType()
{
Uri::createFromString(['https://example.com:8080/foo/bar?abc=123']);
}

public function testCreateEnvironment()
{
$uri = $this->uriFactory([
Expand Down

0 comments on commit 9347a93

Please sign in to comment.