From 253852823cb85945dcc1c391bfbc208ea673a8ba Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sun, 7 May 2017 17:02:48 +0100 Subject: [PATCH 1/4] Remove Slim\Http files --- Slim/Http/Body.php | 22 - Slim/Http/Cookies.php | 195 --- Slim/Http/Environment.php | 52 - Slim/Http/Headers.php | 222 --- Slim/Http/Message.php | 304 ----- Slim/Http/Request.php | 1197 ----------------- Slim/Http/RequestBody.php | 27 - Slim/Http/Response.php | 480 ------- Slim/Http/Stream.php | 450 ------- Slim/Http/UploadedFile.php | 327 ----- Slim/Http/Uri.php | 762 ----------- Slim/Interfaces/Http/CookiesInterface.php | 23 - Slim/Interfaces/Http/EnvironmentInterface.php | 20 - Slim/Interfaces/Http/HeadersInterface.php | 24 - tests/Http/BodyTest.php | 416 ------ tests/Http/CookiesTest.php | 235 ---- tests/Http/EnvironmentTest.php | 59 - tests/Http/HeadersTest.php | 228 ---- tests/Http/MessageTest.php | 214 --- tests/Http/RequestBodyTest.php | 334 ----- tests/Http/RequestTest.php | 1168 ---------------- tests/Http/ResponseTest.php | 341 ----- tests/Http/StreamTest.php | 159 --- tests/Http/UploadedFilesTest.php | 485 ------- tests/Http/UriTest.php | 609 --------- 25 files changed, 8353 deletions(-) delete mode 100644 Slim/Http/Body.php delete mode 100644 Slim/Http/Cookies.php delete mode 100644 Slim/Http/Environment.php delete mode 100644 Slim/Http/Headers.php delete mode 100644 Slim/Http/Message.php delete mode 100644 Slim/Http/Request.php delete mode 100644 Slim/Http/RequestBody.php delete mode 100644 Slim/Http/Response.php delete mode 100644 Slim/Http/Stream.php delete mode 100644 Slim/Http/UploadedFile.php delete mode 100644 Slim/Http/Uri.php delete mode 100644 Slim/Interfaces/Http/CookiesInterface.php delete mode 100644 Slim/Interfaces/Http/EnvironmentInterface.php delete mode 100644 Slim/Interfaces/Http/HeadersInterface.php delete mode 100644 tests/Http/BodyTest.php delete mode 100644 tests/Http/CookiesTest.php delete mode 100644 tests/Http/EnvironmentTest.php delete mode 100644 tests/Http/HeadersTest.php delete mode 100644 tests/Http/MessageTest.php delete mode 100644 tests/Http/RequestBodyTest.php delete mode 100644 tests/Http/RequestTest.php delete mode 100755 tests/Http/ResponseTest.php delete mode 100644 tests/Http/StreamTest.php delete mode 100644 tests/Http/UploadedFilesTest.php delete mode 100644 tests/Http/UriTest.php diff --git a/Slim/Http/Body.php b/Slim/Http/Body.php deleted file mode 100644 index 7a7b4df81..000000000 --- a/Slim/Http/Body.php +++ /dev/null @@ -1,22 +0,0 @@ - '', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => false, - 'httponly' => false - ]; - - /** - * Create new cookies helper - * - * @param array $cookies - */ - public function __construct(array $cookies = []) - { - $this->requestCookies = $cookies; - } - - /** - * Set default cookie properties - * - * @param array $settings - */ - public function setDefaults(array $settings) - { - $this->defaults = array_replace($this->defaults, $settings); - } - - /** - * Get request cookie - * - * @param string $name Cookie name - * @param mixed $default Cookie default value - * - * @return mixed Cookie value if present, else default - */ - public function get($name, $default = null) - { - return isset($this->requestCookies[$name]) ? $this->requestCookies[$name] : $default; - } - - /** - * Set response cookie - * - * @param string $name Cookie name - * @param string|array $value Cookie value, or cookie properties - */ - public function set($name, $value) - { - if (!is_array($value)) { - $value = ['value' => (string)$value]; - } - $this->responseCookies[$name] = array_replace($this->defaults, $value); - } - - /** - * Convert to `Set-Cookie` headers - * - * @return string[] - */ - public function toHeaders() - { - $headers = []; - foreach ($this->responseCookies as $name => $properties) { - $headers[] = $this->toHeader($name, $properties); - } - - return $headers; - } - - /** - * Convert to `Set-Cookie` header - * - * @param string $name Cookie name - * @param array $properties Cookie properties - * - * @return string - */ - protected function toHeader($name, array $properties) - { - $result = urlencode($name) . '=' . urlencode($properties['value']); - - if (isset($properties['domain'])) { - $result .= '; domain=' . $properties['domain']; - } - - if (isset($properties['path'])) { - $result .= '; path=' . $properties['path']; - } - - if (isset($properties['expires'])) { - if (is_string($properties['expires'])) { - $timestamp = strtotime($properties['expires']); - } else { - $timestamp = (int)$properties['expires']; - } - if ($timestamp !== 0) { - $result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp); - } - } - - if (isset($properties['secure']) && $properties['secure']) { - $result .= '; secure'; - } - - if (isset($properties['hostonly']) && $properties['hostonly']) { - $result .= '; HostOnly'; - } - - if (isset($properties['httponly']) && $properties['httponly']) { - $result .= '; HttpOnly'; - } - - return $result; - } - - /** - * Parse HTTP request `Cookie:` header and extract - * into a PHP associative array. - * - * @param string $header The raw HTTP request `Cookie:` header - * - * @return array Associative array of cookie names and values - * - * @throws InvalidArgumentException if the cookie data cannot be parsed - */ - public static function parseHeader($header) - { - if (is_array($header) === true) { - $header = isset($header[0]) ? $header[0] : ''; - } - - if (is_string($header) === false) { - throw new InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.'); - } - - $header = rtrim($header, "\r\n"); - $pieces = preg_split('@[;]\s*@', $header); - $cookies = []; - - foreach ($pieces as $cookie) { - $cookie = explode('=', $cookie, 2); - - if (count($cookie) === 2) { - $key = urldecode($cookie[0]); - $value = urldecode($cookie[1]); - - if (!isset($cookies[$key])) { - $cookies[$key] = $value; - } - } - } - - return $cookies; - } -} diff --git a/Slim/Http/Environment.php b/Slim/Http/Environment.php deleted file mode 100644 index 786dc0a22..000000000 --- a/Slim/Http/Environment.php +++ /dev/null @@ -1,52 +0,0 @@ - 'HTTP/1.1', - 'REQUEST_METHOD' => 'GET', - 'SCRIPT_NAME' => '', - 'REQUEST_URI' => '', - 'QUERY_STRING' => '', - 'SERVER_NAME' => 'localhost', - 'SERVER_PORT' => 80, - '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', - 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', - 'HTTP_USER_AGENT' => 'Slim Framework', - 'REMOTE_ADDR' => '127.0.0.1', - 'REQUEST_TIME' => time(), - 'REQUEST_TIME_FLOAT' => microtime(true), - ], $userData); - - return new static($data); - } -} diff --git a/Slim/Http/Headers.php b/Slim/Http/Headers.php deleted file mode 100644 index f8c4ac140..000000000 --- a/Slim/Http/Headers.php +++ /dev/null @@ -1,222 +0,0 @@ - 1, - 'CONTENT_LENGTH' => 1, - 'PHP_AUTH_USER' => 1, - 'PHP_AUTH_PW' => 1, - 'PHP_AUTH_DIGEST' => 1, - 'AUTH_TYPE' => 1, - ]; - - /** - * Create new headers collection with data extracted from - * the application Environment object - * - * @param Environment $environment The Slim application Environment - * - * @return self - */ - public static function createFromEnvironment(Environment $environment) - { - $data = []; - $environment = self::determineAuthorization($environment); - foreach ($environment as $key => $value) { - $key = strtoupper($key); - if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) { - if ($key !== 'HTTP_CONTENT_LENGTH') { - $data[$key] = $value; - } - } - } - - return new static($data); - } - - /** - * If HTTP_AUTHORIZATION does not exist tries to get it from - * getallheaders() when available. - * - * @param Environment $environment The Slim application Environment - * - * @return Environment - */ - - public static function determineAuthorization(Environment $environment) - { - $authorization = $environment->get('HTTP_AUTHORIZATION'); - - if (null === $authorization && is_callable('getallheaders')) { - $headers = getallheaders(); - $headers = array_change_key_case($headers, CASE_LOWER); - if (isset($headers['authorization'])) { - $environment->set('HTTP_AUTHORIZATION', $headers['authorization']); - } - } - - return $environment; - } - - /** - * Return array of HTTP header names and values. - * This method returns the _original_ header name - * as specified by the end user. - * - * @return array - */ - public function all() - { - $all = parent::all(); - $out = []; - foreach ($all as $key => $props) { - $out[$props['originalKey']] = $props['value']; - } - - return $out; - } - - /** - * Set HTTP header value - * - * This method sets a header value. It replaces - * any values that may already exist for the header name. - * - * @param string $key The case-insensitive header name - * @param string $value The header value - */ - public function set($key, $value) - { - if (!is_array($value)) { - $value = [$value]; - } - parent::set($this->normalizeKey($key), [ - 'value' => $value, - 'originalKey' => $key - ]); - } - - /** - * Get HTTP header value - * - * @param string $key The case-insensitive header name - * @param mixed $default The default value if key does not exist - * - * @return string[] - */ - public function get($key, $default = null) - { - if ($this->has($key)) { - return parent::get($this->normalizeKey($key))['value']; - } - - return $default; - } - - /** - * Get HTTP header key as originally specified - * - * @param string $key The case-insensitive header name - * @param mixed $default The default value if key does not exist - * - * @return string - */ - public function getOriginalKey($key, $default = null) - { - if ($this->has($key)) { - return parent::get($this->normalizeKey($key))['originalKey']; - } - - return $default; - } - - /** - * Add HTTP header value - * - * This method appends a header value. Unlike the set() method, - * this method _appends_ this new value to any values - * that already exist for this header name. - * - * @param string $key The case-insensitive header name - * @param array|string $value The new header value(s) - */ - public function add($key, $value) - { - $oldValues = $this->get($key, []); - $newValues = is_array($value) ? $value : [$value]; - $this->set($key, array_merge($oldValues, array_values($newValues))); - } - - /** - * Does this collection have a given header? - * - * @param string $key The case-insensitive header name - * - * @return bool - */ - public function has($key) - { - return parent::has($this->normalizeKey($key)); - } - - /** - * Remove header from collection - * - * @param string $key The case-insensitive header name - */ - public function remove($key) - { - parent::remove($this->normalizeKey($key)); - } - - /** - * Normalize header name - * - * This method transforms header names into a - * normalized form. This is how we enable case-insensitive - * header names in the other methods in this class. - * - * @param string $key The case-insensitive header name - * - * @return string Normalized header name - */ - public function normalizeKey($key) - { - $key = strtr(strtolower($key), '_', '-'); - if (strpos($key, 'http-') === 0) { - $key = substr($key, 5); - } - - return $key; - } -} diff --git a/Slim/Http/Message.php b/Slim/Http/Message.php deleted file mode 100644 index d02a43c85..000000000 --- a/Slim/Http/Message.php +++ /dev/null @@ -1,304 +0,0 @@ - true, - '1.1' => true, - '2.0' => true, - ]; - - /** - * Headers - * - * @var \Slim\Interfaces\Http\HeadersInterface - */ - protected $headers; - - /** - * Body object - * - * @var \Psr\Http\Message\StreamInterface - */ - protected $body; - - - /** - * Disable magic setter to ensure immutability - */ - public function __set($name, $value) - { - // Do nothing - } - - /******************************************************************************* - * Protocol - ******************************************************************************/ - - /** - * Retrieves the HTTP protocol version as a string. - * - * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). - * - * @return string HTTP protocol version. - */ - public function getProtocolVersion() - { - return $this->protocolVersion; - } - - /** - * Return an instance with the specified HTTP protocol version. - * - * The version string MUST contain only the HTTP version number (e.g., - * "1.1", "1.0"). - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * new protocol version. - * - * @param string $version HTTP protocol version - * @return static - * @throws InvalidArgumentException if the http version is an invalid number - */ - public function withProtocolVersion($version) - { - if (!isset(self::$validProtocolVersions[$version])) { - throw new InvalidArgumentException( - 'Invalid HTTP version. Must be one of: ' - . implode(', ', array_keys(self::$validProtocolVersions)) - ); - } - $clone = clone $this; - $clone->protocolVersion = $version; - - return $clone; - } - - /******************************************************************************* - * Headers - ******************************************************************************/ - - /** - * Retrieves all message header values. - * - * The keys represent the header name as it will be sent over the wire, and - * each value is an array of strings associated with the header. - * - * // Represent the headers as a string - * foreach ($message->getHeaders() as $name => $values) { - * echo $name . ": " . implode(", ", $values); - * } - * - * // Emit headers iteratively: - * foreach ($message->getHeaders() as $name => $values) { - * foreach ($values as $value) { - * header(sprintf('%s: %s', $name, $value), false); - * } - * } - * - * While header names are not case-sensitive, getHeaders() will preserve the - * exact case in which headers were originally specified. - * - * @return array Returns an associative array of the message's headers. Each - * key MUST be a header name, and each value MUST be an array of strings - * for that header. - */ - public function getHeaders() - { - return $this->headers->all(); - } - - /** - * Checks if a header exists by the given case-insensitive name. - * - * @param string $name Case-insensitive header field name. - * @return bool Returns true if any header names match the given header - * name using a case-insensitive string comparison. Returns false if - * no matching header name is found in the message. - */ - public function hasHeader($name) - { - return $this->headers->has($name); - } - - /** - * Retrieves a message header value by the given case-insensitive name. - * - * This method returns an array of all the header values of the given - * case-insensitive header name. - * - * If the header does not appear in the message, this method MUST return an - * empty array. - * - * @param string $name Case-insensitive header field name. - * @return string[] An array of string values as provided for the given - * header. If the header does not appear in the message, this method MUST - * return an empty array. - */ - public function getHeader($name) - { - return $this->headers->get($name, []); - } - - /** - * Retrieves a comma-separated string of the values for a single header. - * - * This method returns all of the header values of the given - * case-insensitive header name as a string concatenated together using - * a comma. - * - * NOTE: Not all header values may be appropriately represented using - * comma concatenation. For such headers, use getHeader() instead - * and supply your own delimiter when concatenating. - * - * If the header does not appear in the message, this method MUST return - * an empty string. - * - * @param string $name Case-insensitive header field name. - * @return string A string of values as provided for the given header - * concatenated together using a comma. If the header does not appear in - * the message, this method MUST return an empty string. - */ - public function getHeaderLine($name) - { - return implode(',', $this->headers->get($name, [])); - } - - /** - * Return an instance with the provided value replacing the specified header. - * - * While header names are case-insensitive, the casing of the header will - * be preserved by this function, and returned from getHeaders(). - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * new and/or updated header and value. - * - * @param string $name Case-insensitive header field name. - * @param string|string[] $value Header value(s). - * @return static - * @throws \InvalidArgumentException for invalid header names or values. - */ - public function withHeader($name, $value) - { - $clone = clone $this; - $clone->headers->set($name, $value); - - return $clone; - } - - /** - * Return an instance with the specified header appended with the given value. - * - * Existing values for the specified header will be maintained. The new - * value(s) will be appended to the existing list. If the header did not - * exist previously, it will be added. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * new header and/or value. - * - * @param string $name Case-insensitive header field name to add. - * @param string|string[] $value Header value(s). - * @return static - * @throws \InvalidArgumentException for invalid header names or values. - */ - public function withAddedHeader($name, $value) - { - $clone = clone $this; - $clone->headers->add($name, $value); - - return $clone; - } - - /** - * Return an instance without the specified header. - * - * Header resolution MUST be done without case-sensitivity. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that removes - * the named header. - * - * @param string $name Case-insensitive header field name to remove. - * @return static - */ - public function withoutHeader($name) - { - $clone = clone $this; - $clone->headers->remove($name); - - return $clone; - } - - /******************************************************************************* - * Body - ******************************************************************************/ - - /** - * Gets the body of the message. - * - * @return StreamInterface Returns the body as a stream. - */ - public function getBody() - { - return $this->body; - } - - /** - * Return an instance with the specified message body. - * - * The body MUST be a StreamInterface object. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return a new instance that has the - * new body stream. - * - * @param StreamInterface $body Body. - * @return static - * @throws \InvalidArgumentException When the body is not valid. - */ - public function withBody(StreamInterface $body) - { - // TODO: Test for invalid body? - $clone = clone $this; - $clone->body = $body; - - return $clone; - } -} diff --git a/Slim/Http/Request.php b/Slim/Http/Request.php deleted file mode 100644 index 39a14ce49..000000000 --- a/Slim/Http/Request.php +++ /dev/null @@ -1,1197 +0,0 @@ -get('Cookie', [])); - $serverParams = $environment->all(); - $body = new RequestBody(); - $uploadedFiles = UploadedFile::createFromEnvironment($environment); - - $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); - - 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 $request; - } - - /** - * Create new HTTP request. - * - * Adds a host header when none was provided and a host is defined in uri. - * - * @param string $method The request method - * @param UriInterface $uri The request URI object - * @param HeadersInterface $headers The request headers collection - * @param array $cookies The request cookies collection - * @param array $serverParams The server environment variables - * @param StreamInterface $body The request body object - * @param array $uploadedFiles The request uploadedFiles collection - * @throws InvalidMethodException on invalid HTTP method - */ - public function __construct( - $method, - UriInterface $uri, - HeadersInterface $headers, - array $cookies, - array $serverParams, - StreamInterface $body, - array $uploadedFiles = [] - ) { - try { - $this->originalMethod = $this->filterMethod($method); - } catch (InvalidMethodException $e) { - $this->originalMethod = $method; - } - - $this->uri = $uri; - $this->headers = $headers; - $this->cookies = $cookies; - $this->serverParams = $serverParams; - $this->attributes = new Collection(); - $this->body = $body; - $this->uploadedFiles = $uploadedFiles; - - if (isset($serverParams['SERVER_PROTOCOL'])) { - $this->protocolVersion = str_replace('HTTP/', '', $serverParams['SERVER_PROTOCOL']); - } - - if (!$this->headers->has('Host') || $this->uri->getHost() !== '') { - $this->headers->set('Host', $this->uri->getHost()); - } - - $this->registerMediaTypeParser('application/json', function ($input) { - $result = json_decode($input, true); - if (!is_array($result)) { - return null; - } - return $result; - }); - - $this->registerMediaTypeParser('application/xml', function ($input) { - $backup = libxml_disable_entity_loader(true); - $backup_errors = libxml_use_internal_errors(true); - $result = simplexml_load_string($input); - libxml_disable_entity_loader($backup); - libxml_clear_errors(); - libxml_use_internal_errors($backup_errors); - if ($result === false) { - return null; - } - return $result; - }); - - $this->registerMediaTypeParser('text/xml', function ($input) { - $backup = libxml_disable_entity_loader(true); - $backup_errors = libxml_use_internal_errors(true); - $result = simplexml_load_string($input); - libxml_disable_entity_loader($backup); - libxml_clear_errors(); - libxml_use_internal_errors($backup_errors); - if ($result === false) { - return null; - } - return $result; - }); - - $this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) { - parse_str($input, $data); - return $data; - }); - - // if the request had an invalid method, we can throw it now - if (isset($e) && $e instanceof InvalidMethodException) { - throw $e; - } - } - - /** - * This method is applied to the cloned object - * after PHP performs an initial shallow-copy. This - * method completes a deep-copy by creating new objects - * for the cloned object's internal reference pointers. - */ - public function __clone() - { - $this->headers = clone $this->headers; - $this->attributes = clone $this->attributes; - $this->body = clone $this->body; - } - - /******************************************************************************* - * Method - ******************************************************************************/ - - /** - * Retrieves the HTTP method of the request. - * - * @return string Returns the request method. - */ - public function getMethod() - { - if ($this->method === null) { - $this->method = $this->originalMethod; - $customMethod = $this->getHeaderLine('X-Http-Method-Override'); - - if ($customMethod) { - $this->method = $this->filterMethod($customMethod); - } elseif ($this->originalMethod === 'POST') { - $overrideMethod = $this->filterMethod($this->getParsedBodyParam('_METHOD')); - if ($overrideMethod !== null) { - $this->method = $overrideMethod; - } - - if ($this->getBody()->eof()) { - $this->getBody()->rewind(); - } - } - } - - return $this->method; - } - - /** - * Get the original HTTP method (ignore override). - * - * Note: This method is not part of the PSR-7 standard. - * - * @return string - */ - public function getOriginalMethod() - { - return $this->originalMethod; - } - - /** - * Return an instance with the provided HTTP method. - * - * While HTTP method names are typically all uppercase characters, HTTP - * method names are case-sensitive and thus implementations SHOULD NOT - * modify the given string. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * changed request method. - * - * @param string $method Case-sensitive method. - * @return static - * @throws \InvalidArgumentException for invalid HTTP methods. - */ - public function withMethod($method) - { - $method = $this->filterMethod($method); - $clone = clone $this; - $clone->originalMethod = $method; - $clone->method = $method; - - return $clone; - } - - /** - * Validate the HTTP method - * - * @param null|string $method - * @return null|string - * @throws \InvalidArgumentException on invalid HTTP method. - */ - protected function filterMethod($method) - { - if ($method === null) { - return $method; - } - - if (!is_string($method)) { - throw new InvalidArgumentException(sprintf( - 'Unsupported HTTP method; must be a string, received %s', - (is_object($method) ? get_class($method) : gettype($method)) - )); - } - - $method = strtoupper($method); - if (preg_match("/^[!#$%&'*+.^_`|~0-9a-z-]+$/i", $method) !== 1) { - throw new InvalidMethodException($this, $method); - } - - return $method; - } - - /** - * Does this request use a given method? - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $method HTTP method - * @return bool - */ - public function isMethod($method) - { - return $this->getMethod() === $method; - } - - /** - * Is this a GET request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isGet() - { - return $this->isMethod('GET'); - } - - /** - * Is this a POST request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isPost() - { - return $this->isMethod('POST'); - } - - /** - * Is this a PUT request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isPut() - { - return $this->isMethod('PUT'); - } - - /** - * Is this a PATCH request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isPatch() - { - return $this->isMethod('PATCH'); - } - - /** - * Is this a DELETE request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isDelete() - { - return $this->isMethod('DELETE'); - } - - /** - * Is this a HEAD request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isHead() - { - return $this->isMethod('HEAD'); - } - - /** - * Is this a OPTIONS request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isOptions() - { - return $this->isMethod('OPTIONS'); - } - - /** - * Is this an XHR request? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isXhr() - { - return $this->getHeaderLine('X-Requested-With') === 'XMLHttpRequest'; - } - - /******************************************************************************* - * URI - ******************************************************************************/ - - /** - * Retrieves the message's request target. - * - * Retrieves the message's request-target either as it will appear (for - * clients), as it appeared at request (for servers), or as it was - * specified for the instance (see withRequestTarget()). - * - * In most cases, this will be the origin-form of the composed URI, - * unless a value was provided to the concrete implementation (see - * withRequestTarget() below). - * - * If no URI is available, and no request-target has been specifically - * provided, this method MUST return the string "/". - * - * @return string - */ - public function getRequestTarget() - { - if ($this->requestTarget) { - return $this->requestTarget; - } - - if ($this->uri === null) { - return '/'; - } - - $path = $this->uri->getPath(); - $path = '/' . ltrim($path, '/'); - - $query = $this->uri->getQuery(); - if ($query) { - $path .= '?' . $query; - } - $this->requestTarget = $path; - - return $this->requestTarget; - } - - /** - * Return an instance with the specific request-target. - * - * If the request needs a non-origin-form request-target — e.g., for - * specifying an absolute-form, authority-form, or asterisk-form — - * this method may be used to create an instance with the specified - * request-target, verbatim. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * changed request target. - * - * @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various - * request-target forms allowed in request messages) - * @param mixed $requestTarget - * @return static - * @throws InvalidArgumentException if the request target is invalid - */ - public function withRequestTarget($requestTarget) - { - if (preg_match('#\s#', $requestTarget)) { - throw new InvalidArgumentException( - 'Invalid request target provided; must be a string and cannot contain whitespace' - ); - } - $clone = clone $this; - $clone->requestTarget = $requestTarget; - - return $clone; - } - - /** - * Retrieves the URI instance. - * - * This method MUST return a UriInterface instance. - * - * @link http://tools.ietf.org/html/rfc3986#section-4.3 - * @return UriInterface Returns a UriInterface instance - * representing the URI of the request. - */ - public function getUri() - { - return $this->uri; - } - - /** - * Returns an instance with the provided URI. - * - * This method MUST update the Host header of the returned request by - * default if the URI contains a host component. If the URI does not - * contain a host component, any pre-existing Host header MUST be carried - * over to the returned request. - * - * You can opt-in to preserving the original state of the Host header by - * setting `$preserveHost` to `true`. When `$preserveHost` is set to - * `true`, this method interacts with the Host header in the following ways: - * - * - If the the Host header is missing or empty, and the new URI contains - * a host component, this method MUST update the Host header in the returned - * request. - * - If the Host header is missing or empty, and the new URI does not contain a - * host component, this method MUST NOT update the Host header in the returned - * request. - * - If a Host header is present and non-empty, this method MUST NOT update - * the Host header in the returned request. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * new UriInterface instance. - * - * @link http://tools.ietf.org/html/rfc3986#section-4.3 - * @param UriInterface $uri New request URI to use. - * @param bool $preserveHost Preserve the original state of the Host header. - * @return static - */ - public function withUri(UriInterface $uri, $preserveHost = false) - { - $clone = clone $this; - $clone->uri = $uri; - - if (!$preserveHost) { - if ($uri->getHost() !== '') { - $clone->headers->set('Host', $uri->getHost()); - } - } else { - if ($uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeaderLine('Host') === '')) { - $clone->headers->set('Host', $uri->getHost()); - } - } - - return $clone; - } - - /** - * Get request content type. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return string|null The request content type, if known - */ - public function getContentType() - { - $result = $this->getHeader('Content-Type'); - - return $result ? $result[0] : null; - } - - /** - * Get request media type, if known. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return string|null The request media type, minus content-type params - */ - public function getMediaType() - { - $contentType = $this->getContentType(); - if ($contentType) { - $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); - - return strtolower($contentTypeParts[0]); - } - - return null; - } - - /** - * Get request media type params, if known. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return array - */ - public function getMediaTypeParams() - { - $contentType = $this->getContentType(); - $contentTypeParams = []; - if ($contentType) { - $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); - $contentTypePartsLength = count($contentTypeParts); - for ($i = 1; $i < $contentTypePartsLength; $i++) { - $paramParts = explode('=', $contentTypeParts[$i]); - $contentTypeParams[strtolower($paramParts[0])] = $paramParts[1]; - } - } - - return $contentTypeParams; - } - - /** - * Get request content character set, if known. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return string|null - */ - public function getContentCharset() - { - $mediaTypeParams = $this->getMediaTypeParams(); - if (isset($mediaTypeParams['charset'])) { - return $mediaTypeParams['charset']; - } - - return null; - } - - /** - * Get request content length, if known. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return int|null - */ - public function getContentLength() - { - $result = $this->headers->get('Content-Length'); - - return $result ? (int)$result[0] : null; - } - - /******************************************************************************* - * Cookies - ******************************************************************************/ - - /** - * Retrieve cookies. - * - * Retrieves cookies sent by the client to the server. - * - * The data MUST be compatible with the structure of the $_COOKIE - * superglobal. - * - * @return array - */ - public function getCookieParams() - { - return $this->cookies; - } - - /** - * Fetch cookie value from cookies sent by the client to the server. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $key The attribute name. - * @param mixed $default Default value to return if the attribute does not exist. - * - * @return mixed - */ - public function getCookieParam($key, $default = null) - { - $cookies = $this->getCookieParams(); - $result = $default; - if (isset($cookies[$key])) { - $result = $cookies[$key]; - } - - return $result; - } - - /** - * Return an instance with the specified cookies. - * - * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST - * be compatible with the structure of $_COOKIE. Typically, this data will - * be injected at instantiation. - * - * This method MUST NOT update the related Cookie header of the request - * instance, nor related values in the server params. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated cookie values. - * - * @param array $cookies Array of key/value pairs representing cookies. - * @return static - */ - public function withCookieParams(array $cookies) - { - $clone = clone $this; - $clone->cookies = $cookies; - - return $clone; - } - - /******************************************************************************* - * Query Params - ******************************************************************************/ - - /** - * Retrieve query string arguments. - * - * Retrieves the deserialized query string arguments, if any. - * - * Note: the query params might not be in sync with the URI or server - * params. If you need to ensure you are only getting the original - * values, you may need to parse the query string from `getUri()->getQuery()` - * or from the `QUERY_STRING` server param. - * - * @return array - */ - public function getQueryParams() - { - if (is_array($this->queryParams)) { - return $this->queryParams; - } - - if ($this->uri === null) { - return []; - } - - parse_str($this->uri->getQuery(), $this->queryParams); // <-- URL decodes data - - return $this->queryParams; - } - - /** - * Return an instance with the specified query string arguments. - * - * These values SHOULD remain immutable over the course of the incoming - * request. They MAY be injected during instantiation, such as from PHP's - * $_GET superglobal, or MAY be derived from some other value such as the - * URI. In cases where the arguments are parsed from the URI, the data - * MUST be compatible with what PHP's parse_str() would return for - * purposes of how duplicate query parameters are handled, and how nested - * sets are handled. - * - * Setting query string arguments MUST NOT change the URI stored by the - * request, nor the values in the server params. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated query string arguments. - * - * @param array $query Array of query string arguments, typically from - * $_GET. - * @return static - */ - public function withQueryParams(array $query) - { - $clone = clone $this; - $clone->queryParams = $query; - - return $clone; - } - - /******************************************************************************* - * File Params - ******************************************************************************/ - - /** - * Retrieve normalized file upload data. - * - * This method returns upload metadata in a normalized tree, with each leaf - * an instance of Psr\Http\Message\UploadedFileInterface. - * - * These values MAY be prepared from $_FILES or the message body during - * instantiation, or MAY be injected via withUploadedFiles(). - * - * @return array An array tree of UploadedFileInterface instances; an empty - * array MUST be returned if no data is present. - */ - public function getUploadedFiles() - { - return $this->uploadedFiles; - } - - /** - * Create a new instance with the specified uploaded files. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated body parameters. - * - * @param array $uploadedFiles An array tree of UploadedFileInterface instances. - * @return static - * @throws \InvalidArgumentException if an invalid structure is provided. - */ - public function withUploadedFiles(array $uploadedFiles) - { - $clone = clone $this; - $clone->uploadedFiles = $uploadedFiles; - - return $clone; - } - - /******************************************************************************* - * Server Params - ******************************************************************************/ - - /** - * Retrieve server parameters. - * - * Retrieves data related to the incoming request environment, - * typically derived from PHP's $_SERVER superglobal. The data IS NOT - * REQUIRED to originate from $_SERVER. - * - * @return array - */ - public function getServerParams() - { - return $this->serverParams; - } - - /** - * Retrieve a server parameter. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $key - * @param mixed $default - * @return mixed - */ - public function getServerParam($key, $default = null) - { - $serverParams = $this->getServerParams(); - - return isset($serverParams[$key]) ? $serverParams[$key] : $default; - } - - /******************************************************************************* - * Attributes - ******************************************************************************/ - - /** - * Retrieve attributes derived from the request. - * - * The request "attributes" may be used to allow injection of any - * parameters derived from the request: e.g., the results of path - * match operations; the results of decrypting cookies; the results of - * deserializing non-form-encoded message bodies; etc. Attributes - * will be application and request specific, and CAN be mutable. - * - * @return array Attributes derived from the request. - */ - public function getAttributes() - { - return $this->attributes->all(); - } - - /** - * Retrieve a single derived request attribute. - * - * Retrieves a single derived request attribute as described in - * getAttributes(). If the attribute has not been previously set, returns - * the default value as provided. - * - * This method obviates the need for a hasAttribute() method, as it allows - * specifying a default value to return if the attribute is not found. - * - * @see getAttributes() - * @param string $name The attribute name. - * @param mixed $default Default value to return if the attribute does not exist. - * @return mixed - */ - public function getAttribute($name, $default = null) - { - return $this->attributes->get($name, $default); - } - - /** - * Return an instance with the specified derived request attribute. - * - * This method allows setting a single derived request attribute as - * described in getAttributes(). - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated attribute. - * - * @see getAttributes() - * @param string $name The attribute name. - * @param mixed $value The value of the attribute. - * @return static - */ - public function withAttribute($name, $value) - { - $clone = clone $this; - $clone->attributes->set($name, $value); - - return $clone; - } - - /** - * Create a new instance with the specified derived request attributes. - * - * Note: This method is not part of the PSR-7 standard. - * - * This method allows setting all new derived request attributes as - * described in getAttributes(). - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return a new instance that has the - * updated attributes. - * - * @param array $attributes New attributes - * @return static - */ - public function withAttributes(array $attributes) - { - $clone = clone $this; - $clone->attributes = new Collection($attributes); - - return $clone; - } - - /** - * Return an instance that removes the specified derived request attribute. - * - * This method allows removing a single derived request attribute as - * described in getAttributes(). - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that removes - * the attribute. - * - * @see getAttributes() - * @param string $name The attribute name. - * @return static - */ - public function withoutAttribute($name) - { - $clone = clone $this; - $clone->attributes->remove($name); - - return $clone; - } - - /******************************************************************************* - * Body - ******************************************************************************/ - - /** - * Retrieve any parameters provided in the request body. - * - * If the request Content-Type is either application/x-www-form-urlencoded - * or multipart/form-data, and the request method is POST, this method MUST - * return the contents of $_POST. - * - * Otherwise, this method may return any results of deserializing - * the request body content; as parsing returns structured content, the - * potential types MUST be arrays or objects only. A null value indicates - * the absence of body content. - * - * @return null|array|object The deserialized body parameters, if any. - * These will typically be an array or object. - * @throws RuntimeException if the request body media type parser returns an invalid value - */ - public function getParsedBody() - { - if ($this->bodyParsed !== false) { - return $this->bodyParsed; - } - - if (!$this->body) { - return null; - } - - $mediaType = $this->getMediaType(); - - // look for a media type with a structured syntax suffix (RFC 6839) - $parts = explode('+', $mediaType); - if (count($parts) >= 2) { - $mediaType = 'application/' . $parts[count($parts)-1]; - } - - if (isset($this->bodyParsers[$mediaType]) === true) { - $body = (string)$this->getBody(); - $parsed = $this->bodyParsers[$mediaType]($body); - - if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) { - throw new RuntimeException( - 'Request body media type parser return value must be an array, an object, or null' - ); - } - $this->bodyParsed = $parsed; - return $this->bodyParsed; - } - - return null; - } - - /** - * Return an instance with the specified body parameters. - * - * These MAY be injected during instantiation. - * - * If the request Content-Type is either application/x-www-form-urlencoded - * or multipart/form-data, and the request method is POST, use this method - * ONLY to inject the contents of $_POST. - * - * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of - * deserializing the request body content. Deserialization/parsing returns - * structured data, and, as such, this method ONLY accepts arrays or objects, - * or a null value if nothing was available to parse. - * - * As an example, if content negotiation determines that the request data - * is a JSON payload, this method could be used to create a request - * instance with the deserialized parameters. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated body parameters. - * - * @param null|array|object $data The deserialized body data. This will - * typically be in an array or object. - * @return static - * @throws \InvalidArgumentException if an unsupported argument type is - * provided. - */ - public function withParsedBody($data) - { - if (!is_null($data) && !is_object($data) && !is_array($data)) { - throw new InvalidArgumentException('Parsed body value must be an array, an object, or null'); - } - - $clone = clone $this; - $clone->bodyParsed = $data; - - return $clone; - } - - /** - * Force Body to be parsed again. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return $this - */ - public function reparseBody() - { - $this->bodyParsed = false; - - return $this; - } - - /** - * Register media type parser. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $mediaType A HTTP media type (excluding content-type - * params). - * @param callable $callable A callable that returns parsed contents for - * media type. - */ - public function registerMediaTypeParser($mediaType, callable $callable) - { - if ($callable instanceof Closure) { - $callable = $callable->bindTo($this); - } - $this->bodyParsers[(string)$mediaType] = $callable; - } - - /******************************************************************************* - * Parameters (e.g., POST and GET data) - ******************************************************************************/ - - /** - * Fetch request parameter value from body or query string (in that order). - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $key The parameter key. - * @param string $default The default value. - * - * @return mixed The parameter value. - */ - public function getParam($key, $default = null) - { - $postParams = $this->getParsedBody(); - $getParams = $this->getQueryParams(); - $result = $default; - if (is_array($postParams) && isset($postParams[$key])) { - $result = $postParams[$key]; - } elseif (is_object($postParams) && property_exists($postParams, $key)) { - $result = $postParams->$key; - } elseif (isset($getParams[$key])) { - $result = $getParams[$key]; - } - - return $result; - } - - /** - * Fetch parameter value from request body. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $key - * @param mixed $default - * - * @return mixed - */ - public function getParsedBodyParam($key, $default = null) - { - $postParams = $this->getParsedBody(); - $result = $default; - if (is_array($postParams) && isset($postParams[$key])) { - $result = $postParams[$key]; - } elseif (is_object($postParams) && property_exists($postParams, $key)) { - $result = $postParams->$key; - } - - return $result; - } - - /** - * Fetch parameter value from query string. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param string $key - * @param mixed $default - * - * @return mixed - */ - public function getQueryParam($key, $default = null) - { - $getParams = $this->getQueryParams(); - $result = $default; - if (isset($getParams[$key])) { - $result = $getParams[$key]; - } - - return $result; - } - - /** - * Fetch associative array of body and query string parameters. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return array - */ - public function getParams() - { - $params = $this->getQueryParams(); - $postParams = $this->getParsedBody(); - if ($postParams) { - $params = array_merge($params, (array)$postParams); - } - - return $params; - } -} diff --git a/Slim/Http/RequestBody.php b/Slim/Http/RequestBody.php deleted file mode 100644 index 50887fddc..000000000 --- a/Slim/Http/RequestBody.php +++ /dev/null @@ -1,27 +0,0 @@ - 'Continue', - 101 => 'Switching Protocols', - 102 => 'Processing', - //Successful 2xx - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - 207 => 'Multi-Status', - 208 => 'Already Reported', - 226 => 'IM Used', - //Redirection 3xx - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Found', - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 306 => '(Unused)', - 307 => 'Temporary Redirect', - 308 => 'Permanent Redirect', - //Client Error 4xx - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - 418 => 'I\'m a teapot', - 421 => 'Misdirected Request', - 422 => 'Unprocessable Entity', - 423 => 'Locked', - 424 => 'Failed Dependency', - 426 => 'Upgrade Required', - 428 => 'Precondition Required', - 429 => 'Too Many Requests', - 431 => 'Request Header Fields Too Large', - 444 => 'Connection Closed Without Response', - 451 => 'Unavailable For Legal Reasons', - 499 => 'Client Closed Request', - //Server Error 5xx - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported', - 506 => 'Variant Also Negotiates', - 507 => 'Insufficient Storage', - 508 => 'Loop Detected', - 510 => 'Not Extended', - 511 => 'Network Authentication Required', - 599 => 'Network Connect Timeout Error', - ]; - - /** - * EOL characters used for HTTP response. - * - * @var string - */ - const EOL = "\r\n"; - - /** - * Create new HTTP response. - * - * @param int $status The response status code. - * @param HeadersInterface|null $headers The response headers. - * @param StreamInterface|null $body The response body. - */ - public function __construct($status = 200, HeadersInterface $headers = null, StreamInterface $body = null) - { - $this->status = $this->filterStatus($status); - $this->headers = $headers ? $headers : new Headers(); - $this->body = $body ? $body : new Body(fopen('php://temp', 'r+')); - } - - /** - * This method is applied to the cloned object - * after PHP performs an initial shallow-copy. This - * method completes a deep-copy by creating new objects - * for the cloned object's internal reference pointers. - */ - public function __clone() - { - $this->headers = clone $this->headers; - } - - /******************************************************************************* - * Status - ******************************************************************************/ - - /** - * Gets the response status code. - * - * The status code is a 3-digit integer result code of the server's attempt - * to understand and satisfy the request. - * - * @return int Status code. - */ - public function getStatusCode() - { - return $this->status; - } - - /** - * Return an instance with the specified status code and, optionally, reason phrase. - * - * If no reason phrase is specified, implementations MAY choose to default - * to the RFC 7231 or IANA recommended reason phrase for the response's - * status code. - * - * This method MUST be implemented in such a way as to retain the - * immutability of the message, and MUST return an instance that has the - * updated status and reason phrase. - * - * @link http://tools.ietf.org/html/rfc7231#section-6 - * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml - * @param int $code The 3-digit integer result code to set. - * @param string $reasonPhrase The reason phrase to use with the - * provided status code; if none is provided, implementations MAY - * use the defaults as suggested in the HTTP specification. - * @return static - * @throws \InvalidArgumentException For invalid status code arguments. - */ - public function withStatus($code, $reasonPhrase = '') - { - $code = $this->filterStatus($code); - - if (!is_string($reasonPhrase) && !method_exists($reasonPhrase, '__toString')) { - throw new InvalidArgumentException('ReasonPhrase must be a string'); - } - - $clone = clone $this; - $clone->status = $code; - if ($reasonPhrase === '' && isset(static::$messages[$code])) { - $reasonPhrase = static::$messages[$code]; - } - - if ($reasonPhrase === '') { - throw new InvalidArgumentException('ReasonPhrase must be supplied for this code'); - } - - $clone->reasonPhrase = $reasonPhrase; - - return $clone; - } - - /** - * Filter HTTP status code. - * - * @param int $status HTTP status code. - * @return int - * @throws \InvalidArgumentException If an invalid HTTP status code is provided. - */ - protected function filterStatus($status) - { - if (!is_integer($status) || $status<100 || $status>599) { - throw new InvalidArgumentException('Invalid HTTP status code'); - } - - return $status; - } - - /** - * Gets the response reason phrase associated with the status code. - * - * Because a reason phrase is not a required element in a response - * status line, the reason phrase value MAY be null. Implementations MAY - * choose to return the default RFC 7231 recommended reason phrase (or those - * listed in the IANA HTTP Status Code Registry) for the response's - * status code. - * - * @link http://tools.ietf.org/html/rfc7231#section-6 - * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml - * @return string Reason phrase; must return an empty string if none present. - */ - public function getReasonPhrase() - { - if ($this->reasonPhrase) { - return $this->reasonPhrase; - } - if (isset(static::$messages[$this->status])) { - return static::$messages[$this->status]; - } - return ''; - } - - /******************************************************************************* - * Body - ******************************************************************************/ - - /** - * Write data to the response body. - * - * Note: This method is not part of the PSR-7 standard. - * - * Proxies to the underlying stream and writes the provided data to it. - * - * @param string $data - * @return $this - */ - public function write($data) - { - $this->getBody()->write($data); - - return $this; - } - - /******************************************************************************* - * Response Helpers - ******************************************************************************/ - - /** - * Redirect. - * - * Note: This method is not part of the PSR-7 standard. - * - * This method prepares the response object to return an HTTP Redirect - * response to the client. - * - * @param string|UriInterface $url The redirect destination. - * @param int|null $status The redirect HTTP status code. - * @return static - */ - public function withRedirect($url, $status = null) - { - $responseWithRedirect = $this->withHeader('Location', (string)$url); - - if (is_null($status) && $this->getStatusCode() === 200) { - $status = 302; - } - - if (!is_null($status)) { - return $responseWithRedirect->withStatus($status); - } - - return $responseWithRedirect; - } - - /** - * Json. - * - * Note: This method is not part of the PSR-7 standard. - * - * This method prepares the response object to return an HTTP Json - * response to the client. - * - * @param mixed $data The data - * @param int $status The HTTP status code. - * @param int $encodingOptions Json encoding options - * @throws \RuntimeException - * @return static - */ - public function withJson($data, $status = null, $encodingOptions = 0) - { - $response = $this->withBody(new Body(fopen('php://temp', 'r+'))); - $response->body->write($json = json_encode($data, $encodingOptions)); - - // Ensure that the json encoding passed successfully - if ($json === false) { - throw new \RuntimeException(json_last_error_msg(), json_last_error()); - } - - $responseWithJson = $response->withHeader('Content-Type', 'application/json;charset=utf-8'); - if (isset($status)) { - return $responseWithJson->withStatus($status); - } - return $responseWithJson; - } - - /** - * Is this response empty? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isEmpty() - { - return in_array($this->getStatusCode(), [204, 205, 304]); - } - - /** - * Is this response informational? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isInformational() - { - return $this->getStatusCode() >= 100 && $this->getStatusCode() < 200; - } - - /** - * Is this response OK? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isOk() - { - return $this->getStatusCode() === 200; - } - - /** - * Is this response successful? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isSuccessful() - { - return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300; - } - - /** - * Is this response a redirect? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isRedirect() - { - return in_array($this->getStatusCode(), [301, 302, 303, 307]); - } - - /** - * Is this response a redirection? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isRedirection() - { - return $this->getStatusCode() >= 300 && $this->getStatusCode() < 400; - } - - /** - * Is this response forbidden? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - * @api - */ - public function isForbidden() - { - return $this->getStatusCode() === 403; - } - - /** - * Is this response not Found? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isNotFound() - { - return $this->getStatusCode() === 404; - } - - /** - * Is this response a client error? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isClientError() - { - return $this->getStatusCode() >= 400 && $this->getStatusCode() < 500; - } - - /** - * Is this response a server error? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - public function isServerError() - { - return $this->getStatusCode() >= 500 && $this->getStatusCode() < 600; - } - - /** - * Convert response to string. - * - * Note: This method is not part of the PSR-7 standard. - * - * @return string - */ - public function __toString() - { - $output = sprintf( - 'HTTP/%s %s %s', - $this->getProtocolVersion(), - $this->getStatusCode(), - $this->getReasonPhrase() - ); - $output .= Response::EOL; - foreach ($this->getHeaders() as $name => $values) { - $output .= sprintf('%s: %s', $name, $this->getHeaderLine($name)) . Response::EOL; - } - $output .= Response::EOL; - $output .= (string)$this->getBody(); - - return $output; - } -} diff --git a/Slim/Http/Stream.php b/Slim/Http/Stream.php deleted file mode 100644 index 27c7a7645..000000000 --- a/Slim/Http/Stream.php +++ /dev/null @@ -1,450 +0,0 @@ - ['r', 'r+', 'w+', 'a+', 'x+', 'c+'], - 'writable' => ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'], - ]; - - /** - * The underlying stream resource - * - * @var resource - */ - protected $stream; - - /** - * Stream metadata - * - * @var array - */ - protected $meta; - - /** - * Is this stream readable? - * - * @var bool - */ - protected $readable; - - /** - * Is this stream writable? - * - * @var bool - */ - protected $writable; - - /** - * Is this stream seekable? - * - * @var bool - */ - protected $seekable; - - /** - * The size of the stream if known - * - * @var null|int - */ - protected $size; - - /** - * Is this stream a pipe? - * - * @var bool - */ - protected $isPipe; - - /** - * Create a new Stream. - * - * @param resource $stream A PHP resource handle. - * - * @throws InvalidArgumentException If argument is not a resource. - */ - public function __construct($stream) - { - $this->attach($stream); - } - - /** - * Get stream metadata as an associative array or retrieve a specific key. - * - * The keys returned are identical to the keys returned from PHP's - * stream_get_meta_data() function. - * - * @link http://php.net/manual/en/function.stream-get-meta-data.php - * - * @param string $key Specific metadata to retrieve. - * - * @return array|mixed|null Returns an associative array if no key is - * provided. Returns a specific key value if a key is provided and the - * value is found, or null if the key is not found. - */ - public function getMetadata($key = null) - { - $this->meta = stream_get_meta_data($this->stream); - if (is_null($key) === true) { - return $this->meta; - } - - return isset($this->meta[$key]) ? $this->meta[$key] : null; - } - - /** - * Is a resource attached to this stream? - * - * Note: This method is not part of the PSR-7 standard. - * - * @return bool - */ - protected function isAttached() - { - return is_resource($this->stream); - } - - /** - * Attach new resource to this object. - * - * Note: This method is not part of the PSR-7 standard. - * - * @param resource $newStream A PHP resource handle. - * - * @throws InvalidArgumentException If argument is not a valid PHP resource. - */ - protected function attach($newStream) - { - if (is_resource($newStream) === false) { - throw new InvalidArgumentException(__METHOD__ . ' argument must be a valid PHP resource'); - } - - if ($this->isAttached() === true) { - $this->detach(); - } - - $this->stream = $newStream; - } - - /** - * Separates any underlying resources from the stream. - * - * After the stream has been detached, the stream is in an unusable state. - * - * @return resource|null Underlying PHP stream, if any - */ - public function detach() - { - $oldResource = $this->stream; - $this->stream = null; - $this->meta = null; - $this->readable = null; - $this->writable = null; - $this->seekable = null; - $this->size = null; - $this->isPipe = null; - - return $oldResource; - } - - /** - * Reads all data from the stream into a string, from the beginning to end. - * - * This method MUST attempt to seek to the beginning of the stream before - * reading data and read the stream until the end is reached. - * - * Warning: This could attempt to load a large amount of data into memory. - * - * This method MUST NOT raise an exception in order to conform with PHP's - * string casting operations. - * - * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring - * @return string - */ - public function __toString() - { - if (!$this->isAttached()) { - return ''; - } - - try { - $this->rewind(); - return $this->getContents(); - } catch (RuntimeException $e) { - return ''; - } - } - - /** - * Closes the stream and any underlying resources. - */ - public function close() - { - if ($this->isAttached() === true) { - if ($this->isPipe()) { - pclose($this->stream); - } else { - fclose($this->stream); - } - } - - $this->detach(); - } - - /** - * Get the size of the stream if known. - * - * @return int|null Returns the size in bytes if known, or null if unknown. - */ - public function getSize() - { - if (!$this->size && $this->isAttached() === true) { - $stats = fstat($this->stream); - $this->size = isset($stats['size']) && !$this->isPipe() ? $stats['size'] : null; - } - - return $this->size; - } - - /** - * Returns the current position of the file read/write pointer - * - * @return int Position of the file pointer - * - * @throws RuntimeException on error. - */ - public function tell() - { - if (!$this->isAttached() || ($position = ftell($this->stream)) === false || $this->isPipe()) { - throw new RuntimeException('Could not get the position of the pointer in stream'); - } - - return $position; - } - - /** - * Returns true if the stream is at the end of the stream. - * - * @return bool - */ - public function eof() - { - return $this->isAttached() ? feof($this->stream) : true; - } - - /** - * Returns whether or not the stream is readable. - * - * @return bool - */ - public function isReadable() - { - if ($this->readable === null) { - if ($this->isPipe()) { - $this->readable = true; - } else { - $this->readable = false; - if ($this->isAttached()) { - $meta = $this->getMetadata(); - foreach (self::$modes['readable'] as $mode) { - if (strpos($meta['mode'], $mode) === 0) { - $this->readable = true; - break; - } - } - } - } - } - - return $this->readable; - } - - /** - * Returns whether or not the stream is writable. - * - * @return bool - */ - public function isWritable() - { - if ($this->writable === null) { - $this->writable = false; - if ($this->isAttached()) { - $meta = $this->getMetadata(); - foreach (self::$modes['writable'] as $mode) { - if (strpos($meta['mode'], $mode) === 0) { - $this->writable = true; - break; - } - } - } - } - - return $this->writable; - } - - /** - * Returns whether or not the stream is seekable. - * - * @return bool - */ - public function isSeekable() - { - if ($this->seekable === null) { - $this->seekable = false; - if ($this->isAttached()) { - $meta = $this->getMetadata(); - $this->seekable = !$this->isPipe() && $meta['seekable']; - } - } - - return $this->seekable; - } - - /** - * Seek to a position in the stream. - * - * @link http://www.php.net/manual/en/function.fseek.php - * - * @param int $offset Stream offset - * @param int $whence Specifies how the cursor position will be calculated - * based on the seek offset. Valid values are identical to the built-in - * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to - * offset bytes SEEK_CUR: Set position to current location plus offset - * SEEK_END: Set position to end-of-stream plus offset. - * - * @throws RuntimeException on failure. - */ - public function seek($offset, $whence = SEEK_SET) - { - // Note that fseek returns 0 on success! - if (!$this->isSeekable() || fseek($this->stream, $offset, $whence) === -1) { - throw new RuntimeException('Could not seek in stream'); - } - } - - /** - * Seek to the beginning of the stream. - * - * If the stream is not seekable, this method will raise an exception; - * otherwise, it will perform a seek(0). - * - * @see seek() - * - * @link http://www.php.net/manual/en/function.fseek.php - * - * @throws RuntimeException on failure. - */ - public function rewind() - { - if (!$this->isSeekable() || rewind($this->stream) === false) { - throw new RuntimeException('Could not rewind stream'); - } - } - - /** - * Read data from the stream. - * - * @param int $length Read up to $length bytes from the object and return - * them. Fewer than $length bytes may be returned if underlying stream - * call returns fewer bytes. - * - * @return string Returns the data read from the stream, or an empty string - * if no bytes are available. - * - * @throws RuntimeException if an error occurs. - */ - public function read($length) - { - if (!$this->isReadable() || ($data = fread($this->stream, $length)) === false) { - throw new RuntimeException('Could not read from stream'); - } - - return $data; - } - - /** - * Write data to the stream. - * - * @param string $string The string that is to be written. - * - * @return int Returns the number of bytes written to the stream. - * - * @throws RuntimeException on failure. - */ - public function write($string) - { - if (!$this->isWritable() || ($written = fwrite($this->stream, $string)) === false) { - throw new RuntimeException('Could not write to stream'); - } - - // reset size so that it will be recalculated on next call to getSize() - $this->size = null; - - return $written; - } - - /** - * Returns the remaining contents in a string - * - * @return string - * - * @throws RuntimeException if unable to read or an error occurs while - * reading. - */ - public function getContents() - { - if (!$this->isReadable() || ($contents = stream_get_contents($this->stream)) === false) { - throw new RuntimeException('Could not get contents of stream'); - } - - return $contents; - } - - /** - * Returns whether or not the stream is a pipe. - * - * @return bool - */ - public function isPipe() - { - if ($this->isPipe === null) { - $this->isPipe = false; - if ($this->isAttached()) { - $mode = fstat($this->stream)['mode']; - $this->isPipe = ($mode & self::FSTAT_MODE_S_IFIFO) !== 0; - } - } - - return $this->isPipe; - } -} diff --git a/Slim/Http/UploadedFile.php b/Slim/Http/UploadedFile.php deleted file mode 100644 index ae5dfb65e..000000000 --- a/Slim/Http/UploadedFile.php +++ /dev/null @@ -1,327 +0,0 @@ -has('slim.files')) { - return $env['slim.files']; - } elseif (isset($_FILES)) { - return static::parseUploadedFiles($_FILES); - } - - return []; - } - - /** - * Parse a non-normalized, i.e. $_FILES superglobal, tree of uploaded file data. - * - * @param array $uploadedFiles The non-normalized tree of uploaded file data. - * - * @return array A normalized tree of UploadedFile instances. - */ - private static function parseUploadedFiles(array $uploadedFiles) - { - $parsed = []; - foreach ($uploadedFiles as $field => $uploadedFile) { - if (!isset($uploadedFile['error'])) { - if (is_array($uploadedFile)) { - $parsed[$field] = static::parseUploadedFiles($uploadedFile); - } - continue; - } - - $parsed[$field] = []; - if (!is_array($uploadedFile['error'])) { - $parsed[$field] = new static( - $uploadedFile['tmp_name'], - isset($uploadedFile['name']) ? $uploadedFile['name'] : null, - isset($uploadedFile['type']) ? $uploadedFile['type'] : null, - isset($uploadedFile['size']) ? $uploadedFile['size'] : null, - $uploadedFile['error'], - true - ); - } else { - $subArray = []; - foreach ($uploadedFile['error'] as $fileIdx => $error) { - // normalise subarray and re-parse to move the input's keyname up a level - $subArray[$fileIdx]['name'] = $uploadedFile['name'][$fileIdx]; - $subArray[$fileIdx]['type'] = $uploadedFile['type'][$fileIdx]; - $subArray[$fileIdx]['tmp_name'] = $uploadedFile['tmp_name'][$fileIdx]; - $subArray[$fileIdx]['error'] = $uploadedFile['error'][$fileIdx]; - $subArray[$fileIdx]['size'] = $uploadedFile['size'][$fileIdx]; - - $parsed[$field] = static::parseUploadedFiles($subArray); - } - } - } - - return $parsed; - } - - /** - * Construct a new UploadedFile instance. - * - * @param string $file The full path to the uploaded file provided by the client. - * @param string|null $name The file name. - * @param string|null $type The file media type. - * @param int|null $size The file size in bytes. - * @param int $error The UPLOAD_ERR_XXX code representing the status of the upload. - * @param bool $sapi Indicates if the upload is in a SAPI environment. - */ - public function __construct($file, $name = null, $type = null, $size = null, $error = UPLOAD_ERR_OK, $sapi = false) - { - $this->file = $file; - $this->name = $name; - $this->type = $type; - $this->size = $size; - $this->error = $error; - $this->sapi = $sapi; - } - - /** - * Retrieve a stream representing the uploaded file. - * - * This method MUST return a StreamInterface instance, representing the - * uploaded file. The purpose of this method is to allow utilizing native PHP - * stream functionality to manipulate the file upload, such as - * stream_copy_to_stream() (though the result will need to be decorated in a - * native PHP stream wrapper to work with such functions). - * - * If the moveTo() method has been called previously, this method MUST raise - * an exception. - * - * @return StreamInterface Stream representation of the uploaded file. - * @throws \RuntimeException in cases when no stream is available or can be - * created. - */ - public function getStream() - { - if ($this->moved) { - throw new \RuntimeException(sprintf('Uploaded file %1s has already been moved', $this->name)); - } - if ($this->stream === null) { - $this->stream = new Stream(fopen($this->file, 'r')); - } - - return $this->stream; - } - - /** - * Move the uploaded file to a new location. - * - * Use this method as an alternative to move_uploaded_file(). This method is - * guaranteed to work in both SAPI and non-SAPI environments. - * Implementations must determine which environment they are in, and use the - * appropriate method (move_uploaded_file(), rename(), or a stream - * operation) to perform the operation. - * - * $targetPath may be an absolute path, or a relative path. If it is a - * relative path, resolution should be the same as used by PHP's rename() - * function. - * - * The original file or stream MUST be removed on completion. - * - * If this method is called more than once, any subsequent calls MUST raise - * an exception. - * - * When used in an SAPI environment where $_FILES is populated, when writing - * files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be - * used to ensure permissions and upload status are verified correctly. - * - * If you wish to move to a stream, use getStream(), as SAPI operations - * cannot guarantee writing to stream destinations. - * - * @see http://php.net/is_uploaded_file - * @see http://php.net/move_uploaded_file - * - * @param string $targetPath Path to which to move the uploaded file. - * - * @throws InvalidArgumentException if the $path specified is invalid. - * @throws RuntimeException on any error during the move operation, or on - * the second or subsequent call to the method. - */ - public function moveTo($targetPath) - { - if ($this->moved) { - throw new RuntimeException('Uploaded file already moved'); - } - - $targetIsStream = strpos($targetPath, '://') > 0; - if (!$targetIsStream && !is_writable(dirname($targetPath))) { - throw new InvalidArgumentException('Upload target path is not writable'); - } - - if ($targetIsStream) { - if (!copy($this->file, $targetPath)) { - throw new RuntimeException(sprintf('Error moving uploaded file %1s to %2s', $this->name, $targetPath)); - } - if (!unlink($this->file)) { - throw new RuntimeException(sprintf('Error removing uploaded file %1s', $this->name)); - } - } elseif ($this->sapi) { - if (!is_uploaded_file($this->file)) { - throw new RuntimeException(sprintf('%1s is not a valid uploaded file', $this->file)); - } - - if (!move_uploaded_file($this->file, $targetPath)) { - throw new RuntimeException(sprintf('Error moving uploaded file %1s to %2s', $this->name, $targetPath)); - } - } else { - if (!rename($this->file, $targetPath)) { - throw new RuntimeException(sprintf('Error moving uploaded file %1s to %2s', $this->name, $targetPath)); - } - } - - $this->moved = true; - } - - /** - * Retrieve the error associated with the uploaded file. - * - * The return value MUST be one of PHP's UPLOAD_ERR_XXX constants. - * - * If the file was uploaded successfully, this method MUST return - * UPLOAD_ERR_OK. - * - * Implementations SHOULD return the value stored in the "error" key of - * the file in the $_FILES array. - * - * @see http://php.net/manual/en/features.file-upload.errors.php - * - * @return int One of PHP's UPLOAD_ERR_XXX constants. - */ - public function getError() - { - return $this->error; - } - - /** - * Retrieve the filename sent by the client. - * - * Do not trust the value returned by this method. A client could send - * a malicious filename with the intention to corrupt or hack your - * application. - * - * Implementations SHOULD return the value stored in the "name" key of - * the file in the $_FILES array. - * - * @return string|null The filename sent by the client or null if none - * was provided. - */ - public function getClientFilename() - { - return $this->name; - } - - /** - * Retrieve the media type sent by the client. - * - * Do not trust the value returned by this method. A client could send - * a malicious media type with the intention to corrupt or hack your - * application. - * - * Implementations SHOULD return the value stored in the "type" key of - * the file in the $_FILES array. - * - * @return string|null The media type sent by the client or null if none - * was provided. - */ - public function getClientMediaType() - { - return $this->type; - } - - /** - * Retrieve the file size. - * - * Implementations SHOULD return the value stored in the "size" key of - * the file in the $_FILES array if available, as PHP calculates this based - * on the actual size transmitted. - * - * @return int|null The file size in bytes or null if unknown. - */ - public function getSize() - { - return $this->size; - } -} diff --git a/Slim/Http/Uri.php b/Slim/Http/Uri.php deleted file mode 100644 index fb51a49b9..000000000 --- a/Slim/Http/Uri.php +++ /dev/null @@ -1,762 +0,0 @@ -scheme = $this->filterScheme($scheme); - $this->host = $host; - $this->port = $this->filterPort($port); - $this->path = empty($path) ? '/' : $this->filterPath($path); - $this->query = $this->filterQuery($query); - $this->fragment = $this->filterQuery($fragment); - $this->user = $user; - $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 environment. - * - * @param Environment $env - * - * @return self - */ - public static function createFromEnvironment(Environment $env) - { - // 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); - } - } - - // Path - $requestScriptName = parse_url($env->get('SCRIPT_NAME'), PHP_URL_PATH); - $requestScriptDir = dirname($requestScriptName); - - // 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); - $requestUri = rawurldecode($requestUri); - - $basePath = ''; - $virtualPath = $requestUri; - if (stripos($requestUri, $requestScriptName) === 0) { - $basePath = $requestScriptName; - } elseif ($requestScriptDir !== '/' && stripos($requestUri, $requestScriptDir) === 0) { - $basePath = $requestScriptDir; - } - - if ($basePath) { - $virtualPath = ltrim(substr($requestUri, strlen($basePath)), '/'); - } - - // Query string - $queryString = $env->get('QUERY_STRING', ''); - if ($queryString === '') { - $queryString = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_QUERY); - } - - // Fragment - $fragment = ''; - - // Build Uri - $uri = new static($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password); - - return $uri; - } - - /******************************************************************************** - * Scheme - *******************************************************************************/ - - /** - * Retrieve the scheme component of the URI. - * - * If no scheme is present, this method MUST return an empty string. - * - * The value returned MUST be normalized to lowercase, per RFC 3986 - * Section 3.1. - * - * The trailing ":" character is not part of the scheme and MUST NOT be - * added. - * - * @see https://tools.ietf.org/html/rfc3986#section-3.1 - * @return string The URI scheme. - */ - public function getScheme() - { - return $this->scheme; - } - - /** - * Return an instance with the specified scheme. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified scheme. - * - * Implementations MUST support the schemes "http" and "https" case - * insensitively, and MAY accommodate other schemes if required. - * - * An empty scheme is equivalent to removing the scheme. - * - * @param string $scheme The scheme to use with the new instance. - * @return self A new instance with the specified scheme. - * @throws \InvalidArgumentException for invalid or unsupported schemes. - */ - public function withScheme($scheme) - { - $scheme = $this->filterScheme($scheme); - $clone = clone $this; - $clone->scheme = $scheme; - - return $clone; - } - - /** - * Filter Uri scheme. - * - * @param string $scheme Raw Uri scheme. - * @return string - * - * @throws InvalidArgumentException If the Uri scheme is not a string. - * @throws InvalidArgumentException If Uri scheme is not "", "https", or "http". - */ - protected function filterScheme($scheme) - { - static $valid = [ - '' => true, - 'https' => true, - 'http' => true, - ]; - - if (!is_string($scheme) && !method_exists($scheme, '__toString')) { - throw new InvalidArgumentException('Uri scheme must be a string'); - } - - $scheme = str_replace('://', '', strtolower((string)$scheme)); - if (!isset($valid[$scheme])) { - throw new InvalidArgumentException('Uri scheme must be one of: "", "https", "http"'); - } - - return $scheme; - } - - /******************************************************************************** - * Authority - *******************************************************************************/ - - /** - * Retrieve the authority component of the URI. - * - * If no authority information is present, this method MUST return an empty - * string. - * - * The authority syntax of the URI is: - * - *
-     * [user-info@]host[:port]
-     * 
- * - * If the port component is not set or is the standard port for the current - * scheme, it SHOULD NOT be included. - * - * @see https://tools.ietf.org/html/rfc3986#section-3.2 - * @return string The URI authority, in "[user-info@]host[:port]" format. - */ - public function getAuthority() - { - $userInfo = $this->getUserInfo(); - $host = $this->getHost(); - $port = $this->getPort(); - - return ($userInfo ? $userInfo . '@' : '') . $host . ($port !== null ? ':' . $port : ''); - } - - /** - * Retrieve the user information component of the URI. - * - * If no user information is present, this method MUST return an empty - * string. - * - * If a user is present in the URI, this will return that value; - * additionally, if the password is also present, it will be appended to the - * user value, with a colon (":") separating the values. - * - * The trailing "@" character is not part of the user information and MUST - * NOT be added. - * - * @return string The URI user information, in "username[:password]" format. - */ - public function getUserInfo() - { - return $this->user . ($this->password ? ':' . $this->password : ''); - } - - /** - * Return an instance with the specified user information. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified user information. - * - * Password is optional, but the user information MUST include the - * user; an empty string for the user is equivalent to removing user - * information. - * - * @param string $user The user name to use for authority. - * @param null|string $password The password associated with $user. - * @return self A new instance with the specified user information. - */ - public function withUserInfo($user, $password = null) - { - $clone = clone $this; - $clone->user = $user; - $clone->password = $password ? $password : ''; - - return $clone; - } - - /** - * Retrieve the host component of the URI. - * - * If no host is present, this method MUST return an empty string. - * - * The value returned MUST be normalized to lowercase, per RFC 3986 - * Section 3.2.2. - * - * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 - * @return string The URI host. - */ - public function getHost() - { - return $this->host; - } - - /** - * Return an instance with the specified host. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified host. - * - * An empty host value is equivalent to removing the host. - * - * @param string $host The hostname to use with the new instance. - * @return self A new instance with the specified host. - * @throws \InvalidArgumentException for invalid hostnames. - */ - public function withHost($host) - { - $clone = clone $this; - $clone->host = $host; - - return $clone; - } - - /** - * Retrieve the port component of the URI. - * - * If a port is present, and it is non-standard for the current scheme, - * this method MUST return it as an integer. If the port is the standard port - * used with the current scheme, this method SHOULD return null. - * - * If no port is present, and no scheme is present, this method MUST return - * a null value. - * - * If no port is present, but a scheme is present, this method MAY return - * the standard port for that scheme, but SHOULD return null. - * - * @return null|int The URI port. - */ - public function getPort() - { - return $this->port && !$this->hasStandardPort() ? $this->port : null; - } - - /** - * Return an instance with the specified port. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified port. - * - * Implementations MUST raise an exception for ports outside the - * established TCP and UDP port ranges. - * - * A null value provided for the port is equivalent to removing the port - * information. - * - * @param null|int $port The port to use with the new instance; a null value - * removes the port information. - * @return self A new instance with the specified port. - * @throws \InvalidArgumentException for invalid ports. - */ - public function withPort($port) - { - $port = $this->filterPort($port); - $clone = clone $this; - $clone->port = $port; - - return $clone; - } - - /** - * Does this Uri use a standard port? - * - * @return bool - */ - protected function hasStandardPort() - { - return ($this->scheme === 'http' && $this->port === 80) || ($this->scheme === 'https' && $this->port === 443); - } - - /** - * Filter Uri port. - * - * @param null|int $port The Uri port number. - * @return null|int - * - * @throws InvalidArgumentException If the port is invalid. - */ - protected function filterPort($port) - { - if (is_null($port) || (is_integer($port) && ($port >= 1 && $port <= 65535))) { - return $port; - } - - throw new InvalidArgumentException('Uri port must be null or an integer between 1 and 65535 (inclusive)'); - } - - /******************************************************************************** - * Path - *******************************************************************************/ - - /** - * Retrieve the path component of the URI. - * - * The path can either be empty or absolute (starting with a slash) or - * rootless (not starting with a slash). Implementations MUST support all - * three syntaxes. - * - * Normally, the empty path "" and absolute path "/" are considered equal as - * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically - * do this normalization because in contexts with a trimmed base path, e.g. - * the front controller, this difference becomes significant. It's the task - * of the user to handle both "" and "/". - * - * The value returned MUST be percent-encoded, but MUST NOT double-encode - * any characters. To determine what characters to encode, please refer to - * RFC 3986, Sections 2 and 3.3. - * - * As an example, if the value should include a slash ("/") not intended as - * delimiter between path segments, that value MUST be passed in encoded - * form (e.g., "%2F") to the instance. - * - * @see https://tools.ietf.org/html/rfc3986#section-2 - * @see https://tools.ietf.org/html/rfc3986#section-3.3 - * @return string The URI path. - */ - public function getPath() - { - return $this->path; - } - - /** - * Return an instance with the specified path. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified path. - * - * The path can either be empty or absolute (starting with a slash) or - * rootless (not starting with a slash). Implementations MUST support all - * three syntaxes. - * - * If the path is intended to be domain-relative rather than path relative then - * it must begin with a slash ("/"). Paths not starting with a slash ("/") - * are assumed to be relative to some base path known to the application or - * consumer. - * - * Users can provide both encoded and decoded path characters. - * Implementations ensure the correct encoding as outlined in getPath(). - * - * @param string $path The path to use with the new instance. - * @return self A new instance with the specified path. - * @throws \InvalidArgumentException for invalid paths. - */ - public function withPath($path) - { - if (!is_string($path)) { - throw new InvalidArgumentException('Uri path must be a string'); - } - - $clone = clone $this; - $clone->path = $this->filterPath($path); - - return $clone; - } - - /** - * Filter Uri path. - * - * This method percent-encodes all reserved - * characters in the provided path string. This method - * will NOT double-encode characters that are already - * percent-encoded. - * - * @param string $path The raw uri path. - * @return string The RFC 3986 percent-encoded uri path. - * @link http://www.faqs.org/rfcs/rfc3986.html - */ - protected function filterPath($path) - { - return preg_replace_callback( - '/(?:[^a-zA-Z0-9_\-\.~:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/', - function ($match) { - return rawurlencode($match[0]); - }, - $path - ); - } - - /******************************************************************************** - * Query - *******************************************************************************/ - - /** - * Retrieve the query string of the URI. - * - * If no query string is present, this method MUST return an empty string. - * - * The leading "?" character is not part of the query and MUST NOT be - * added. - * - * The value returned MUST be percent-encoded, but MUST NOT double-encode - * any characters. To determine what characters to encode, please refer to - * RFC 3986, Sections 2 and 3.4. - * - * As an example, if a value in a key/value pair of the query string should - * include an ampersand ("&") not intended as a delimiter between values, - * that value MUST be passed in encoded form (e.g., "%26") to the instance. - * - * @see https://tools.ietf.org/html/rfc3986#section-2 - * @see https://tools.ietf.org/html/rfc3986#section-3.4 - * @return string The URI query string. - */ - public function getQuery() - { - return $this->query; - } - - /** - * Return an instance with the specified query string. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified query string. - * - * Users can provide both encoded and decoded query characters. - * Implementations ensure the correct encoding as outlined in getQuery(). - * - * An empty query string value is equivalent to removing the query string. - * - * @param string $query The query string to use with the new instance. - * @return self A new instance with the specified query string. - * @throws \InvalidArgumentException for invalid query strings. - */ - public function withQuery($query) - { - if (!is_string($query) && !method_exists($query, '__toString')) { - throw new InvalidArgumentException('Uri query must be a string'); - } - $query = ltrim((string)$query, '?'); - $clone = clone $this; - $clone->query = $this->filterQuery($query); - - return $clone; - } - - /** - * Filters the query string or fragment of a URI. - * - * @param string $query The raw uri query string. - * @return string The percent-encoded query string. - */ - protected function filterQuery($query) - { - return preg_replace_callback( - '/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/', - function ($match) { - return rawurlencode($match[0]); - }, - $query - ); - } - - /******************************************************************************** - * Fragment - *******************************************************************************/ - - /** - * Retrieve the fragment component of the URI. - * - * If no fragment is present, this method MUST return an empty string. - * - * The leading "#" character is not part of the fragment and MUST NOT be - * added. - * - * The value returned MUST be percent-encoded, but MUST NOT double-encode - * any characters. To determine what characters to encode, please refer to - * RFC 3986, Sections 2 and 3.5. - * - * @see https://tools.ietf.org/html/rfc3986#section-2 - * @see https://tools.ietf.org/html/rfc3986#section-3.5 - * @return string The URI fragment. - */ - public function getFragment() - { - return $this->fragment; - } - - /** - * Return an instance with the specified URI fragment. - * - * This method MUST retain the state of the current instance, and return - * an instance that contains the specified URI fragment. - * - * Users can provide both encoded and decoded fragment characters. - * Implementations ensure the correct encoding as outlined in getFragment(). - * - * An empty fragment value is equivalent to removing the fragment. - * - * @param string $fragment The fragment to use with the new instance. - * @return self A new instance with the specified fragment. - */ - public function withFragment($fragment) - { - if (!is_string($fragment) && !method_exists($fragment, '__toString')) { - throw new InvalidArgumentException('Uri fragment must be a string'); - } - $fragment = ltrim((string)$fragment, '#'); - $clone = clone $this; - $clone->fragment = $this->filterQuery($fragment); - - return $clone; - } - - /******************************************************************************** - * Helpers - *******************************************************************************/ - - /** - * Return the string representation as a URI reference. - * - * Depending on which components of the URI are present, the resulting - * string is either a full URI or relative reference according to RFC 3986, - * Section 4.1. The method concatenates the various components of the URI, - * using the appropriate delimiters: - * - * - If a scheme is present, it MUST be suffixed by ":". - * - If an authority is present, it MUST be prefixed by "//". - * - The path can be concatenated without delimiters. But there are two - * cases where the path has to be adjusted to make the URI reference - * valid as PHP does not allow to throw an exception in __toString(): - * - If the path is rootless and an authority is present, the path MUST - * be prefixed by "/". - * - If the path is starting with more than one "/" and no authority is - * present, the starting slashes MUST be reduced to one. - * - If a query is present, it MUST be prefixed by "?". - * - If a fragment is present, it MUST be prefixed by "#". - * - * @see http://tools.ietf.org/html/rfc3986#section-4.1 - * @return string - */ - public function __toString() - { - $scheme = $this->getScheme(); - $authority = $this->getAuthority(); - $path = $this->getPath(); - $query = $this->getQuery(); - $fragment = $this->getFragment(); - - $path = '/' . ltrim($path, '/'); - - return ($scheme ? $scheme . ':' : '') - . ($authority ? '//' . $authority : '') - . $path - . ($query ? '?' . $query : '') - . ($fragment ? '#' . $fragment : ''); - } - - /** - * Return the fully qualified base URL. - * - * Note that this method never includes a trailing / - * - * This method is not part of PSR-7. - * - * @return string - */ - public function getBaseUrl() - { - $scheme = $this->getScheme(); - $authority = $this->getAuthority(); - - return ($scheme ? $scheme . ':' : '') - . ($authority ? '//' . $authority : ''); - } -} diff --git a/Slim/Interfaces/Http/CookiesInterface.php b/Slim/Interfaces/Http/CookiesInterface.php deleted file mode 100644 index 206175adb..000000000 --- a/Slim/Interfaces/Http/CookiesInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -stream) === true) { - fclose($this->stream); - } - } - - /** - * This method creates a new resource, and it seeds - * the resource with lorem ipsum text. The returned - * resource is readable, writable, and seekable. - * - * @param string $mode - * - * @return resource - */ - public function resourceFactory($mode = 'r+') - { - $stream = fopen('php://temp', $mode); - fwrite($stream, $this->text); - rewind($stream); - - return $stream; - } - - public function testConstructorAttachesStream() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - - $this->assertSame($this->stream, $bodyStream->getValue($body)); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testConstructorInvalidStream() - { - $this->stream = 'foo'; - $body = new Body($this->stream); - } - - public function testGetMetadata() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertTrue(is_array($body->getMetadata())); - } - - public function testGetMetadataKey() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertEquals('php://temp', $body->getMetadata('uri')); - } - - public function testGetMetadataKeyNotFound() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertNull($body->getMetadata('foo')); - } - - public function testDetach() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - - $bodyMetadata = new ReflectionProperty($body, 'meta'); - $bodyMetadata->setAccessible(true); - - $bodyReadable = new ReflectionProperty($body, 'readable'); - $bodyReadable->setAccessible(true); - - $bodyWritable = new ReflectionProperty($body, 'writable'); - $bodyWritable->setAccessible(true); - - $bodySeekable = new ReflectionProperty($body, 'seekable'); - $bodySeekable->setAccessible(true); - - $result = $body->detach(); - - $this->assertSame($this->stream, $result); - $this->assertNull($bodyStream->getValue($body)); - $this->assertNull($bodyMetadata->getValue($body)); - $this->assertNull($bodyReadable->getValue($body)); - $this->assertNull($bodyWritable->getValue($body)); - $this->assertNull($bodySeekable->getValue($body)); - } - - public function testToStringAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertEquals($this->text, (string)$body); - } - - public function testToStringAttachedRewindsFirst() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertEquals($this->text, (string)$body); - $this->assertEquals($this->text, (string)$body); - $this->assertEquals($this->text, (string)$body); - } - - public function testToStringDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($body, null); - - $this->assertEquals('', (string)$body); - } - - public function testClose() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->close(); - - $this->assertAttributeEquals(null, 'stream', $body); - //$this->assertFalse($body->isAttached()); #1269 - } - - public function testGetSizeAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertEquals(mb_strlen($this->text), $body->getSize()); - } - - public function testGetSizeDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($body, null); - - $this->assertNull($body->getSize()); - } - - public function testTellAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - fseek($this->stream, 10); - - $this->assertEquals(10, $body->tell()); - } - - /** - * @expectedException \RuntimeException - */ - public function testTellDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($body, null); - - $body->tell(); - } - - public function testEofAttachedFalse() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - fseek($this->stream, 10); - - $this->assertFalse($body->eof()); - } - - public function testEofAttachedTrue() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - while (feof($this->stream) === false) { - fread($this->stream, 1024); - } - - $this->assertTrue($body->eof()); - } - - public function testEofDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $bodyStream = new ReflectionProperty($body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($body, null); - - $this->assertTrue($body->eof()); - } - - public function isReadableAttachedTrue() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertTrue($body->isReadable()); - } - - public function isReadableAttachedFalse() - { - $stream = fopen('php://temp', 'w'); - $body = new Body($this->stream); - - $this->assertFalse($body->isReadable()); - fclose($stream); - } - - public function testIsReadableDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $this->assertFalse($body->isReadable()); - } - - public function isWritableAttachedTrue() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertTrue($body->isWritable()); - } - - public function isWritableAttachedFalse() - { - $stream = fopen('php://temp', 'r'); - $body = new Body($this->stream); - - $this->assertFalse($body->isWritable()); - fclose($stream); - } - - public function testIsWritableDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $this->assertFalse($body->isWritable()); - } - - public function isSeekableAttachedTrue() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertTrue($body->isSeekable()); - } - - // TODO: Is seekable is false when attached... how? - - public function testIsSeekableDetached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $this->assertFalse($body->isSeekable()); - } - - public function testSeekAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->seek(10); - - $this->assertEquals(10, ftell($this->stream)); - } - - /** - * @expectedException \RuntimeException - */ - public function testSeekDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $body->seek(10); - } - - public function testRewindAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - fseek($this->stream, 10); - $body->rewind(); - - $this->assertEquals(0, ftell($this->stream)); - } - - /** - * @expectedException \RuntimeException - */ - public function testRewindDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $body->rewind(); - } - - public function testReadAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - - $this->assertEquals(substr($this->text, 0, 10), $body->read(10)); - } - - /** - * @expectedException \RuntimeException - */ - public function testReadDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $body->read(10); - } - - public function testWriteAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - while (feof($this->stream) === false) { - fread($this->stream, 1024); - } - $body->write('foo'); - - $this->assertEquals($this->text . 'foo', (string)$body); - } - - /** - * @expectedException \RuntimeException - */ - public function testWriteDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $body->write('foo'); - } - - public function testGetContentsAttached() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - fseek($this->stream, 10); - - $this->assertEquals(substr($this->text, 10), $body->getContents()); - } - - /** - * @expectedException \RuntimeException - */ - public function testGetContentsDetachedThrowsRuntimeException() - { - $this->stream = $this->resourceFactory(); - $body = new Body($this->stream); - $body->detach(); - - $body->getContents(); - } -} diff --git a/tests/Http/CookiesTest.php b/tests/Http/CookiesTest.php deleted file mode 100644 index 8315d80d4..000000000 --- a/tests/Http/CookiesTest.php +++ /dev/null @@ -1,235 +0,0 @@ - 'Works', - ]); - $prop = new ReflectionProperty($cookies, 'requestCookies'); - $prop->setAccessible(true); - $this->assertNotEmpty($prop->getValue($cookies)['test']); - $this->assertEquals('Works', $prop->getValue($cookies)['test']); - } - - public function testSetDefaults() - { - $defaults = [ - 'value' => 'toast', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => true, - 'httponly' => true - ]; - - $cookies = new Cookies; - - $prop = new ReflectionProperty($cookies, 'defaults'); - $prop->setAccessible(true); - - $origDefaults = $prop->getValue($cookies); - - $cookies->setDefaults($defaults); - - $this->assertEquals($defaults, $prop->getValue($cookies)); - $this->assertNotEquals($origDefaults, $prop->getValue($cookies)); - } - - public function testSetCookieValues() - { - $cookies = new Cookies; - $cookies->set('foo', 'bar'); - - $prop = new ReflectionProperty($cookies, 'responseCookies'); - $prop->setAccessible(true); - - //we expect all of these values with null/false defaults - $expectedValue = [ - 'foo' => [ - 'value' => 'bar', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => false, - 'httponly' => false - ] - ]; - - $this->assertEquals($expectedValue, $prop->getValue($cookies)); - } - - public function testSetCookieValuesContainDefaults() - { - $cookies = new Cookies; - $defaults = [ - 'value' => 'toast', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => true, - 'httponly' => true - ]; - - $cookies->setDefaults($defaults); - $cookies->set('foo', 'bar'); - - $prop = new ReflectionProperty($cookies, 'responseCookies'); - $prop->setAccessible(true); - - //we expect to have secure and httponly from defaults - $expectedValue = [ - 'foo' => [ - 'value' => 'bar', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => true, - 'httponly' => true - ] - ]; - - $this->assertEquals($expectedValue, $prop->getValue($cookies)); - } - - public function testSetCookieValuesCanOverrideDefaults() - { - $cookies = new Cookies; - $defaults = [ - 'value' => 'toast', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => true, - 'httponly' => true - ]; - - $cookies->setDefaults($defaults); - - //default has secure true, lets override it to false - $cookies->set('foo', ['value' => 'bar', 'secure' => false]); - - $prop = new ReflectionProperty($cookies, 'responseCookies'); - $prop->setAccessible(true); - - $expectedValue = [ - 'foo' => [ - 'value' => 'bar', - 'domain' => null, - 'hostonly' => null, - 'path' => null, - 'expires' => null, - 'secure' => false, - 'httponly' => true - ] - ]; - - $this->assertEquals($expectedValue, $prop->getValue($cookies)); - } - - public function testGet() - { - $cookies = new Cookies(['foo' => 'bar']); - $this->assertEquals('bar', $cookies->get('foo')); - $this->assertNull($cookies->get('missing')); - $this->assertEquals('defaultValue', $cookies->get('missing', 'defaultValue')); - } - - public function testParseHeader() - { - $cookies = Cookies::parseHeader('foo=bar; name=Josh'); - $this->assertEquals('bar', $cookies['foo']); - $this->assertEquals('Josh', $cookies['name']); - } - - public function testParseHeaderWithJsonArray() - { - $cookies = Cookies::parseHeader('foo=bar; testarray=["someVar1","someVar2","someVar3"]'); - $this->assertEquals('bar', $cookies['foo']); - $this->assertContains('someVar3', json_decode($cookies['testarray'])); - } - - public function testToHeaders() - { - $cookies = new Cookies; - $cookies->set('test', 'Works'); - $cookies->set('test_array', ['value' => 'bar', 'domain' => 'example.com']); - $this->assertEquals('test=Works', $cookies->toHeaders()[0]); - $this->assertEquals('test_array=bar; domain=example.com', $cookies->toHeaders()[1]); - } - - public function testToHeader() - { - $cookies = new Cookies(); - $class = new ReflectionClass($cookies); - $method = $class->getMethod('toHeader'); - $method->setAccessible(true); - $properties = [ - 'name' => 'test', - 'properties' => [ - 'value' => 'Works' - ] - ]; - $time = time(); - $formattedDate = gmdate('D, d-M-Y H:i:s e', $time); - $propertiesComplex = [ - 'name' => 'test_complex', - 'properties' => [ - 'value' => 'Works', - 'domain' => 'example.com', - 'expires' => $time, - 'path' => '/', - 'secure' => true, - 'hostonly' => true, - 'httponly' => true - ] - ]; - $stringDate = '2016-01-01 12:00:00'; - $formattedStringDate = gmdate('D, d-M-Y H:i:s e', strtotime($stringDate)); - $propertiesStringDate = [ - 'name' => 'test_date', - 'properties' => [ - 'value' => 'Works', - 'expires' => $stringDate, - ] - ]; - $cookie = $method->invokeArgs($cookies, $properties); - $cookieComplex = $method->invokeArgs($cookies, $propertiesComplex); - $cookieStringDate = $method->invokeArgs($cookies, $propertiesStringDate); - $this->assertEquals('test=Works', $cookie); - $this->assertEquals( - 'test_complex=Works; domain=example.com; path=/; expires=' - . $formattedDate . '; secure; HostOnly; HttpOnly', - $cookieComplex - ); - $this->assertEquals('test_date=Works; expires=' . $formattedStringDate, $cookieStringDate); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testParseHeaderException() - { - Cookies::parseHeader(new \StdClass); - } -} diff --git a/tests/Http/EnvironmentTest.php b/tests/Http/EnvironmentTest.php deleted file mode 100644 index e427c6abe..000000000 --- a/tests/Http/EnvironmentTest.php +++ /dev/null @@ -1,59 +0,0 @@ -assertEquals($_SERVER, $env->all()); - } - - /** - * Test environment from mock data - */ - public function testMock() - { - $env = Environment::mock([ - 'SCRIPT_NAME' => '/foo/bar/index.php', - 'REQUEST_URI' => '/foo/bar?abc=123', - ]); - - $this->assertInstanceOf('\Slim\Interfaces\CollectionInterface', $env); - $this->assertEquals('/foo/bar/index.php', $env->get('SCRIPT_NAME')); - $this->assertEquals('/foo/bar?abc=123', $env->get('REQUEST_URI')); - $this->assertEquals('localhost', $env->get('HTTP_HOST')); - } -} diff --git a/tests/Http/HeadersTest.php b/tests/Http/HeadersTest.php deleted file mode 100644 index cccf700c0..000000000 --- a/tests/Http/HeadersTest.php +++ /dev/null @@ -1,228 +0,0 @@ - 'application/json', - ]); - $h = Headers::createFromEnvironment($e); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['accept'])); - $this->assertEquals('application/json', $prop->getValue($h)['accept']['value'][0]); - } - - public function testCreateFromEnvironmentWithSpecialHeaders() - { - $e = Environment::mock([ - 'CONTENT_TYPE' => 'application/json', - ]); - $h = Headers::createFromEnvironment($e); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['content-type'])); - $this->assertEquals('application/json', $prop->getValue($h)['content-type']['value'][0]); - } - - public function testCreateFromEnvironmentIgnoresHeaders() - { - $e = Environment::mock([ - 'CONTENT_TYPE' => 'text/csv', - 'HTTP_CONTENT_LENGTH' => 1230, // <-- Ignored - ]); - $h = Headers::createFromEnvironment($e); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertNotContains('content-length', $prop->getValue($h)); - } - - public function testConstructor() - { - $h = new Headers([ - 'Content-Length' => 100, - ]); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['content-length'])); - $this->assertEquals(100, $prop->getValue($h)['content-length']['value'][0]); - } - - public function testSetSingleValue() - { - $h = new Headers(); - $h->set('Content-Length', 100); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['content-length'])); - $this->assertEquals(100, $prop->getValue($h)['content-length']['value'][0]); - } - - public function testSetArrayValue() - { - $h = new Headers(); - $h->set('Allow', ['GET', 'POST']); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['allow'])); - $this->assertEquals(['GET', 'POST'], $prop->getValue($h)['allow']['value']); - } - - public function testGet() - { - $h = new Headers(); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - $prop->setValue($h, [ - 'allow' => [ - 'value' => ['GET', 'POST'], - 'originalKey' => 'Allow' - ] - ]); - - $this->assertEquals(['GET', 'POST'], $h->get('Allow')); - } - - public function testGetOriginalKey() - { - $h = new Headers(); - $h->set('http-test_key', 'testValue'); - $h->get('test-key'); - - $value = $h->get('test-key'); - - $this->assertEquals('testValue', reset($value)); - $this->assertEquals('http-test_key', $h->getOriginalKey('test-key')); - $this->assertNull($h->getOriginalKey('test-non-existing')); - } - - public function testGetNotExists() - { - $h = new Headers(); - - $this->assertNull($h->get('Foo')); - } - - public function testAddNewValue() - { - $h = new Headers(); - $h->add('Foo', 'Bar'); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['foo'])); - $this->assertEquals(['Bar'], $prop->getValue($h)['foo']['value']); - } - - public function testAddAnotherValue() - { - $h = new Headers(); - $h->add('Foo', 'Bar'); - $h->add('Foo', 'Xyz'); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['foo'])); - $this->assertEquals(['Bar', 'Xyz'], $prop->getValue($h)['foo']['value']); - } - - public function testAddArrayValue() - { - $h = new Headers(); - $h->add('Foo', 'Bar'); - $h->add('Foo', ['Xyz', '123']); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - - $this->assertTrue(is_array($prop->getValue($h)['foo'])); - $this->assertEquals(['Bar', 'Xyz', '123'], $prop->getValue($h)['foo']['value']); - } - - public function testHas() - { - $h = new Headers(); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - $prop->setValue($h, [ - 'allow' => [ - 'value' => ['GET', 'POST'], - 'originalKey' => 'Allow' - ] - ]); - $this->assertTrue($h->has('allow')); - $this->assertFalse($h->has('foo')); - } - - public function testRemove() - { - $h = new Headers(); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - $prop->setValue($h, [ - 'Allow' => [ - 'value' => ['GET', 'POST'], - 'originalKey' => 'Allow' - ] - ]); - $h->remove('Allow'); - - $this->assertNotContains('Allow', $prop->getValue($h)); - } - - public function testOriginalKeys() - { - $h = new Headers(); - $prop = new ReflectionProperty($h, 'data'); - $prop->setAccessible(true); - $prop->setValue($h, [ - 'Allow' => [ - 'value' => ['GET', 'POST'], - 'originalKey' => 'ALLOW' - ] - ]); - $all = $h->all(); - - $this->assertArrayHasKey('ALLOW', $all); - } - - public function testNormalizeKey() - { - $h = new Headers(); - $this->assertEquals('foo-bar', $h->normalizeKey('HTTP_FOO_BAR')); - $this->assertEquals('foo-bar', $h->normalizeKey('HTTP-FOO-BAR')); - $this->assertEquals('foo-bar', $h->normalizeKey('Http-Foo-Bar')); - $this->assertEquals('foo-bar', $h->normalizeKey('Http_Foo_Bar')); - $this->assertEquals('foo-bar', $h->normalizeKey('http_foo_bar')); - $this->assertEquals('foo-bar', $h->normalizeKey('http-foo-bar')); - } - - public function testDetermineAuthorization() - { - $e = Environment::mock([]); - $en = Headers::determineAuthorization($e); - $h = Headers::createFromEnvironment($e); - - $this->assertEquals('electrolytes', $en->get('HTTP_AUTHORIZATION')); - $this->assertEquals(['electrolytes'], $h->get('Authorization')); - } -} diff --git a/tests/Http/MessageTest.php b/tests/Http/MessageTest.php deleted file mode 100644 index 9beb7e4a4..000000000 --- a/tests/Http/MessageTest.php +++ /dev/null @@ -1,214 +0,0 @@ -protocolVersion = '1.0'; - - $this->assertEquals('1.0', $message->getProtocolVersion()); - } - - /** - * @covers Slim\Http\Message::withProtocolVersion - */ - public function testWithProtocolVersion() - { - $message = new MessageStub(); - $clone = $message->withProtocolVersion('1.0'); - - $this->assertEquals('1.0', $clone->protocolVersion); - } - - /** - * @covers Slim\Http\Message::withProtocolVersion - * @expectedException \InvalidArgumentException - */ - public function testWithProtocolVersionInvalidThrowsException() - { - $message = new MessageStub(); - $message->withProtocolVersion('3.0'); - } - - /******************************************************************************* - * Headers - ******************************************************************************/ - - /** - * @covers Slim\Http\Message::getHeaders - */ - public function testGetHeaders() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $headers->add('X-Foo', 'two'); - $headers->add('X-Foo', 'three'); - - $message = new MessageStub(); - $message->headers = $headers; - - $shouldBe = [ - 'X-Foo' => [ - 'one', - 'two', - 'three', - ], - ]; - - $this->assertEquals($shouldBe, $message->getHeaders()); - } - - /** - * @covers Slim\Http\Message::hasHeader - */ - public function testHasHeader() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - - $message = new MessageStub(); - $message->headers = $headers; - - $this->assertTrue($message->hasHeader('X-Foo')); - $this->assertFalse($message->hasHeader('X-Bar')); - } - - /** - * @covers Slim\Http\Message::getHeaderLine - */ - public function testGetHeaderLine() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $headers->add('X-Foo', 'two'); - $headers->add('X-Foo', 'three'); - - $message = new MessageStub(); - $message->headers = $headers; - - $this->assertEquals('one,two,three', $message->getHeaderLine('X-Foo')); - $this->assertEquals('', $message->getHeaderLine('X-Bar')); - } - - /** - * @covers Slim\Http\Message::getHeader - */ - public function testGetHeader() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $headers->add('X-Foo', 'two'); - $headers->add('X-Foo', 'three'); - - $message = new MessageStub(); - $message->headers = $headers; - - $this->assertEquals(['one', 'two', 'three'], $message->getHeader('X-Foo')); - $this->assertEquals([], $message->getHeader('X-Bar')); - } - - /** - * @covers Slim\Http\Message::withHeader - */ - public function testWithHeader() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $message = new MessageStub(); - $message->headers = $headers; - $clone = $message->withHeader('X-Foo', 'bar'); - - $this->assertEquals('bar', $clone->getHeaderLine('X-Foo')); - } - - /** - * @covers Slim\Http\Message::withAddedHeader - */ - public function testWithAddedHeader() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $message = new MessageStub(); - $message->headers = $headers; - $clone = $message->withAddedHeader('X-Foo', 'two'); - - $this->assertEquals('one,two', $clone->getHeaderLine('X-Foo')); - } - - /** - * @covers Slim\Http\Message::withoutHeader - */ - public function testWithoutHeader() - { - $headers = new Headers(); - $headers->add('X-Foo', 'one'); - $headers->add('X-Bar', 'two'); - $response = new MessageStub(); - $response->headers = $headers; - $clone = $response->withoutHeader('X-Foo'); - $shouldBe = [ - 'X-Bar' => ['two'], - ]; - - $this->assertEquals($shouldBe, $clone->getHeaders()); - } - - /******************************************************************************* - * Body - ******************************************************************************/ - - /** - * @covers Slim\Http\Message::getBody - */ - public function testGetBody() - { - $body = $this->getBody(); - $message = new MessageStub(); - $message->body = $body; - - $this->assertSame($body, $message->getBody()); - } - - /** - * @covers Slim\Http\Message::withBody - */ - public function testWithBody() - { - $body = $this->getBody(); - $body2 = $this->getBody(); - $message = new MessageStub(); - $message->body = $body; - $clone = $message->withBody($body2); - - $this->assertSame($body, $message->body); - $this->assertSame($body2, $clone->body); - } - - /** - * @return \PHPUnit_Framework_MockObject_MockObject|\Slim\Http\Body - */ - protected function getBody() - { - return $this->getMockBuilder('Slim\Http\Body')->disableOriginalConstructor()->getMock(); - } -} diff --git a/tests/Http/RequestBodyTest.php b/tests/Http/RequestBodyTest.php deleted file mode 100644 index 2aa8d966d..000000000 --- a/tests/Http/RequestBodyTest.php +++ /dev/null @@ -1,334 +0,0 @@ -body = new RequestBody(); - $this->body->write($this->text); - $this->body->rewind(); - } - - protected function tearDown() - { - if (is_resource($this->stream) === true) { - fclose($this->stream); - } - $this->body = null; - } - - /** - * This method creates a new resource, and it seeds - * the resource with lorem ipsum text. The returned - * resource is readable, writable, and seekable. - * - * @param string $mode - * - * @return resource - */ - public function resourceFactory($mode = 'r+') - { - $stream = fopen('php://temp', $mode); - fwrite($stream, $this->text); - rewind($stream); - - return $stream; - } - - public function testConstructorAttachesStream() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - - $this->assertInternalType('resource', $bodyStream->getValue($this->body)); - } - - public function testConstructorSetsMetadata() - { - $bodyMetadata = new ReflectionProperty($this->body, 'meta'); - $bodyMetadata->setAccessible(true); - - $this->assertTrue(is_array($bodyMetadata->getValue($this->body))); - } - - public function testGetMetadata() - { - $this->assertTrue(is_array($this->body->getMetadata())); - } - - public function testGetMetadataKey() - { - $this->assertEquals('php://temp', $this->body->getMetadata('uri')); - } - - public function testGetMetadataKeyNotFound() - { - $this->assertNull($this->body->getMetadata('foo')); - } - - public function testDetach() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - - $bodyMetadata = new ReflectionProperty($this->body, 'meta'); - $bodyMetadata->setAccessible(true); - - $bodyReadable = new ReflectionProperty($this->body, 'readable'); - $bodyReadable->setAccessible(true); - - $bodyWritable = new ReflectionProperty($this->body, 'writable'); - $bodyWritable->setAccessible(true); - - $bodySeekable = new ReflectionProperty($this->body, 'seekable'); - $bodySeekable->setAccessible(true); - - $result = $this->body->detach(); - - $this->assertInternalType('resource', $result); - $this->assertNull($bodyStream->getValue($this->body)); - $this->assertNull($bodyMetadata->getValue($this->body)); - $this->assertNull($bodyReadable->getValue($this->body)); - $this->assertNull($bodyWritable->getValue($this->body)); - $this->assertNull($bodySeekable->getValue($this->body)); - } - - public function testToStringAttached() - { - $this->assertEquals($this->text, (string)$this->body); - } - - public function testToStringAttachedRewindsFirst() - { - $this->assertEquals($this->text, (string)$this->body); - $this->assertEquals($this->text, (string)$this->body); - $this->assertEquals($this->text, (string)$this->body); - } - - public function testToStringDetached() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($this->body, null); - - $this->assertEquals('', (string)$this->body); - } - - /** - * @expectedException \RuntimeException - */ - public function testClose() - { - $this->body->close(); - - $this->assertAttributeEquals(null, 'stream', $this->body); - $this->assertFalse($this->body->isReadable()); - $this->assertFalse($this->body->isWritable()); - $this->assertEquals('', (string)$this->body); - - $this->body->tell(); - } - - public function testGetSizeAttached() - { - $this->assertEquals(mb_strlen($this->text), $this->body->getSize()); - } - - public function testGetSizeDetached() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($this->body, null); - - $this->assertNull($this->body->getSize()); - } - - public function testTellAttached() - { - $this->body->seek(10); - - $this->assertEquals(10, $this->body->tell()); - } - - /** - * @expectedException \RuntimeException - */ - public function testTellDetachedThrowsRuntimeException() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($this->body, null); - - $this->body->tell(); - } - - public function testEofAttachedFalse() - { - $this->body->seek(10); - - $this->assertFalse($this->body->eof()); - } - - public function testEofAttachedTrue() - { - while ($this->body->eof() === false) { - $this->body->read(1024); - } - - $this->assertTrue($this->body->eof()); - } - - public function testEofDetached() - { - $bodyStream = new ReflectionProperty($this->body, 'stream'); - $bodyStream->setAccessible(true); - $bodyStream->setValue($this->body, null); - - $this->assertTrue($this->body->eof()); - } - - public function testIsReadableAttachedTrue() - { - $this->assertTrue($this->body->isReadable()); - } - - public function testIsReadableDetached() - { - $this->body->detach(); - - $this->assertFalse($this->body->isReadable()); - } - - public function testIsWritableAttachedTrue() - { - $this->assertTrue($this->body->isWritable()); - } - - public function testIsWritableDetached() - { - $this->body->detach(); - - $this->assertFalse($this->body->isWritable()); - } - - public function isSeekableAttachedTrue() - { - $this->assertTrue($this->body->isSeekable()); - } - - // TODO: Is seekable is false when attached... how? - - public function testIsSeekableDetached() - { - $this->body->detach(); - - $this->assertFalse($this->body->isSeekable()); - } - - public function testSeekAttached() - { - $this->body->seek(10); - - $this->assertEquals(10, $this->body->tell()); - } - - /** - * @expectedException \RuntimeException - */ - public function testSeekDetachedThrowsRuntimeException() - { - $this->body->detach(); - $this->body->seek(10); - } - - public function testRewindAttached() - { - $this->body->seek(10); - $this->body->rewind(); - - $this->assertEquals(0, $this->body->tell()); - } - - /** - * @expectedException \RuntimeException - */ - public function testRewindDetachedThrowsRuntimeException() - { - $this->body->detach(); - $this->body->rewind(); - } - - public function testReadAttached() - { - $this->assertEquals(substr($this->text, 0, 10), $this->body->read(10)); - } - - /** - * @expectedException \RuntimeException - */ - public function testReadDetachedThrowsRuntimeException() - { - $this->body->detach(); - $this->body->read(10); - } - - public function testWriteAttached() - { - while ($this->body->eof() === false) { - $this->body->read(1024); - } - $this->body->write('foo'); - - $this->assertEquals($this->text . 'foo', (string)$this->body); - } - - /** - * @expectedException \RuntimeException - */ - public function testWriteDetachedThrowsRuntimeException() - { - $this->body->detach(); - $this->body->write('foo'); - } - - public function testGetContentsAttached() - { - $this->body->seek(10); - - $this->assertEquals(substr($this->text, 10), $this->body->getContents()); - } - - /** - * @expectedException \RuntimeException - */ - public function testGetContentsDetachedThrowsRuntimeException() - { - $this->body->detach(); - $this->body->getContents(); - } -} diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php deleted file mode 100644 index 6d0bdbc75..000000000 --- a/tests/Http/RequestTest.php +++ /dev/null @@ -1,1168 +0,0 @@ - 'john', - 'id' => '123', - ]; - $serverParams = $env->all(); - $body = new RequestBody(); - $uploadedFiles = UploadedFile::createFromEnvironment($env); - $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); - - return $request; - } - - public function testDisableSetter() - { - $request = $this->requestFactory(); - $request->foo = 'bar'; - - $this->assertFalse(property_exists($request, 'foo')); - } - - public function testAddsHostHeaderFromUri() - { - $request = $this->requestFactory(); - $this->assertEquals('example.com', $request->getHeaderLine('Host')); - } - - /******************************************************************************* - * Method - ******************************************************************************/ - - public function testGetMethod() - { - $this->assertEquals('GET', $this->requestFactory()->getMethod()); - } - - public function testGetOriginalMethod() - { - $this->assertEquals('GET', $this->requestFactory()->getOriginalMethod()); - } - - public function testWithMethod() - { - $request = $this->requestFactory()->withMethod('PUT'); - - $this->assertAttributeEquals('PUT', 'method', $request); - $this->assertAttributeEquals('PUT', 'originalMethod', $request); - } - - public function testWithAllAllowedCharactersMethod() - { - $request = $this->requestFactory()->withMethod("!#$%&'*+.^_`|~09AZ-"); - - $this->assertAttributeEquals("!#$%&'*+.^_`|~09AZ-", 'method', $request); - $this->assertAttributeEquals("!#$%&'*+.^_`|~09AZ-", 'originalMethod', $request); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testWithMethodInvalid() - { - $this->requestFactory()->withMethod('B@R'); - } - - public function testWithMethodNull() - { - $request = $this->requestFactory()->withMethod(null); - - $this->assertAttributeEquals(null, 'originalMethod', $request); - } - - /** - * @covers Slim\Http\Request::createFromEnvironment - */ - public function testCreateFromEnvironment() - { - $env = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo', - 'REQUEST_METHOD' => 'POST', - ]); - - $request = Request::createFromEnvironment($env); - $this->assertEquals('POST', $request->getMethod()); - $this->assertEquals($env->all(), $request->getServerParams()); - } - - /** - * @covers Slim\Http\Request::createFromEnvironment - */ - public function testCreateFromEnvironmentWithMultipart() - { - $_POST['foo'] = 'bar'; - - $env = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo', - 'REQUEST_METHOD' => 'POST', - 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo' - ]); - - $request = Request::createFromEnvironment($env); - unset($_POST); - - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - } - - /** - * @covers Slim\Http\Request::createFromEnvironment - */ - public function testCreateFromEnvironmentWithMultipartMethodOverride() - { - $_POST['_METHOD'] = 'PUT'; - - $env = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo', - 'REQUEST_METHOD' => 'POST', - 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo' - ]); - - $request = Request::createFromEnvironment($env); - unset($_POST); - - $this->assertEquals('POST', $request->getOriginalMethod()); - $this->assertEquals('PUT', $request->getMethod()); - } - - public function testGetMethodWithOverrideHeader() - { - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = new Headers([ - 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals('PUT', $request->getMethod()); - $this->assertEquals('POST', $request->getOriginalMethod()); - } - - public function testGetMethodWithOverrideParameterFromBodyObject() - { - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = new Headers([ - 'Content-Type' => 'application/x-www-form-urlencoded', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('_METHOD=PUT'); - $body->rewind(); - $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals('PUT', $request->getMethod()); - $this->assertEquals('POST', $request->getOriginalMethod()); - } - - public function testGetMethodOverrideParameterFromBodyArray() - { - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = new Headers([ - 'Content-Type' => 'application/x-www-form-urlencoded', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('_METHOD=PUT'); - $body->rewind(); - $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); - $request->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) { - parse_str($input, $body); - return $body; // <-- Array - }); - - $this->assertEquals('PUT', $request->getMethod()); - } - - /** - * @expectedException \InvalidArgumentException - */ - 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('B@R', $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); - } - - public function testIsGet() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'GET'); - - $this->assertTrue($request->isGet()); - } - - public function testIsPost() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'POST'); - - $this->assertTrue($request->isPost()); - } - - public function testIsPut() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'PUT'); - - $this->assertTrue($request->isPut()); - } - - public function testIsPatch() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'PATCH'); - - $this->assertTrue($request->isPatch()); - } - - public function testIsDelete() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'DELETE'); - - $this->assertTrue($request->isDelete()); - } - - public function testIsHead() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'HEAD'); - - $this->assertTrue($request->isHead()); - } - - public function testIsOptions() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'originalMethod'); - $prop->setAccessible(true); - $prop->setValue($request, 'OPTIONS'); - - $this->assertTrue($request->isOptions()); - } - - public function testIsXhr() - { - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = new Headers([ - 'Content-Type' => 'application/x-www-form-urlencoded', - 'X-Requested-With' => 'XMLHttpRequest', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); - - $this->assertTrue($request->isXhr()); - } - - /******************************************************************************* - * URI - ******************************************************************************/ - - public function testGetRequestTarget() - { - $this->assertEquals('/foo/bar?abc=123', $this->requestFactory()->getRequestTarget()); - } - - public function testGetRequestTargetAlreadySet() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'requestTarget'); - $prop->setAccessible(true); - $prop->setValue($request, '/foo/bar?abc=123'); - - $this->assertEquals('/foo/bar?abc=123', $request->getRequestTarget()); - } - - public function testGetRequestTargetIfNoUri() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'uri'); - $prop->setAccessible(true); - $prop->setValue($request, null); - - $this->assertEquals('/', $request->getRequestTarget()); - } - - public function testWithRequestTarget() - { - $clone = $this->requestFactory()->withRequestTarget('/test?user=1'); - - $this->assertAttributeEquals('/test?user=1', 'requestTarget', $clone); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testWithRequestTargetThatHasSpaces() - { - $this->requestFactory()->withRequestTarget('/test/m ore/stuff?user=1'); - } - - 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); - - $this->assertSame($uri, $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); - - $this->assertAttributeSame($uri2, 'uri', $clone); - } - - public function testWithUriPreservesHost() - { - // When `$preserveHost` is set to `true`, this method interacts with - // the Host header in the following ways: - - // - If the the Host header is missing or empty, and the new URI contains - // a host component, this method MUST update the Host header in the returned - // request. - $uri1 = Uri::createFromString(''); - $uri2 = Uri::createFromString('http://example2.com/test'); - - // Request - $headers = new Headers(); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $request = new Request('GET', $uri1, $headers, $cookies, $serverParams, $body); - - $clone = $request->withUri($uri2, true); - $this->assertSame('example2.com', $clone->getHeaderLine('Host')); - - // - If the Host header is missing or empty, and the new URI does not contain a - // host component, this method MUST NOT update the Host header in the returned - // request. - $uri3 = Uri::createFromString(''); - - $clone = $request->withUri($uri3, true); - $this->assertSame('', $clone->getHeaderLine('Host')); - - // - If a Host header is present and non-empty, this method MUST NOT update - // the Host header in the returned request. - $request = $request->withHeader('Host', 'example.com'); - $clone = $request->withUri($uri2, true); - $this->assertSame('example.com', $clone->getHeaderLine('Host')); - } - - public function testGetContentType() - { - $headers = new Headers([ - '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()); - } - - public function testGetContentTypeEmpty() - { - $request = $this->requestFactory(); - - $this->assertNull($request->getContentType()); - } - - public function testGetMediaType() - { - $headers = new Headers([ - '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()); - } - - public function testGetMediaTypeEmpty() - { - $request = $this->requestFactory(); - - $this->assertNull($request->getMediaType()); - } - - public function testGetMediaTypeParams() - { - $headers = new Headers([ - 'Content-Type' => ['application/json;charset=utf8;foo=bar'], - ]); - $request = $this->requestFactory(); - $headersProp = new ReflectionProperty($request, 'headers'); - $headersProp->setAccessible(true); - $headersProp->setValue($request, $headers); - - $this->assertEquals(['charset' => 'utf8', 'foo' => 'bar'], $request->getMediaTypeParams()); - } - - public function testGetMediaTypeParamsEmpty() - { - $headers = new Headers([ - 'Content-Type' => ['application/json'], - ]); - $request = $this->requestFactory(); - $headersProp = new ReflectionProperty($request, 'headers'); - $headersProp->setAccessible(true); - $headersProp->setValue($request, $headers); - - $this->assertEquals([], $request->getMediaTypeParams()); - } - - public function testGetMediaTypeParamsWithoutHeader() - { - $request = $this->requestFactory(); - - $this->assertEquals([], $request->getMediaTypeParams()); - } - - public function testGetContentCharset() - { - $headers = new Headers([ - 'Content-Type' => ['application/json;charset=utf8'], - ]); - $request = $this->requestFactory(); - $headersProp = new ReflectionProperty($request, 'headers'); - $headersProp->setAccessible(true); - $headersProp->setValue($request, $headers); - - $this->assertEquals('utf8', $request->getContentCharset()); - } - - public function testGetContentCharsetEmpty() - { - $headers = new Headers([ - 'Content-Type' => ['application/json'], - ]); - $request = $this->requestFactory(); - $headersProp = new ReflectionProperty($request, 'headers'); - $headersProp->setAccessible(true); - $headersProp->setValue($request, $headers); - - $this->assertNull($request->getContentCharset()); - } - - public function testGetContentCharsetWithoutHeader() - { - $request = $this->requestFactory(); - - $this->assertNull($request->getContentCharset()); - } - - public function testGetContentLength() - { - $headers = new Headers([ - 'Content-Length' => '150', // <-- Note we define as a string - ]); - $request = $this->requestFactory(); - $headersProp = new ReflectionProperty($request, 'headers'); - $headersProp->setAccessible(true); - $headersProp->setValue($request, $headers); - - $this->assertEquals(150, $request->getContentLength()); - } - - public function testGetContentLengthWithoutHeader() - { - $request = $this->requestFactory(); - - $this->assertNull($request->getContentLength()); - } - - /******************************************************************************* - * Cookies - ******************************************************************************/ - - public function testGetCookieParam() - { - $shouldBe = 'john'; - - $this->assertEquals($shouldBe, $this->requestFactory()->getCookieParam('user')); - } - - public function testGetCookieParamWithDefault() - { - $shouldBe = 'bar'; - - $this->assertEquals($shouldBe, $this->requestFactory()->getCookieParam('foo', 'bar')); - } - - public function testGetCookieParams() - { - $shouldBe = [ - 'user' => 'john', - 'id' => '123', - ]; - - $this->assertEquals($shouldBe, $this->requestFactory()->getCookieParams()); - } - - public function testWithCookieParams() - { - $request = $this->requestFactory(); - $clone = $request->withCookieParams(['type' => 'framework']); - - $this->assertEquals(['type' => 'framework'], $clone->getCookieParams()); - } - - /******************************************************************************* - * Query Params - ******************************************************************************/ - - public function testGetQueryParams() - { - $this->assertEquals(['abc' => '123'], $this->requestFactory()->getQueryParams()); - } - - public function testGetQueryParamsAlreadySet() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'queryParams'); - $prop->setAccessible(true); - $prop->setValue($request, ['foo' => 'bar']); - - $this->assertEquals(['foo' => 'bar'], $request->getQueryParams()); - } - - public function testWithQueryParams() - { - $request = $this->requestFactory(); - $clone = $request->withQueryParams(['foo' => 'bar']); - $cloneUri = $clone->getUri(); - - $this->assertEquals('abc=123', $cloneUri->getQuery()); // <-- Unchanged - $this->assertEquals(['foo' => 'bar'], $clone->getQueryParams()); // <-- Changed - } - - public function testWithQueryParamsEmptyArray() - { - $request = $this->requestFactory(); - $clone = $request->withQueryParams([]); - $cloneUri = $clone->getUri(); - - $this->assertEquals('abc=123', $cloneUri->getQuery()); // <-- Unchanged - $this->assertEquals([], $clone->getQueryParams()); // <-- Changed - } - - public function testGetQueryParamsWithoutUri() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'uri'); - $prop->setAccessible(true); - $prop->setValue($request, null); - - $this->assertEquals([], $request->getQueryParams()); - } - - /******************************************************************************* - * Uploaded files - ******************************************************************************/ - - /** - * @covers Slim\Http\Request::withUploadedFiles - * @covers Slim\Http\Request::getUploadedFiles - */ - public function testWithUploadedFiles() - { - $files = [new UploadedFile('foo.txt'), new UploadedFile('bar.txt')]; - - $request = $this->requestFactory(); - $clone = $request->withUploadedFiles($files); - - $this->assertEquals([], $request->getUploadedFiles()); - $this->assertEquals($files, $clone->getUploadedFiles()); - } - - /******************************************************************************* - * Server Params - ******************************************************************************/ - - public function testGetServerParams() - { - $mockEnv = Environment::mock(["HTTP_AUTHORIZATION" => "test"]); - $request = $this->requestFactory(["HTTP_AUTHORIZATION" => "test"]); - - $serverParams = $request->getServerParams(); - foreach ($serverParams as $key => $value) { - if ($key == 'REQUEST_TIME' || $key == 'REQUEST_TIME_FLOAT') { - $this->assertGreaterThanOrEqual( - $mockEnv[$key], - $value, - sprintf("%s value of %s was less than expected value of %s", $key, $value, $mockEnv[$key]) - ); - } else { - $this->assertEquals( - $mockEnv[$key], - $value, - sprintf("%s value of %s did not equal expected value of %s", $key, $value, $mockEnv[$key]) - ); - } - } - } - - public function testGetServerParam() - { - $shouldBe = 'HTTP/1.1'; - $request = $this->requestFactory(['SERVER_PROTOCOL' => 'HTTP/1.1']); - - $this->assertEquals($shouldBe, $this->requestFactory()->getServerParam('SERVER_PROTOCOL')); - } - - public function testGetServerParamWithDefault() - { - $shouldBe = 'bar'; - - $this->assertEquals($shouldBe, $this->requestFactory()->getServerParam('HTTP_NOT_EXIST', 'bar')); - } - - /******************************************************************************* - * File Params - ******************************************************************************/ - - /******************************************************************************* - * Attributes - ******************************************************************************/ - - public function testGetAttributes() - { - $request = $this->requestFactory(); - $attrProp = new ReflectionProperty($request, 'attributes'); - $attrProp->setAccessible(true); - $attrProp->setValue($request, new Collection(['foo' => 'bar'])); - - $this->assertEquals(['foo' => 'bar'], $request->getAttributes()); - } - - public function testGetAttribute() - { - $request = $this->requestFactory(); - $attrProp = new ReflectionProperty($request, 'attributes'); - $attrProp->setAccessible(true); - $attrProp->setValue($request, new Collection(['foo' => 'bar'])); - - $this->assertEquals('bar', $request->getAttribute('foo')); - $this->assertNull($request->getAttribute('bar')); - $this->assertEquals(2, $request->getAttribute('bar', 2)); - } - - public function testWithAttribute() - { - $request = $this->requestFactory(); - $attrProp = new ReflectionProperty($request, 'attributes'); - $attrProp->setAccessible(true); - $attrProp->setValue($request, new Collection(['foo' => 'bar'])); - $clone = $request->withAttribute('test', '123'); - - $this->assertEquals('123', $clone->getAttribute('test')); - } - - public function testWithAttributes() - { - $request = $this->requestFactory(); - $attrProp = new ReflectionProperty($request, 'attributes'); - $attrProp->setAccessible(true); - $attrProp->setValue($request, new Collection(['foo' => 'bar'])); - $clone = $request->withAttributes(['test' => '123']); - - $this->assertNull($clone->getAttribute('foo')); - $this->assertEquals('123', $clone->getAttribute('test')); - } - - public function testWithoutAttribute() - { - $request = $this->requestFactory(); - $attrProp = new ReflectionProperty($request, 'attributes'); - $attrProp->setAccessible(true); - $attrProp->setValue($request, new Collection(['foo' => 'bar'])); - $clone = $request->withoutAttribute('foo'); - - $this->assertNull($clone->getAttribute('foo')); - } - - /******************************************************************************* - * Body - ******************************************************************************/ - - public function testGetParsedBodyForm() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/x-www-form-urlencoded;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('foo=bar'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - } - - public function testGetParsedBodyJson() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/json;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('{"foo":"bar"}'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - } - - public function testGetParsedBodyInvalidJson() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/json;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('{foo}bar'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertNull($request->getParsedBody()); - } - - public function testGetParsedBodySemiValidJson() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/json;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('"foo bar"'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertNull($request->getParsedBody()); - } - - public function testGetParsedBodyWithJsonStructuredSuffix() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/vnd.api+json;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('{"foo":"bar"}'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - } - - public function testGetParsedBodyXml() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/xml;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('Josh'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals('Josh', $request->getParsedBody()->name); - } - - public function testGetParsedBodyWithXmlStructuredSuffix() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/hal+xml;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('Josh'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals('Josh', $request->getParsedBody()->name); - } - - public function testGetParsedBodyXmlWithTextXMLMediaType() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'text/xml'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('Josh'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals('Josh', $request->getParsedBody()->name); - } - - /** - * Will fail if a simple_xml warning is created - */ - public function testInvalidXmlIsQuietForTextXml() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'text/xml'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('Josh'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals(null, $request->getParsedBody()); - } - - /** - * Will fail if a simple_xml warning is created - */ - public function testInvalidXmlIsQuietForApplicationXml() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/xml'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('Josh'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals(null, $request->getParsedBody()); - } - - - public function testGetParsedBodyWhenAlreadyParsed() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'bodyParsed'); - $prop->setAccessible(true); - $prop->setValue($request, ['foo' => 'bar']); - - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - } - - public function testGetParsedBodyWhenBodyDoesNotExist() - { - $request = $this->requestFactory(); - $prop = new ReflectionProperty($request, 'body'); - $prop->setAccessible(true); - $prop->setValue($request, null); - - $this->assertNull($request->getParsedBody()); - } - - public function testGetParsedBodyAfterCallReparseBody() - { - $uri = Uri::createFromString('https://example.com:443/?one=1'); - $headers = new Headers([ - 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf8', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('foo=bar'); - $body->rewind(); - $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); - - $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); - - $newBody = new RequestBody(); - $newBody->write('abc=123'); - $newBody->rewind(); - $request = $request->withBody($newBody); - $request->reparseBody(); - - $this->assertEquals(['abc' => '123'], $request->getParsedBody()); - } - - /** - * @expectedException \RuntimeException - */ - public function testGetParsedBodyAsArray() - { - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = new Headers([ - 'Content-Type' => 'application/json;charset=utf8', - ]); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $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 - }); - $request->getParsedBody(); // <-- Triggers exception - } - - public function testWithParsedBody() - { - $clone = $this->requestFactory()->withParsedBody(['xyz' => '123']); - - $this->assertEquals(['xyz' => '123'], $clone->getParsedBody()); - } - - public function testWithParsedBodyEmptyArray() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/x-www-form-urlencoded;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('foo=bar'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - - $clone = $request->withParsedBody([]); - - $this->assertEquals([], $clone->getParsedBody()); - } - - public function testWithParsedBodyNull() - { - $method = 'GET'; - $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); - $headers = new Headers(); - $headers->set('Content-Type', 'application/x-www-form-urlencoded;charset=utf8'); - $cookies = []; - $serverParams = []; - $body = new RequestBody(); - $body->write('foo=bar'); - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - - $clone = $request->withParsedBody(null); - - $this->assertNull($clone->getParsedBody()); - } - - public function testGetParsedBodyReturnsNullWhenThereIsNoBodyData() - { - $request = $this->requestFactory(['REQUEST_METHOD' => 'POST']); - - $this->assertNull($request->getParsedBody()); - } - - public function testGetParsedBodyReturnsNullWhenThereIsNoMediaTypeParserRegistered() - { - $request = $this->requestFactory([ - 'REQUEST_METHOD' => 'POST', - 'CONTENT_TYPE' => 'text/csv', - ]); - $request->getBody()->write('foo,bar,baz'); - - $this->assertNull($request->getParsedBody()); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testWithParsedBodyInvalid() - { - $this->requestFactory()->withParsedBody(2); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testWithParsedBodyInvalidFalseValue() - { - $this->requestFactory()->withParsedBody(false); - } - - /******************************************************************************* - * Parameters - ******************************************************************************/ - - public function testGetParameterFromBody() - { - $body = new RequestBody(); - $body->write('foo=bar'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals('bar', $request->getParam('foo')); - } - - public function testGetParameterFromBodyWithBodyParemeterHelper() - { - $body = new RequestBody(); - $body->write('foo=bar'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals('bar', $request->getParsedBodyParam('foo')); - } - - public function testGetParameterFromQuery() - { - $request = $this->requestFactory()->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals('123', $request->getParam('abc')); - } - - public function testGetParameterFromQueryWithQueryParemeterHelper() - { - $request = $this->requestFactory()->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals('123', $request->getQueryParam('abc')); - } - - public function testGetParameterFromBodyOverQuery() - { - $body = new RequestBody(); - $body->write('abc=xyz'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - $this->assertEquals('xyz', $request->getParam('abc')); - } - - public function testGetParameterWithDefaultFromBodyOverQuery() - { - $body = new RequestBody(); - $body->write('abc=xyz'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - $this->assertEquals('xyz', $request->getParam('abc')); - $this->assertEquals('bar', $request->getParam('foo', 'bar')); - } - - public function testGetParameters() - { - $body = new RequestBody(); - $body->write('foo=bar'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals(['abc' => '123', 'foo' => 'bar'], $request->getParams()); - } - - public function testGetParametersWithBodyPriority() - { - $body = new RequestBody(); - $body->write('foo=bar&abc=xyz'); - $body->rewind(); - $request = $this->requestFactory() - ->withBody($body) - ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); - - $this->assertEquals(['abc' => 'xyz', 'foo' => 'bar'], $request->getParams()); - } - - /******************************************************************************* - * Protocol - ******************************************************************************/ - - public function testGetProtocolVersion() - { - $env = Environment::mock(['SERVER_PROTOCOL' => 'HTTP/1.0']); - $request = Request::createFromEnvironment($env); - - $this->assertEquals('1.0', $request->getProtocolVersion()); - } -} diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php deleted file mode 100755 index 6e6cf7253..000000000 --- a/tests/Http/ResponseTest.php +++ /dev/null @@ -1,341 +0,0 @@ -assertAttributeEquals(200, 'status', $response); - $this->assertAttributeInstanceOf('\Slim\Http\Headers', 'headers', $response); - $this->assertAttributeInstanceOf('\Psr\Http\Message\StreamInterface', 'body', $response); - } - - public function testConstructorWithCustomArgs() - { - $headers = new Headers(); - $body = new Body(fopen('php://temp', 'r+')); - $response = new Response(404, $headers, $body); - - $this->assertAttributeEquals(404, 'status', $response); - $this->assertAttributeSame($headers, 'headers', $response); - $this->assertAttributeSame($body, 'body', $response); - } - - public function testDeepCopyClone() - { - $headers = new Headers(); - $body = new Body(fopen('php://temp', 'r+')); - $response = new Response(404, $headers, $body); - $clone = clone $response; - - $this->assertAttributeEquals('1.1', 'protocolVersion', $clone); - $this->assertAttributeEquals(404, 'status', $clone); - $this->assertAttributeNotSame($headers, 'headers', $clone); - $this->assertAttributeSame($body, 'body', $clone); - } - - public function testDisableSetter() - { - $response = new Response(); - $response->foo = 'bar'; - - $this->assertFalse(property_exists($response, 'foo')); - } - - /******************************************************************************* - * Status - ******************************************************************************/ - - public function testGetStatusCode() - { - $response = new Response(); - $responseStatus = new ReflectionProperty($response, 'status'); - $responseStatus->setAccessible(true); - $responseStatus->setValue($response, '404'); - - $this->assertEquals(404, $response->getStatusCode()); - } - - public function testWithStatus() - { - $response = new Response(); - $clone = $response->withStatus(302); - - $this->assertAttributeEquals(302, 'status', $clone); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testWithStatusInvalidStatusCodeThrowsException() - { - $response = new Response(); - $response->withStatus(800); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage ReasonPhrase must be a string - */ - public function testWithStatusInvalidReasonPhraseThrowsException() - { - $response = new Response(); - $response->withStatus(200, null); - } - - public function testWithStatusEmptyReasonPhrase() - { - $responseWithNoMessage = new Response(310); - - $this->assertEquals('', $responseWithNoMessage->getReasonPhrase()); - } - - public function testGetReasonPhrase() - { - $response = new Response(404); - - $this->assertEquals('Not Found', $response->getReasonPhrase()); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage ReasonPhrase must be supplied for this code - */ - public function testMustSetReasonPhraseForUnrecognisedCode() - { - $response = new Response(); - $response = $response->withStatus(199); - } - - public function testSetReasonPhraseForUnrecognisedCode() - { - $response = new Response(); - $response = $response->withStatus(199, 'Random Message'); - - $this->assertEquals('Random Message', $response->getReasonPhrase()); - } - - public function testGetCustomReasonPhrase() - { - $response = new Response(); - $clone = $response->withStatus(200, 'Custom Phrase'); - - $this->assertEquals('Custom Phrase', $clone->getReasonPhrase()); - } - - /** - * @covers Slim\Http\Response::withRedirect - */ - public function testWithRedirect() - { - $response = new Response(200); - $clone = $response->withRedirect('/foo', 301); - $cloneWithDefaultStatus = $response->withRedirect('/foo'); - $cloneWithStatusMethod = $response->withStatus(301)->withRedirect('/foo'); - - $this->assertSame(200, $response->getStatusCode()); - $this->assertFalse($response->hasHeader('Location')); - - $this->assertSame(301, $clone->getStatusCode()); - $this->assertTrue($clone->hasHeader('Location')); - $this->assertEquals('/foo', $clone->getHeaderLine('Location')); - - $this->assertSame(302, $cloneWithDefaultStatus->getStatusCode()); - $this->assertTrue($cloneWithDefaultStatus->hasHeader('Location')); - $this->assertEquals('/foo', $cloneWithDefaultStatus->getHeaderLine('Location')); - - $this->assertSame(301, $cloneWithStatusMethod->getStatusCode()); - $this->assertTrue($cloneWithStatusMethod->hasHeader('Location')); - $this->assertEquals('/foo', $cloneWithStatusMethod->getHeaderLine('Location')); - } - - /******************************************************************************* - * Behaviors - ******************************************************************************/ - - public function testIsEmpty() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 204); - - $this->assertTrue($response->isEmpty()); - } - - public function testIsInformational() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 100); - - $this->assertTrue($response->isInformational()); - } - - public function testIsOk() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 200); - - $this->assertTrue($response->isOk()); - } - - public function testIsSuccessful() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 201); - - $this->assertTrue($response->isSuccessful()); - } - - public function testIsRedirect() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 302); - - $this->assertTrue($response->isRedirect()); - } - - public function testIsRedirection() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 308); - - $this->assertTrue($response->isRedirection()); - } - - public function testIsForbidden() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 403); - - $this->assertTrue($response->isForbidden()); - } - - public function testIsNotFound() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 404); - - $this->assertTrue($response->isNotFound()); - } - - public function testIsClientError() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 400); - - $this->assertTrue($response->isClientError()); - } - - public function testIsServerError() - { - $response = new Response(); - $prop = new ReflectionProperty($response, 'status'); - $prop->setAccessible(true); - $prop->setValue($response, 503); - - $this->assertTrue($response->isServerError()); - } - - public function testToString() - { - $output = 'HTTP/1.1 404 Not Found' . Response::EOL . - 'X-Foo: Bar' . Response::EOL . Response::EOL . - 'Where am I?'; - $this->expectOutputString($output); - $response = new Response(); - $response = $response->withStatus(404)->withHeader('X-Foo', 'Bar')->write('Where am I?'); - - echo $response; - } - - public function testWithJson() - { - $data = ['foo' => 'bar1&bar2']; - - $originalResponse = new Response(); - $response = $originalResponse->withJson($data, 201); - - $this->assertNotEquals($response->getStatusCode(), $originalResponse->getStatusCode()); - $this->assertEquals(201, $response->getStatusCode()); - $this->assertEquals('application/json;charset=utf-8', $response->getHeaderLine('Content-Type')); - - $body = $response->getBody(); - $body->rewind(); - $dataJson = $body->getContents(); //json_decode($body->getContents(), true); - - $originalBody = $originalResponse->getBody(); - $originalBody->rewind(); - $originalContents = $originalBody->getContents(); - - // test the original body hasn't be replaced - $this->assertNotEquals($dataJson, $originalContents); - - $this->assertEquals('{"foo":"bar1&bar2"}', $dataJson); - $this->assertEquals($data['foo'], json_decode($dataJson, true)['foo']); - - // Test encoding option - $response = $response->withJson($data, 200, JSON_HEX_AMP); - - $body = $response->getBody(); - $body->rewind(); - $dataJson = $body->getContents(); - - $this->assertEquals('{"foo":"bar1\u0026bar2"}', $dataJson); - $this->assertEquals($data['foo'], json_decode($dataJson, true)['foo']); - - $response = $response->withStatus(201)->withJson([]); - $this->assertEquals($response->getStatusCode(), 201); - } - - /** - * @expectedException \RuntimeException - */ - public function testWithInvalidJsonThrowsException() - { - $data = ['foo' => 'bar'.chr(233)]; - $this->assertEquals('bar'.chr(233), $data['foo']); - - $response = new Response(); - $response->withJson($data, 200); - - // Safety net: this assertion should not occur, since the RuntimeException - // must have been caught earlier by the test framework - $this->assertFalse(true); - } -} diff --git a/tests/Http/StreamTest.php b/tests/Http/StreamTest.php deleted file mode 100644 index 12104f149..000000000 --- a/tests/Http/StreamTest.php +++ /dev/null @@ -1,159 +0,0 @@ -pipeFh != null) { - stream_get_contents($this->pipeFh); // prevent broken pipe error message - } - } - - /** - * @covers Slim\Http\Stream::isPipe - */ - public function testIsPipe() - { - $this->openPipeStream(); - - $this->assertTrue($this->pipeStream->isPipe()); - - $this->pipeStream->detach(); - $this->assertFalse($this->pipeStream->isPipe()); - - $fhFile = fopen(__FILE__, 'r'); - $fileStream = new Stream($fhFile); - $this->assertFalse($fileStream->isPipe()); - } - - /** - * @covers Slim\Http\Stream::isReadable - */ - public function testIsPipeReadable() - { - $this->openPipeStream(); - - $this->assertTrue($this->pipeStream->isReadable()); - } - - /** - * @covers Slim\Http\Stream::isSeekable - */ - public function testPipeIsNotSeekable() - { - $this->openPipeStream(); - - $this->assertFalse($this->pipeStream->isSeekable()); - } - - /** - * @covers Slim\Http\Stream::seek - * @expectedException \RuntimeException - */ - public function testCannotSeekPipe() - { - $this->openPipeStream(); - - $this->pipeStream->seek(0); - } - - /** - * @covers Slim\Http\Stream::tell - * @expectedException \RuntimeException - */ - public function testCannotTellPipe() - { - $this->openPipeStream(); - - $this->pipeStream->tell(); - } - - /** - * @covers Slim\Http\Stream::rewind - * @expectedException \RuntimeException - */ - public function testCannotRewindPipe() - { - $this->openPipeStream(); - - $this->pipeStream->rewind(); - } - - /** - * @covers Slim\Http\Stream::getSize - */ - public function testPipeGetSizeYieldsNull() - { - $this->openPipeStream(); - - $this->assertNull($this->pipeStream->getSize()); - } - - /** - * @covers Slim\Http\Stream::close - */ - public function testClosePipe() - { - $this->openPipeStream(); - - stream_get_contents($this->pipeFh); // prevent broken pipe error message - $this->pipeStream->close(); - $this->pipeFh = null; - - $this->assertFalse($this->pipeStream->isPipe()); - } - - /** - * @covers Slim\Http\Stream::__toString - */ - public function testPipeToString() - { - $this->openPipeStream(); - - $this->assertSame('', (string) $this->pipeStream); - } - - /** - * @covers Slim\Http\Stream::getContents - */ - - public function testPipeGetContents() - { - $this->openPipeStream(); - - $contents = trim($this->pipeStream->getContents()); - $this->assertSame('12', $contents); - } - - /** - * Opens the pipe stream - * - * @see StreamTest::pipeStream - */ - private function openPipeStream() - { - $this->pipeFh = popen('echo 12', 'r'); - $this->pipeStream = new Stream($this->pipeFh); - } -} diff --git a/tests/Http/UploadedFilesTest.php b/tests/Http/UploadedFilesTest.php deleted file mode 100644 index e8ca325ea..000000000 --- a/tests/Http/UploadedFilesTest.php +++ /dev/null @@ -1,485 +0,0 @@ -assertEquals($expected, $uploadedFile); - } - - /** - * @param array $input The input array to parse. - * - * @dataProvider providerCreateFromEnvironment - */ - public function testCreateFromEnvironmentFromUserData(array $input) - { - //If slim.files provided - it will return what was provided - $userData['slim.files'] = $input; - - $uploadedFile = UploadedFile::createFromEnvironment(Environment::mock($userData)); - $this->assertEquals($input, $uploadedFile); - } - - public function testCreateFromEnvironmentWithoutFile() - { - unset($_FILES); - - $uploadedFile = UploadedFile::createFromEnvironment(Environment::mock()); - $this->assertEquals([], $uploadedFile); - } - - /** - * @return UploadedFile - */ - public function testConstructor() - { - $attr = [ - 'tmp_name' => self::$filename, - 'name' => 'my-avatar.txt', - 'size' => 8, - 'type' => 'text/plain', - 'error' => 0, - ]; - - $uploadedFile = new UploadedFile( - $attr['tmp_name'], - $attr['name'], - $attr['type'], - $attr['size'], - $attr['error'], - false - ); - - - $this->assertEquals($attr['name'], $uploadedFile->getClientFilename()); - $this->assertEquals($attr['type'], $uploadedFile->getClientMediaType()); - $this->assertEquals($attr['size'], $uploadedFile->getSize()); - $this->assertEquals($attr['error'], $uploadedFile->getError()); - - return $uploadedFile; - } - - /** - * @depends testConstructor - * @param UploadedFile $uploadedFile - * @return UploadedFile - */ - public function testGetStream(UploadedFile $uploadedFile) - { - $stream = $uploadedFile->getStream(); - $this->assertEquals(true, $uploadedFile->getStream() instanceof Stream); - $stream->close(); - - return $uploadedFile; - } - - /** - * @depends testConstructor - * @param UploadedFile $uploadedFile - * @expectedException \InvalidArgumentException - */ - public function testMoveToNotWritable(UploadedFile $uploadedFile) - { - $tempName = uniqid('file-'); - $path = 'some_random_dir' . DIRECTORY_SEPARATOR . $tempName; - $uploadedFile->moveTo($path); - } - - /** - * @depends testConstructor - * @param UploadedFile $uploadedFile - * @return UploadedFile - */ - public function testMoveTo(UploadedFile $uploadedFile) - { - $tempName = uniqid('file-'); - $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempName; - $uploadedFile->moveTo($path); - - $this->assertFileExists($path); - - unlink($path); - - return $uploadedFile; - } - - /** - * @depends testMoveTo - * @param UploadedFile $uploadedFile - * @expectedException \RuntimeException - */ - public function testMoveToCannotBeDoneTwice(UploadedFile $uploadedFile) - { - $tempName = uniqid('file-'); - $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempName; - $uploadedFile->moveTo($path); - $this->assertFileExists($path); - unlink($path); - - $uploadedFile->moveTo($path); - } - - /** - * This test must run after testMoveTo - * - * @depends testConstructor - * @param UploadedFile $uploadedFile - * @expectedException \RuntimeException - */ - public function testMoveToAgain(UploadedFile $uploadedFile) - { - $tempName = uniqid('file-'); - $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempName; - $uploadedFile->moveTo($path); - } - - /** - * This test must run after testMoveTo - * - * @depends testConstructor - * @param UploadedFile $uploadedFile - * @expectedException \RuntimeException - */ - public function testMovedStream($uploadedFile) - { - $uploadedFile->getStream(); - } - - public function testMoveToStream() - { - $uploadedFile = $this->generateNewTmpFile(); - $uploadedFile->moveTo('php://temp'); - } - - public function providerCreateFromEnvironment() - { - return [ - // no nest: - [ - // $_FILES array - [ - 'avatar' => [ - 'tmp_name' => 'phpUxcOty', - 'name' => 'my-avatar.png', - 'size' => 90996, - 'type' => 'image/png', - 'error' => 0, - ], - ], - // expected format of array - [ - 'avatar' => new UploadedFile('phpUxcOty', 'my-avatar.png', 'image/png', 90996, UPLOAD_ERR_OK, true) - ] - ], - // no nest, with error: - [ - // $_FILES array - [ - 'avatar' => [ - 'tmp_name' => 'phpUxcOty', - 'name' => 'my-avatar.png', - 'size' => 90996, - 'type' => 'image/png', - 'error' => 7, - ], - ], - // expected format of array - [ - 'avatar' => new UploadedFile( - 'phpUxcOty', - 'my-avatar.png', - 'image/png', - 90996, - UPLOAD_ERR_CANT_WRITE, - true - ) - ] - ], - - // array of files: - [ - // $_FILES array - [ - 'avatars' => [ - 'tmp_name' => [ - 0 => __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 1 => __DIR__ . DIRECTORY_SEPARATOR . 'file1.html', - ], - 'name' => [ - 0 => 'file0.txt', - 1 => 'file1.html', - ], - 'type' => [ - 0 => 'text/plain', - 1 => 'text/html', - ], - 'error' => [ - 0 => 0, - 1 => 0 - ], - 'size' => [ - 0 => 0, - 1 => 0 - ] - ], - ], - // expected format of array - [ - 'avatars' => [ - 0 => new UploadedFile( - __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 'file0.txt', - 'text/plain', - null, - UPLOAD_ERR_OK, - true - ), - 1 => new UploadedFile( - __DIR__ . DIRECTORY_SEPARATOR . 'file1.html', - 'file1.html', - 'text/html', - null, - UPLOAD_ERR_OK, - true - ), - ], - ] - ], - // array of files as multidimensional array: - [ - // $_FILES array - [ - [ - 'avatars' => [ - 'tmp_name' => [ - 0 => __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 1 => __DIR__ . DIRECTORY_SEPARATOR . 'file1.html', - ], - 'name' => [ - 0 => 'file0.txt', - 1 => 'file1.html', - ], - 'type' => [ - 0 => 'text/plain', - 1 => 'text/html', - ], - 'size' => [ - 0 => 0, - 1 => 0, - ], - ], - ], - ], - // expected format of array - [ - 0 => - [ - 'avatars' => - [ - 'tmp_name' => [], - 'name' => [], - 'type' => [], - 'size' => [], - ], - ], - ], - ], - // single nested file: - [ - // $_FILES array - [ - 'details' => [ - 'tmp_name' => [ - 'avatar' => __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - ], - 'name' => [ - 'avatar' => 'file0.txt', - ], - 'type' => [ - 'avatar' => 'text/plain', - ], - 'error' => [ - 'avatar' => 0, - ], - 'size' => [ - 'avatar' => 0, - ], - ], - ], - // expected format of array - [ - 'details' => [ - 'avatar' => new UploadedFile( - __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 'file0.txt', - 'text/plain', - null, - UPLOAD_ERR_OK, - true - ), - ], - ] - ], - // nested array of files: - [ - [ - 'files' => [ - 'tmp_name' => [ - 'details' => [ - 'avatar' => [ - 0 => __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 1 => __DIR__ . DIRECTORY_SEPARATOR . 'file1.html', - ], - ], - ], - 'name' => [ - 'details' => [ - 'avatar' => [ - 0 => 'file0.txt', - 1 => 'file1.html', - ], - ], - ], - 'type' => [ - 'details' => [ - 'avatar' => [ - 0 => 'text/plain', - 1 => 'text/html', - ], - ], - ], - 'error' => [ - 'details' => [ - 'avatar' => [ - 0 => 0, - 1 => 0 - ], - ], - ], - 'size' => [ - 'details' => [ - 'avatar' => [ - 0 => 0, - 1 => 0 - ], - ], - ], - ], - ], - // expected format of array - [ - 'files' => [ - 'details' => [ - 'avatar' => [ - 0 => new UploadedFile( - __DIR__ . DIRECTORY_SEPARATOR . 'file0.txt', - 'file0.txt', - 'text/plain', - null, - UPLOAD_ERR_OK, - true - ), - 1 => new UploadedFile( - __DIR__ . DIRECTORY_SEPARATOR . 'file1.html', - 'file1.html', - 'text/html', - null, - UPLOAD_ERR_OK, - true - ), - ], - ], - ], - ] - ], - ]; - } - - /** - * @param array $mockEnv An array representing a mock environment. - * - * @return Request - */ - public function requestFactory(array $mockEnv) - { - $env = Environment::mock(); - - $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); - $headers = Headers::createFromEnvironment($env); - $cookies = []; - $serverParams = $env->all(); - $body = new RequestBody(); - $uploadedFiles = UploadedFile::createFromEnvironment($env); - $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); - - return $request; - } -} diff --git a/tests/Http/UriTest.php b/tests/Http/UriTest.php deleted file mode 100644 index 6680dac93..000000000 --- a/tests/Http/UriTest.php +++ /dev/null @@ -1,609 +0,0 @@ -assertEquals('https', $this->uriFactory()->getScheme()); - } - - public function testWithScheme() - { - $uri = $this->uriFactory()->withScheme('http'); - - $this->assertAttributeEquals('http', 'scheme', $uri); - } - - public function testWithSchemeRemovesSuffix() - { - $uri = $this->uriFactory()->withScheme('http://'); - - $this->assertAttributeEquals('http', 'scheme', $uri); - } - - public function testWithSchemeEmpty() - { - $uri = $this->uriFactory()->withScheme(''); - - $this->assertAttributeEquals('', 'scheme', $uri); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri scheme must be one of: "", "https", "http" - */ - public function testWithSchemeInvalid() - { - $this->uriFactory()->withScheme('ftp'); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri scheme must be a string - */ - public function testWithSchemeInvalidType() - { - $this->uriFactory()->withScheme([]); - } - - /******************************************************************************** - * Authority - *******************************************************************************/ - - public function testGetAuthorityWithUsernameAndPassword() - { - $this->assertEquals('josh:sekrit@example.com', $this->uriFactory()->getAuthority()); - } - - public function testGetAuthorityWithUsername() - { - $scheme = 'https'; - $user = 'josh'; - $password = ''; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 443; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('josh@example.com', $uri->getAuthority()); - } - - public function testGetAuthority() - { - $scheme = 'https'; - $user = ''; - $password = ''; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 443; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('example.com', $uri->getAuthority()); - } - - public function testGetAuthorityWithNonStandardPort() - { - $scheme = 'https'; - $user = ''; - $password = ''; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 400; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('example.com:400', $uri->getAuthority()); - } - - public function testGetUserInfoWithUsernameAndPassword() - { - $scheme = 'https'; - $user = 'josh'; - $password = 'sekrit'; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 443; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('josh:sekrit', $uri->getUserInfo()); - } - - public function testGetUserInfoWithUsername() - { - $scheme = 'https'; - $user = 'josh'; - $password = ''; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 443; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('josh', $uri->getUserInfo()); - } - - public function testGetUserInfoNone() - { - $scheme = 'https'; - $user = ''; - $password = ''; - $host = 'example.com'; - $path = '/foo/bar'; - $port = 443; - $query = 'abc=123'; - $fragment = 'section3'; - $uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password); - - $this->assertEquals('', $uri->getUserInfo()); - } - - public function testWithUserInfo() - { - $uri = $this->uriFactory()->withUserInfo('bob', 'pass'); - - $this->assertAttributeEquals('bob', 'user', $uri); - $this->assertAttributeEquals('pass', 'password', $uri); - } - - public function testWithUserInfoRemovesPassword() - { - $uri = $this->uriFactory()->withUserInfo('bob'); - - $this->assertAttributeEquals('bob', 'user', $uri); - $this->assertAttributeEquals('', 'password', $uri); - } - - public function testGetHost() - { - $this->assertEquals('example.com', $this->uriFactory()->getHost()); - } - - public function testWithHost() - { - $uri = $this->uriFactory()->withHost('slimframework.com'); - - $this->assertAttributeEquals('slimframework.com', 'host', $uri); - } - - public function testGetPortWithSchemeAndNonDefaultPort() - { - $uri = new Uri('https', 'www.example.com', 4000); - - $this->assertEquals(4000, $uri->getPort()); - } - - public function testGetPortWithSchemeAndDefaultPort() - { - $uriHppt = new Uri('http', 'www.example.com', 80); - $uriHppts = new Uri('https', 'www.example.com', 443); - - $this->assertNull($uriHppt->getPort()); - $this->assertNull($uriHppts->getPort()); - } - - public function testGetPortWithoutSchemeAndPort() - { - $uri = new Uri('', 'www.example.com'); - - $this->assertNull($uri->getPort()); - } - - public function testGetPortWithSchemeWithoutPort() - { - $uri = new Uri('http', 'www.example.com'); - - $this->assertNull($uri->getPort()); - } - - public function testWithPort() - { - $uri = $this->uriFactory()->withPort(8000); - - $this->assertAttributeEquals(8000, 'port', $uri); - } - - public function testWithPortNull() - { - $uri = $this->uriFactory()->withPort(null); - - $this->assertAttributeEquals(null, 'port', $uri); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testWithPortInvalidInt() - { - $this->uriFactory()->withPort(70000); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testWithPortInvalidString() - { - $this->uriFactory()->withPort('Foo'); - } - - /******************************************************************************** - * Path - *******************************************************************************/ - - public function testGetPath() - { - $this->assertEquals('/foo/bar', $this->uriFactory()->getPath()); - } - - public function testWithPath() - { - $uri = $this->uriFactory()->withPath('/new'); - - $this->assertAttributeEquals('/new', 'path', $uri); - } - - public function testWithPathWithoutPrefix() - { - $uri = $this->uriFactory()->withPath('new'); - - $this->assertAttributeEquals('new', 'path', $uri); - } - - public function testWithPathEmptyValue() - { - $uri = $this->uriFactory()->withPath(''); - - $this->assertAttributeEquals('', 'path', $uri); - } - - public function testWithPathUrlEncodesInput() - { - $uri = $this->uriFactory()->withPath('/includes?/new'); - - $this->assertAttributeEquals('/includes%3F/new', 'path', $uri); - } - - public function testWithPathDoesNotDoubleEncodeInput() - { - $uri = $this->uriFactory()->withPath('/include%25s/new'); - - $this->assertAttributeEquals('/include%25s/new', 'path', $uri); - } - - /** - * @covers Slim\Http\Uri::withPath - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri path must be a string - */ - public function testWithPathInvalidType() - { - $this->uriFactory()->withPath(['foo']); - } - - /******************************************************************************** - * Query - *******************************************************************************/ - - public function testGetQuery() - { - $this->assertEquals('abc=123', $this->uriFactory()->getQuery()); - } - - public function testWithQuery() - { - $uri = $this->uriFactory()->withQuery('xyz=123'); - - $this->assertAttributeEquals('xyz=123', 'query', $uri); - } - - public function testWithQueryRemovesPrefix() - { - $uri = $this->uriFactory()->withQuery('?xyz=123'); - - $this->assertAttributeEquals('xyz=123', 'query', $uri); - } - - public function testWithQueryEmpty() - { - $uri = $this->uriFactory()->withQuery(''); - - $this->assertAttributeEquals('', 'query', $uri); - } - - public function testFilterQuery() - { - $uri = $this->uriFactory()->withQuery('?foobar=%match'); - - $this->assertAttributeEquals('foobar=%25match', 'query', $uri); - } - - /** - * @covers Slim\Http\Uri::withQuery - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri query must be a string - */ - public function testWithQueryInvalidType() - { - $this->uriFactory()->withQuery(['foo']); - } - - /******************************************************************************** - * Fragment - *******************************************************************************/ - - public function testGetFragment() - { - $this->assertEquals('section3', $this->uriFactory()->getFragment()); - } - - public function testWithFragment() - { - $uri = $this->uriFactory()->withFragment('other-fragment'); - - $this->assertAttributeEquals('other-fragment', 'fragment', $uri); - } - - public function testWithFragmentRemovesPrefix() - { - $uri = $this->uriFactory()->withFragment('#other-fragment'); - - $this->assertAttributeEquals('other-fragment', 'fragment', $uri); - } - - public function testWithFragmentEmpty() - { - $uri = $this->uriFactory()->withFragment(''); - - $this->assertAttributeEquals('', 'fragment', $uri); - } - - /** - * @covers Slim\Http\Uri::withFragment - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri fragment must be a string - */ - public function testWithFragmentInvalidType() - { - $this->uriFactory()->withFragment(['foo']); - } - - /******************************************************************************** - * Helpers - *******************************************************************************/ - - public function testToString() - { - $uri = $this->uriFactory(); - - $this->assertEquals('https://josh:sekrit@example.com/foo/bar?abc=123#section3', (string) $uri); - - $uri = $uri->withPath('bar'); - $this->assertEquals('https://josh:sekrit@example.com/bar?abc=123#section3', (string) $uri); - - $uri = $uri->withPath('/bar'); - $this->assertEquals('https://josh:sekrit@example.com/bar?abc=123#section3', (string) $uri); - - // ensure that a Uri with just a base path correctly converts to a string - // (This occurs via createFromEnvironment when index.php is in a subdirectory) - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/foo/index.php', - 'REQUEST_URI' => '/foo/', - 'HTTP_HOST' => 'example.com', - ]); - $uri = Uri::createFromEnvironment($environment); - $this->assertEquals('http://example.com/', (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() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo/bar', - 'PHP_AUTH_USER' => 'josh', - 'PHP_AUTH_PW' => 'sekrit', - 'QUERY_STRING' => 'abc=123', - 'HTTP_HOST' => 'example.com:8080', - 'SERVER_PORT' => 8080, - ]); - - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('josh:sekrit', $uri->getUserInfo()); - $this->assertEquals('example.com', $uri->getHost()); - $this->assertEquals('8080', $uri->getPort()); - $this->assertEquals('/foo/bar', $uri->getPath()); - $this->assertEquals('abc=123', $uri->getQuery()); - $this->assertEquals('', $uri->getFragment()); - } - - public function testCreateEnvironmentWithIPv6Host() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo/bar', - 'PHP_AUTH_USER' => 'josh', - 'PHP_AUTH_PW' => 'sekrit', - 'QUERY_STRING' => 'abc=123', - 'HTTP_HOST' => '[2001:db8::1]:8080', - 'REMOTE_ADDR' => '2001:db8::1', - 'SERVER_PORT' => 8080, - ]); - - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('josh:sekrit', $uri->getUserInfo()); - $this->assertEquals('[2001:db8::1]', $uri->getHost()); - $this->assertEquals('8080', $uri->getPort()); - $this->assertEquals('/foo/bar', $uri->getPath()); - $this->assertEquals('abc=123', $uri->getQuery()); - $this->assertEquals('', $uri->getFragment()); - } - - public function testCreateEnvironmentWithBasePathContainingSpace() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => "/f'oo bar/index.php", - 'REQUEST_URI' => "/f%27oo%20bar/baz", - ]); - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('baz', $uri->getPath()); - } - - public function testGetBaseUrl() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/foo/index.php', - 'REQUEST_URI' => '/foo/bar', - 'QUERY_STRING' => 'abc=123', - 'HTTP_HOST' => 'example.com:80', - 'SERVER_PORT' => 80 - ]); - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('http://example.com', $uri->getBaseUrl()); - } - - public function testGetBaseUrlWithNoBasePath() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo/bar', - 'QUERY_STRING' => 'abc=123', - 'HTTP_HOST' => 'example.com:80', - 'SERVER_PORT' => 80 - ]); - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('http://example.com', $uri->getBaseUrl()); - } - - public function testGetBaseUrlWithAuthority() - { - $environment = Environment::mock([ - 'SCRIPT_NAME' => '/foo/index.php', - 'REQUEST_URI' => '/foo/bar', - 'PHP_AUTH_USER' => 'josh', - 'PHP_AUTH_PW' => 'sekrit', - 'QUERY_STRING' => 'abc=123', - 'HTTP_HOST' => 'example.com:8080', - 'SERVER_PORT' => 8080 - ]); - $uri = Uri::createFromEnvironment($environment); - - $this->assertEquals('http://josh:sekrit@example.com:8080', $uri->getBaseUrl()); - } - - /** - * @covers Slim\Http\Uri::createFromEnvironment - * @ticket 1380 - */ - public function testWithPathWhenBaseRootIsEmpty() - { - $environment = \Slim\Http\Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/bar', - ]); - $uri = \Slim\Http\Uri::createFromEnvironment($environment); - - $this->assertEquals('http://localhost/test', (string) $uri->withPath('test')); - } - - /** - * When the URL is /foo/index.php/bar/baz, we need the baseURL to be - * /foo/index.php so that routing works correctly. - * - * @ticket 1639 as a fix to 1590 broke this. - */ - public function testRequestURIContainsIndexDotPhp() - { - $uri = Uri::createFromEnvironment( - Environment::mock( - [ - 'SCRIPT_NAME' => '/foo/index.php', - 'REQUEST_URI' => '/foo/index.php/bar/baz', - ] - ) - ); - $this->assertSame('bar/baz', $uri->getPath()); - } - - public function testRequestURICanContainParams() - { - $uri = Uri::createFromEnvironment( - Environment::mock( - [ - 'REQUEST_URI' => '/foo?abc=123', - ] - ) - ); - $this->assertEquals('abc=123', $uri->getQuery()); - } -} From 39ba21ccc17c9ec394b7207de08a28e1ff9ce517 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sun, 7 May 2017 17:03:52 +0100 Subject: [PATCH 2/4] Update dock block for new location of HeadersInterface --- tests/Mocks/MessageStub.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Mocks/MessageStub.php b/tests/Mocks/MessageStub.php index 526b4013e..056bd9f39 100644 --- a/tests/Mocks/MessageStub.php +++ b/tests/Mocks/MessageStub.php @@ -25,7 +25,7 @@ class MessageStub extends Message /** * Headers * - * @var \Slim\Interfaces\Http\HeadersInterface + * @var \Slim\Http\HeadersInterface */ public $headers; From 3eab9c5093cd522659bc29a0bace67cdf76dfab1 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sun, 7 May 2017 17:04:54 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Use=20Http=E2=80=99s=20createFromGlobals()?= =?UTF-8?q?=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This replaces createFromEnvironment() and also means that we no longer need the `environment` key in the container. --- Slim/Container.php | 2 - Slim/DefaultServicesProvider.php | 15 +-- tests/AppTest.php | 174 +++++++++++++++---------------- tests/ContainerTest.php | 2 +- tests/RouteTest.php | 20 ++-- 5 files changed, 99 insertions(+), 114 deletions(-) diff --git a/Slim/Container.php b/Slim/Container.php index ac0d9179c..8c7804e59 100644 --- a/Slim/Container.php +++ b/Slim/Container.php @@ -21,7 +21,6 @@ * with these service keys configured and ready for use: * * - settings: an array or instance of \ArrayAccess - * - environment: an instance of \Slim\Interfaces\Http\EnvironmentInterface * - request: an instance of \Psr\Http\Message\ServerRequestInterface * - response: an instance of \Psr\Http\Message\ResponseInterface * - router: an instance of \Slim\Interfaces\RouterInterface @@ -32,7 +31,6 @@ * - callableResolver: an instance of \Slim\Interfaces\CallableResolverInterface * * @property-read array settings - * @property-read \Slim\Interfaces\Http\EnvironmentInterface environment * @property-read \Psr\Http\Message\ServerRequestInterface request * @property-read \Psr\Http\Message\ResponseInterface response * @property-read \Slim\Interfaces\RouterInterface router diff --git a/Slim/DefaultServicesProvider.php b/Slim/DefaultServicesProvider.php index 007205aa4..36475de87 100644 --- a/Slim/DefaultServicesProvider.php +++ b/Slim/DefaultServicesProvider.php @@ -20,7 +20,6 @@ use Slim\Http\Request; use Slim\Http\Response; use Slim\Interfaces\CallableResolverInterface; -use Slim\Interfaces\Http\EnvironmentInterface; use Slim\Interfaces\InvocationStrategyInterface; use Slim\Interfaces\RouterInterface; @@ -36,18 +35,6 @@ class DefaultServicesProvider */ public function register($container) { - if (!isset($container['environment'])) { - /** - * This service MUST return a shared instance - * of \Slim\Interfaces\Http\EnvironmentInterface. - * - * @return EnvironmentInterface - */ - $container['environment'] = function () { - return new Environment($_SERVER); - }; - } - if (!isset($container['request'])) { /** * PSR-7 Request object @@ -57,7 +44,7 @@ public function register($container) * @return ServerRequestInterface */ $container['request'] = function ($container) { - return Request::createFromEnvironment($container->get('environment')); + return Request::createFromGlobals($_SERVER); }; } diff --git a/tests/AppTest.php b/tests/AppTest.php index ab7772bf9..a4007d755 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -837,10 +837,10 @@ public function testAddMiddlewareOnRoute() 'REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -880,10 +880,10 @@ public function testAddMiddlewareOnRouteGroup() 'REQUEST_URI' => '/foo/', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -924,10 +924,10 @@ public function testAddMiddlewareOnTwoRouteGroup() 'REQUEST_URI' => '/foo/baz/', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -974,10 +974,10 @@ public function testAddMiddlewareOnRouteAndOnTwoRouteGroup() 'REQUEST_URI' => '/foo/baz/', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1008,10 +1008,10 @@ public function testInvokeReturnMethodNotAllowed() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'POST', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1048,10 +1048,10 @@ public function testInvokeWithMatchingRoute() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1076,10 +1076,10 @@ public function testInvokeWithMatchingRouteWithSetArgument() 'REQUEST_URI' => '/foo/bar', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1104,10 +1104,10 @@ public function testInvokeWithMatchingRouteWithSetArguments() 'REQUEST_URI' => '/foo/bar', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1132,10 +1132,10 @@ public function testInvokeWithMatchingRouteWithNamedParameter() 'REQUEST_URI' => '/foo/test!', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1161,10 +1161,10 @@ public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgS 'REQUEST_URI' => '/foo/test!', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1189,10 +1189,10 @@ public function testInvokeWithMatchingRouteWithNamedParameterOverwritesSetArgume 'REQUEST_URI' => '/foo/test!', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1219,10 +1219,10 @@ public function testInvokeWithoutMatchingRoute() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1247,10 +1247,10 @@ public function testInvokeWithPimpleCallable() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1287,10 +1287,10 @@ public function testInvokeWithPimpleUndefinedCallable() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1317,10 +1317,10 @@ public function testInvokeWithPimpleCallableViaMagicMethod() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1363,10 +1363,10 @@ function handle($req, $res) 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1390,10 +1390,10 @@ public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArguments() 'REQUEST_URI' => '/foo/rob', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $req = $req->withAttribute("one", 1); @@ -1419,10 +1419,10 @@ public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRe 'REQUEST_URI' => '/foo/rob', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $req = $req->withAttribute("one", 1); @@ -1447,10 +1447,10 @@ public function testRun() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1484,10 +1484,10 @@ public function testRespond() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1516,10 +1516,10 @@ public function testRespondWithHeaderNotSent() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1547,10 +1547,10 @@ public function testRespondNoContent() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1616,10 +1616,10 @@ public function testRespondWithPaddedStreamFilterOutput() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1669,10 +1669,10 @@ public function testResponseWithStreamReadYieldingLessBytesThanAsked() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Mocks\SmallChunksStream(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = (new Response())->withBody($body); @@ -1696,10 +1696,10 @@ public function testExceptionErrorHandlerDoesNotDisplayErrorDetails() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1735,10 +1735,10 @@ public function testExceptionPhpErrorHandlerDoesNotDisplayErrorDetails() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1771,10 +1771,10 @@ public function appFactory() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -1917,10 +1917,10 @@ public function testExceptionErrorHandlerDisplaysErrorDetails() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); @@ -2094,10 +2094,10 @@ public function testContainerSetToRoute() 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', ]); - $uri = Uri::createFromEnvironment($env); - $headers = Headers::createFromEnvironment($env); + $uri = Uri::createFromGlobals($env); + $headers = Headers::createFromGlobals($env); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new RequestBody(); $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $res = new Response(); diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 32981858c..298eefd2a 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -29,7 +29,7 @@ public function setUp() */ public function testGet() { - $this->assertInstanceOf('\Slim\Http\Environment', $this->container->get('environment')); + $this->assertInstanceOf('\Slim\Handlers\NotFound', $this->container->get('notFoundHandler')); } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index ac1d38f81..a285e5b8c 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -204,7 +204,7 @@ public function testAddMiddlewareAsString() 'user' => 'john', 'id' => '123', ]; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); @@ -228,7 +228,7 @@ public function testControllerInContainer() $uri = Uri::createFromString('https://example.com:80'); $body = new Body(fopen('php://temp', 'r+')); - $request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), $body); + $request = new Request('GET', $uri, new Headers(), [], Environment::mock(), $body); CallableTest::$CalledCount = 0; @@ -253,7 +253,7 @@ public function testInvokeWhenReturningAResponse() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -279,7 +279,7 @@ public function testInvokeWhenEchoingOutput() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -305,7 +305,7 @@ public function testInvokeWhenReturningAString() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -332,7 +332,7 @@ public function testInvokeWhenPrependingOutputBuffer() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -357,7 +357,7 @@ public function testInvokeWithException() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -384,7 +384,7 @@ public function testInvokeWhenDisablingOutputBuffer() $uri = Uri::createFromString('https://example.com:80'); $headers = new Headers(); $cookies = []; - $serverParams = $env->all(); + $serverParams = $env; $body = new Body(fopen('php://temp', 'r+')); $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); $response = new Response; @@ -415,7 +415,7 @@ public function testInvokeDeferredCallable() $uri = Uri::createFromString('https://example.com:80'); $body = new Body(fopen('php://temp', 'r+')); - $request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), $body); + $request = new Request('GET', $uri, new Headers(), [], Environment::mock(), $body); $result = $route->callMiddlewareStack($request, new Response); @@ -453,7 +453,7 @@ public function testChangingCallable() $uri = Uri::createFromString('https://example.com:80'); $body = new Body(fopen('php://temp', 'r+')); - $request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), $body); + $request = new Request('GET', $uri, new Headers(), [], Environment::mock(), $body); $result = $route->callMiddlewareStack($request, new Response); From a7362be5fc8bea20ef89279fc751bb9209c1534c Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Mon, 8 May 2017 07:46:04 +0100 Subject: [PATCH 4/4] Require Slim-Http v0.2 or higher --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 823832660..34babf9a8 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ ], "require": { "php": ">=5.6.0", + "slim/http": ">=0.2", "pimple/pimple": "^3.0", "psr/http-message": "^1.0", "nikic/fast-route": "^1.0",