├── .circleci └── config.yml ├── .editorconfig ├── .gitignore ├── .scrutinizer.yml ├── LICENSE ├── README.md ├── composer.json ├── phpcs.xml.dist ├── phpunit.xml.dist ├── psalm.xml.dist ├── renovate.json ├── src ├── AbstractHeader.php ├── HeaderAccessControlAllowCredentials.php ├── HeaderAccessControlAllowHeaders.php ├── HeaderAccessControlAllowMethods.php ├── HeaderAccessControlAllowOrigin.php ├── HeaderAccessControlExposeHeaders.php ├── HeaderAccessControlMaxAge.php ├── HeaderAge.php ├── HeaderAllow.php ├── HeaderCacheControl.php ├── HeaderClearSiteData.php ├── HeaderConnection.php ├── HeaderContentDisposition.php ├── HeaderContentEncoding.php ├── HeaderContentLanguage.php ├── HeaderContentLength.php ├── HeaderContentLocation.php ├── HeaderContentMD5.php ├── HeaderContentRange.php ├── HeaderContentSecurityPolicy.php ├── HeaderContentSecurityPolicyReportOnly.php ├── HeaderContentType.php ├── HeaderCookie.php ├── HeaderCustom.php ├── HeaderDate.php ├── HeaderEtag.php ├── HeaderExpires.php ├── HeaderKeepAlive.php ├── HeaderLastModified.php ├── HeaderLink.php ├── HeaderLocation.php ├── HeaderRefresh.php ├── HeaderRetryAfter.php ├── HeaderSetCookie.php ├── HeaderSunset.php ├── HeaderTrailer.php ├── HeaderTransferEncoding.php ├── HeaderVary.php ├── HeaderWWWAuthenticate.php └── HeaderWarning.php └── tests ├── AbstractHeaderTest.php ├── HeaderAccessControlAllowCredentialsTest.php ├── HeaderAccessControlAllowHeadersTest.php ├── HeaderAccessControlAllowMethodsTest.php ├── HeaderAccessControlAllowOriginTest.php ├── HeaderAccessControlExposeHeadersTest.php ├── HeaderAccessControlMaxAgeTest.php ├── HeaderAgeTest.php ├── HeaderAllowTest.php ├── HeaderCacheControlTest.php ├── HeaderClearSiteDataTest.php ├── HeaderConnectionTest.php ├── HeaderContentDispositionTest.php ├── HeaderContentEncodingTest.php ├── HeaderContentLanguageTest.php ├── HeaderContentLengthTest.php ├── HeaderContentLocationTest.php ├── HeaderContentMD5Test.php ├── HeaderContentRangeTest.php ├── HeaderContentSecurityPolicyReportOnlyTest.php ├── HeaderContentSecurityPolicyTest.php ├── HeaderContentTypeTest.php ├── HeaderCookieTest.php ├── HeaderCustomTest.php ├── HeaderDateTest.php ├── HeaderEtagTest.php ├── HeaderExpiresTest.php ├── HeaderKeepAliveTest.php ├── HeaderLastModifiedTest.php ├── HeaderLinkTest.php ├── HeaderLocationTest.php ├── HeaderRefreshTest.php ├── HeaderRetryAfterTest.php ├── HeaderSetCookieTest.php ├── HeaderSunsetTest.php ├── HeaderTrailerTest.php ├── HeaderTransferEncodingTest.php ├── HeaderVaryTest.php ├── HeaderWWWAuthenticateTest.php └── HeaderWarningTest.php /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # PHP CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-php/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | php74: 8 | docker: 9 | - image: cimg/php:7.4 10 | steps: 11 | - checkout 12 | - run: php -v 13 | - run: composer install --no-interaction 14 | - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text 15 | php80: 16 | docker: 17 | - image: cimg/php:8.0 18 | steps: 19 | - checkout 20 | - run: php -v 21 | - run: composer install --no-interaction 22 | - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text 23 | php81: 24 | docker: 25 | - image: cimg/php:8.1 26 | steps: 27 | - checkout 28 | - run: php -v 29 | - run: composer install --no-interaction 30 | - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text 31 | workflows: 32 | version: 2 33 | build: 34 | jobs: 35 | - php74 36 | - php80 37 | - php81 38 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # More info at: 2 | # https://editorconfig.org/ 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | end_of_line = lf 9 | indent_style = space 10 | indent_size = 4 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | 14 | [*.yml] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.php_cs.cache 2 | /.phpunit.result.cache 3 | /composer.lock 4 | /coverage.xml 5 | /phpbench.json 6 | /phpcs.xml 7 | /phpunit.xml 8 | /psalm.xml 9 | /vendor/ 10 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | build: 2 | image: default-bionic 3 | nodes: 4 | analysis: 5 | environment: 6 | php: 8.1 7 | tests: 8 | override: 9 | - php-scrutinizer-run 10 | coverage: 11 | environment: 12 | php: 8.1 13 | tests: 14 | override: 15 | - command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml 16 | coverage: 17 | file: coverage.xml 18 | format: clover 19 | php80: 20 | environment: 21 | php: 8.0 22 | tests: 23 | override: 24 | - command: php vendor/bin/phpunit 25 | php74: 26 | environment: 27 | php: 7.4 28 | tests: 29 | override: 30 | - command: php vendor/bin/phpunit 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Anatoly Nekhay 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ⚠️ This package was **moved** to the [http-message](https://github.com/sunrise-php/http-message) package and is abandoned. 2 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sunrise/http-header-kit", 3 | "homepage": "https://github.com/sunrise-php/http-header-kit", 4 | "description": "HTTP header kit for PHP 7.1+ compatible with PSR-7 and not only", 5 | "license": "MIT", 6 | "keywords": [ 7 | "fenric", 8 | "sunrise", 9 | "http", 10 | "message", 11 | "header" 12 | ], 13 | "authors": [ 14 | { 15 | "name": "Anatoly Fenric", 16 | "email": "afenric@gmail.com", 17 | "homepage": "https://github.com/fenric" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=7.4", 22 | "sunrise/http-header": "^3.0" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "~9.5.0", 26 | "sunrise/coding-standard": "~1.0.0", 27 | "sunrise/uri": "~1.2.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Sunrise\\Http\\Header\\": "src/" 32 | } 33 | }, 34 | "scripts": { 35 | "test": [ 36 | "phpcs", 37 | "psalm", 38 | "XDEBUG_MODE=coverage phpunit --coverage-text --colors=always" 39 | ], 40 | "build": [ 41 | "phpdoc -d src/ -t phpdoc/", 42 | "XDEBUG_MODE=coverage phpunit --coverage-html coverage/" 43 | ] 44 | }, 45 | "abandoned": "sunrise/http-message" 46 | } 47 | -------------------------------------------------------------------------------- /phpcs.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src 6 | tests 7 | 8 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | ./src 9 | 10 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /psalm.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /src/AbstractHeader.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use ArrayIterator; 18 | use DateTime; 19 | use DateTimeImmutable; 20 | use DateTimeInterface; 21 | use DateTimeZone; 22 | use InvalidArgumentException; 23 | use Traversable; 24 | 25 | /** 26 | * Import functions 27 | */ 28 | use function gettype; 29 | use function is_int; 30 | use function is_string; 31 | use function preg_match; 32 | use function sprintf; 33 | 34 | /** 35 | * AbstractHeader 36 | */ 37 | abstract class AbstractHeader implements HeaderInterface 38 | { 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | final public function __toString() : string 44 | { 45 | return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue()); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | final public function getIterator() : Traversable 52 | { 53 | return new ArrayIterator([$this->getFieldName(), $this->getFieldValue()]); 54 | } 55 | 56 | /** 57 | * Gets Regular Expression for a token validation 58 | * 59 | * @return string 60 | */ 61 | protected function getTokenValidationRegularExpression() : string 62 | { 63 | return HeaderInterface::RFC7230_TOKEN; 64 | } 65 | 66 | /** 67 | * Gets Regular Expression for a parameter name validation 68 | * 69 | * @return string 70 | */ 71 | protected function getParameterNameValidationRegularExpression() : string 72 | { 73 | return HeaderInterface::RFC7230_TOKEN; 74 | } 75 | 76 | /** 77 | * Gets Regular Expression for a parameter value validation 78 | * 79 | * @return string 80 | */ 81 | protected function getParameterValueValidationRegularExpression() : string 82 | { 83 | return HeaderInterface::RFC7230_QUOTED_STRING; 84 | } 85 | 86 | /** 87 | * Checks if the given string is token 88 | * 89 | * @param string $token 90 | * 91 | * @return bool 92 | */ 93 | final protected function isToken(string $token) : bool 94 | { 95 | return preg_match($this->getTokenValidationRegularExpression(), $token) === 1; 96 | } 97 | 98 | /** 99 | * Validates the given token(s) 100 | * 101 | * @param string ...$tokens 102 | * 103 | * @return void 104 | * 105 | * @throws InvalidArgumentException 106 | * If one of the tokens isn't valid. 107 | */ 108 | final protected function validateToken(string ...$tokens) : void 109 | { 110 | foreach ($tokens as $token) { 111 | if (!$this->isToken($token)) { 112 | throw new InvalidArgumentException(sprintf( 113 | 'The value "%2$s" for the header "%1$s" is not valid', 114 | $this->getFieldName(), 115 | $token 116 | )); 117 | } 118 | } 119 | } 120 | 121 | /** 122 | * Validates the given quoted string(s) 123 | * 124 | * @param string ...$quotedStrings 125 | * 126 | * @return void 127 | * 128 | * @throws InvalidArgumentException 129 | * If one of the quoted strings isn't valid. 130 | */ 131 | final protected function validateQuotedString(string ...$quotedStrings) : void 132 | { 133 | foreach ($quotedStrings as $quotedString) { 134 | if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $quotedString)) { 135 | throw new InvalidArgumentException(sprintf( 136 | 'The value "%2$s" for the header "%1$s" is not valid', 137 | $this->getFieldName(), 138 | $quotedString 139 | )); 140 | } 141 | } 142 | } 143 | 144 | /** 145 | * Validates and normalizes the given parameters 146 | * 147 | * @param array $parameters 148 | * 149 | * @return array 150 | * 151 | * @throws InvalidArgumentException 152 | * If one of the parameters isn't valid. 153 | */ 154 | final protected function validateParameters(array $parameters) : array 155 | { 156 | foreach ($parameters as $name => $value) { 157 | // e.g. Cache-Control: max-age=31536000 158 | if (is_int($value)) { 159 | $parameters[$name] = $value = (string) $value; 160 | } 161 | 162 | if (!is_string($name) || !preg_match($this->getParameterNameValidationRegularExpression(), $name)) { 163 | throw new InvalidArgumentException(sprintf( 164 | 'The parameter-name "%2$s" for the header "%1$s" is not valid', 165 | $this->getFieldName(), 166 | (is_string($name) ? $name : ('<' . gettype($name) . '>')) 167 | )); 168 | } 169 | 170 | /** @psalm-suppress MixedArgument */ 171 | if (!is_string($value) || !preg_match($this->getParameterValueValidationRegularExpression(), $value)) { 172 | throw new InvalidArgumentException(sprintf( 173 | 'The parameter-value "%2$s" for the header "%1$s" is not valid', 174 | $this->getFieldName(), 175 | (is_string($value) ? $value : ('<' . gettype($value) . '>')) 176 | )); 177 | } 178 | } 179 | 180 | return $parameters; 181 | } 182 | 183 | /** 184 | * Validates the given header field-name 185 | * 186 | * @param mixed $fieldName 187 | * 188 | * @return void 189 | * 190 | * @throws InvalidArgumentException 191 | * If the header field-name isn't valid. 192 | */ 193 | final protected function validateFieldName($fieldName) : void 194 | { 195 | if (!is_string($fieldName)) { 196 | throw new InvalidArgumentException('Header field-name must be a string'); 197 | } 198 | 199 | if (!preg_match(HeaderInterface::RFC7230_TOKEN, $fieldName)) { 200 | throw new InvalidArgumentException('Header field-name is invalid'); 201 | } 202 | } 203 | 204 | /** 205 | * Validates the given header field-value 206 | * 207 | * @param mixed $fieldValue 208 | * 209 | * @return void 210 | * 211 | * @throws InvalidArgumentException 212 | * If the header field-value isn't valid. 213 | */ 214 | final protected function validateFieldValue($fieldValue) : void 215 | { 216 | if (!is_string($fieldValue)) { 217 | throw new InvalidArgumentException('Header field-value must be a string'); 218 | } 219 | 220 | // a header's field value can be empty... 221 | if ($fieldValue === '') { 222 | return; 223 | } 224 | 225 | if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $fieldValue)) { 226 | throw new InvalidArgumentException('Header field-value is invalid'); 227 | } 228 | } 229 | 230 | /** 231 | * Normalizes the given date-time object 232 | * 233 | * @param T $dateTime 234 | * 235 | * @return T 236 | * 237 | * @template T as DateTimeInterface 238 | */ 239 | final protected function normalizeDateTime(DateTimeInterface $dateTime) : DateTimeInterface 240 | { 241 | if ($dateTime instanceof DateTime) { 242 | return (clone $dateTime)->setTimezone(new DateTimeZone('GMT')); 243 | } 244 | 245 | if ($dateTime instanceof DateTimeImmutable) { 246 | return $dateTime->setTimezone(new DateTimeZone('GMT')); 247 | } 248 | 249 | // @codeCoverageIgnoreStart 250 | return $dateTime; 251 | // @codeCoverageIgnoreEnd 252 | } 253 | 254 | /** 255 | * Formats the given date-time object 256 | * 257 | * @param DateTimeInterface $dateTime 258 | * 259 | * @return string 260 | * 261 | * @link https://datatracker.ietf.org/doc/html/rfc822 262 | */ 263 | final protected function formatDateTime(DateTimeInterface $dateTime) : string 264 | { 265 | return $this->normalizeDateTime($dateTime)->format(DateTime::RFC822); 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /src/HeaderAccessControlAllowCredentials.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials 16 | */ 17 | class HeaderAccessControlAllowCredentials extends AbstractHeader 18 | { 19 | 20 | /** 21 | * {@inheritdoc} 22 | */ 23 | public function getFieldName() : string 24 | { 25 | return 'Access-Control-Allow-Credentials'; 26 | } 27 | 28 | /** 29 | * {@inheritdoc} 30 | */ 31 | public function getFieldValue() : string 32 | { 33 | return 'true'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/HeaderAccessControlAllowHeaders.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers 21 | */ 22 | class HeaderAccessControlAllowHeaders extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var list 27 | */ 28 | protected $headers; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param string ...$headers 34 | */ 35 | public function __construct(string ...$headers) 36 | { 37 | /** @var list $headers */ 38 | 39 | $this->validateToken(...$headers); 40 | 41 | $this->headers = $headers; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Access-Control-Allow-Headers'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | return implode(', ', $this->headers); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/HeaderAccessControlAllowMethods.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function strtoupper; 19 | 20 | /** 21 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods 22 | */ 23 | class HeaderAccessControlAllowMethods extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var list 28 | */ 29 | protected $methods = []; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param string ...$methods 35 | */ 36 | public function __construct(string ...$methods) 37 | { 38 | /** @var list $methods */ 39 | 40 | $this->validateToken(...$methods); 41 | 42 | // normalize the list of methods... 43 | foreach ($methods as $method) { 44 | $this->methods[] = strtoupper($method); 45 | } 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getFieldName() : string 52 | { 53 | return 'Access-Control-Allow-Methods'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function getFieldValue() : string 60 | { 61 | return implode(', ', $this->methods); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/HeaderAccessControlAllowOrigin.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | use Psr\Http\Message\UriInterface; 19 | 20 | /** 21 | * Import functions 22 | */ 23 | use function sprintf; 24 | 25 | /** 26 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin 27 | */ 28 | class HeaderAccessControlAllowOrigin extends AbstractHeader 29 | { 30 | 31 | /** 32 | * @var UriInterface|null 33 | */ 34 | protected $uri; 35 | 36 | /** 37 | * Constructor of the class 38 | * 39 | * @param UriInterface|null $uri 40 | */ 41 | public function __construct(?UriInterface $uri) 42 | { 43 | if (isset($uri)) { 44 | $this->validateUri($uri); 45 | } 46 | 47 | $this->uri = $uri; 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function getFieldName() : string 54 | { 55 | return 'Access-Control-Allow-Origin'; 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function getFieldValue() : string 62 | { 63 | if ($this->uri === null) { 64 | return '*'; 65 | } 66 | 67 | $value = $this->uri->getScheme() . ':'; 68 | $value .= '//' . $this->uri->getHost(); 69 | 70 | $port = $this->uri->getPort(); 71 | if (isset($port)) { 72 | $value .= ':' . $port; 73 | } 74 | 75 | return $value; 76 | } 77 | 78 | /** 79 | * Validates the given URI 80 | * 81 | * @param UriInterface $uri 82 | * 83 | * @return void 84 | * 85 | * @throws InvalidArgumentException 86 | * If the URI isn't valid. 87 | */ 88 | private function validateUri(UriInterface $uri) : void 89 | { 90 | if ($uri->getScheme() === '' || $uri->getHost() === '') { 91 | throw new InvalidArgumentException(sprintf( 92 | 'The URI "%2$s" for the header "%1$s" is not valid', 93 | $this->getFieldName(), 94 | $uri->__toString() 95 | )); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/HeaderAccessControlExposeHeaders.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers 21 | */ 22 | class HeaderAccessControlExposeHeaders extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var list 27 | */ 28 | protected $headers; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param string ...$headers 34 | */ 35 | public function __construct(string ...$headers) 36 | { 37 | /** @var list $headers */ 38 | 39 | $this->validateToken(...$headers); 40 | 41 | $this->headers = $headers; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Access-Control-Expose-Headers'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | return implode(', ', $this->headers); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/HeaderAccessControlMaxAge.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * Import functions 21 | */ 22 | use function sprintf; 23 | 24 | /** 25 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age 26 | */ 27 | class HeaderAccessControlMaxAge extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var int 32 | */ 33 | protected $value; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param int $value 39 | */ 40 | public function __construct(int $value) 41 | { 42 | $this->validateValue($value); 43 | 44 | $this->value = $value; 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getFieldName() : string 51 | { 52 | return 'Access-Control-Max-Age'; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldValue() : string 59 | { 60 | return sprintf('%d', $this->value); 61 | } 62 | 63 | /** 64 | * Validates the given value 65 | * 66 | * @param int $value 67 | * 68 | * @return void 69 | * 70 | * @throws InvalidArgumentException 71 | * If the value isn't valid. 72 | */ 73 | private function validateValue(int $value) : void 74 | { 75 | if (! ($value === -1 || $value >= 1)) { 76 | throw new InvalidArgumentException(sprintf( 77 | 'The value "%2$d" for the header "%1$s" is not valid', 78 | $this->getFieldName(), 79 | $value 80 | )); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/HeaderAge.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * Import functions 21 | */ 22 | use function sprintf; 23 | 24 | /** 25 | * @link https://tools.ietf.org/html/rfc2616#section-14.6 26 | */ 27 | class HeaderAge extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var int 32 | */ 33 | protected $value; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param int $value 39 | */ 40 | public function __construct(int $value) 41 | { 42 | $this->validateValue($value); 43 | 44 | $this->value = $value; 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getFieldName() : string 51 | { 52 | return 'Age'; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldValue() : string 59 | { 60 | return sprintf('%d', $this->value); 61 | } 62 | 63 | /** 64 | * Validates the given value 65 | * 66 | * @param int $value 67 | * 68 | * @return void 69 | * 70 | * @throws InvalidArgumentException 71 | * If the value isn't valid. 72 | */ 73 | private function validateValue(int $value) : void 74 | { 75 | if (! ($value >= 0)) { 76 | throw new InvalidArgumentException(sprintf( 77 | 'The value "%2$d" for the header "%1$s" is not valid', 78 | $this->getFieldName(), 79 | $value 80 | )); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/HeaderAllow.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function strtoupper; 19 | 20 | /** 21 | * @link https://tools.ietf.org/html/rfc2616#section-14.7 22 | */ 23 | class HeaderAllow extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var list 28 | */ 29 | protected $methods = []; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param string ...$methods 35 | */ 36 | public function __construct(string ...$methods) 37 | { 38 | /** @var list $methods */ 39 | 40 | $this->validateToken(...$methods); 41 | 42 | // normalize the list of methods... 43 | foreach ($methods as $method) { 44 | $this->methods[] = strtoupper($method); 45 | } 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getFieldName() : string 52 | { 53 | return 'Allow'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function getFieldValue() : string 60 | { 61 | return implode(', ', $this->methods); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/HeaderCacheControl.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function sprintf; 19 | 20 | /** 21 | * @link https://tools.ietf.org/html/rfc2616#section-14.9 22 | */ 23 | class HeaderCacheControl extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var array 28 | */ 29 | protected $parameters; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param array $parameters 35 | */ 36 | public function __construct(array $parameters) 37 | { 38 | /** @var array */ 39 | $parameters = $this->validateParameters($parameters); 40 | 41 | $this->parameters = $parameters; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Cache-Control'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | $segments = []; 58 | foreach ($this->parameters as $name => $value) { 59 | // the construction isn't valid... 60 | if ($value === '') { 61 | $segments[] = $name; 62 | continue; 63 | } 64 | 65 | $format = $this->isToken($value) ? '%s=%s' : '%s="%s"'; 66 | 67 | $segments[] = sprintf($format, $name, $value); 68 | } 69 | 70 | return implode(', ', $segments); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/HeaderClearSiteData.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function sprintf; 19 | 20 | /** 21 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data 22 | */ 23 | class HeaderClearSiteData extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var list 28 | */ 29 | protected $directives; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param string ...$directives 35 | */ 36 | public function __construct(string ...$directives) 37 | { 38 | /** @var list $directives */ 39 | 40 | $this->validateQuotedString(...$directives); 41 | 42 | $this->directives = $directives; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFieldName() : string 49 | { 50 | return 'Clear-Site-Data'; 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function getFieldValue() : string 57 | { 58 | $segments = []; 59 | foreach ($this->directives as $directive) { 60 | $segments[] = sprintf('"%s"', $directive); 61 | } 62 | 63 | return implode(', ', $segments); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/HeaderConnection.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * @link https://tools.ietf.org/html/rfc2616#section-14.10 16 | */ 17 | class HeaderConnection extends AbstractHeader 18 | { 19 | 20 | /** 21 | * @var string 22 | */ 23 | public const CONNECTION_CLOSE = 'close'; 24 | 25 | /** 26 | * @var string 27 | */ 28 | public const CONNECTION_KEEP_ALIVE = 'keep-alive'; 29 | 30 | /** 31 | * @var string 32 | */ 33 | protected $value; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param string $value 39 | */ 40 | public function __construct(string $value) 41 | { 42 | $this->validateToken($value); 43 | 44 | $this->value = $value; 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getFieldName() : string 51 | { 52 | return 'Connection'; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldValue() : string 59 | { 60 | return $this->value; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/HeaderContentDisposition.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function sprintf; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-19.5.1 21 | */ 22 | class HeaderContentDisposition extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var string 27 | */ 28 | protected $type; 29 | 30 | /** 31 | * @var array 32 | */ 33 | protected $parameters; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param string $type 39 | * @param array $parameters 40 | */ 41 | public function __construct(string $type, array $parameters = []) 42 | { 43 | $this->validateToken($type); 44 | 45 | /** @var array */ 46 | $parameters = $this->validateParameters($parameters); 47 | 48 | $this->type = $type; 49 | $this->parameters = $parameters; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldName() : string 56 | { 57 | return 'Content-Disposition'; 58 | } 59 | 60 | /** 61 | * {@inheritdoc} 62 | */ 63 | public function getFieldValue() : string 64 | { 65 | $v = $this->type; 66 | foreach ($this->parameters as $name => $value) { 67 | $v .= sprintf('; %s="%s"', $name, $value); 68 | } 69 | 70 | return $v; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/HeaderContentEncoding.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.11 21 | */ 22 | class HeaderContentEncoding extends AbstractHeader 23 | { 24 | 25 | /** 26 | * Directives 27 | * 28 | * @var string 29 | */ 30 | public const GZIP = 'gzip'; 31 | public const COMPRESS = 'compress'; 32 | public const DEFLATE = 'deflate'; 33 | public const BR = 'br'; 34 | 35 | /** 36 | * @var list 37 | */ 38 | protected $directives; 39 | 40 | /** 41 | * Constructor of the class 42 | * 43 | * @param string ...$directives 44 | */ 45 | public function __construct(string ...$directives) 46 | { 47 | /** @var list $directives */ 48 | 49 | $this->validateToken(...$directives); 50 | 51 | $this->directives = $directives; 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function getFieldName() : string 58 | { 59 | return 'Content-Encoding'; 60 | } 61 | 62 | /** 63 | * {@inheritdoc} 64 | */ 65 | public function getFieldValue() : string 66 | { 67 | return implode(', ', $this->directives); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/HeaderContentLanguage.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.12 21 | */ 22 | class HeaderContentLanguage extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var list 27 | */ 28 | protected $languages; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param string ...$languages 34 | */ 35 | public function __construct(string ...$languages) 36 | { 37 | /** @var list $languages */ 38 | 39 | $this->validateToken(...$languages); 40 | 41 | $this->languages = $languages; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Content-Language'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | return implode(', ', $this->languages); 58 | } 59 | 60 | /** 61 | * {@inheritdoc} 62 | * 63 | * @link https://tools.ietf.org/html/rfc2616#section-3.10 64 | */ 65 | protected function getTokenValidationRegularExpression() : string 66 | { 67 | return '/^[a-zA-Z]{1,8}(?:\-[a-zA-Z]{1,8})?$/'; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/HeaderContentLength.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * Import functions 21 | */ 22 | use function sprintf; 23 | 24 | /** 25 | * @link https://tools.ietf.org/html/rfc2616#section-14.13 26 | */ 27 | class HeaderContentLength extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var int 32 | */ 33 | protected $value; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param int $value 39 | */ 40 | public function __construct(int $value) 41 | { 42 | $this->validateValue($value); 43 | 44 | $this->value = $value; 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getFieldName() : string 51 | { 52 | return 'Content-Length'; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldValue() : string 59 | { 60 | return sprintf('%d', $this->value); 61 | } 62 | 63 | /** 64 | * Validates the given value 65 | * 66 | * @param int $value 67 | * 68 | * @return void 69 | * 70 | * @throws InvalidArgumentException 71 | * If the value isn't valid. 72 | */ 73 | private function validateValue(int $value) : void 74 | { 75 | if (! ($value >= 0)) { 76 | throw new InvalidArgumentException(sprintf( 77 | 'The value "%2$d" for the header "%1$s" is not valid', 78 | $this->getFieldName(), 79 | $value 80 | )); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/HeaderContentLocation.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use Psr\Http\Message\UriInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.14 21 | */ 22 | class HeaderContentLocation extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var UriInterface 27 | */ 28 | protected $uri; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param UriInterface $uri 34 | */ 35 | public function __construct(UriInterface $uri) 36 | { 37 | $this->uri = $uri; 38 | } 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | public function getFieldName() : string 44 | { 45 | return 'Content-Location'; 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getFieldValue() : string 52 | { 53 | return $this->uri->__toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/HeaderContentMD5.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * @link https://tools.ietf.org/html/rfc2616#section-14.15 16 | */ 17 | class HeaderContentMD5 extends AbstractHeader 18 | { 19 | 20 | /** 21 | * @var string 22 | */ 23 | protected $value; 24 | 25 | /** 26 | * Constructor of the class 27 | * 28 | * @param string $value 29 | */ 30 | public function __construct(string $value) 31 | { 32 | $this->validateToken($value); 33 | 34 | $this->value = $value; 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function getFieldName() : string 41 | { 42 | return 'Content-MD5'; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFieldValue() : string 49 | { 50 | return $this->value; 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | * 56 | * @link https://tools.ietf.org/html/rfc2045#section-6.8 57 | */ 58 | protected function getTokenValidationRegularExpression() : string 59 | { 60 | return '/^[A-Za-z0-9\+\/]+=*$/'; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/HeaderContentRange.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * Import functions 21 | */ 22 | use function sprintf; 23 | 24 | /** 25 | * @link https://tools.ietf.org/html/rfc2616#section-14.16 26 | */ 27 | class HeaderContentRange extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var int 32 | */ 33 | protected $firstBytePosition; 34 | 35 | /** 36 | * @var int 37 | */ 38 | protected $lastBytePosition; 39 | 40 | /** 41 | * @var int 42 | */ 43 | protected $instanceLength; 44 | 45 | /** 46 | * Constructor of the class 47 | * 48 | * @param int $firstBytePosition 49 | * @param int $lastBytePosition 50 | * @param int $instanceLength 51 | */ 52 | public function __construct(int $firstBytePosition, int $lastBytePosition, int $instanceLength) 53 | { 54 | $this->validateRange($firstBytePosition, $lastBytePosition, $instanceLength); 55 | 56 | $this->firstBytePosition = $firstBytePosition; 57 | $this->lastBytePosition = $lastBytePosition; 58 | $this->instanceLength = $instanceLength; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function getFieldName() : string 65 | { 66 | return 'Content-Range'; 67 | } 68 | 69 | /** 70 | * {@inheritdoc} 71 | */ 72 | public function getFieldValue() : string 73 | { 74 | return sprintf( 75 | 'bytes %d-%d/%d', 76 | $this->firstBytePosition, 77 | $this->lastBytePosition, 78 | $this->instanceLength 79 | ); 80 | } 81 | 82 | /** 83 | * Validates the given range 84 | * 85 | * @param int $firstBytePosition 86 | * @param int $lastBytePosition 87 | * @param int $instanceLength 88 | * 89 | * @return void 90 | * 91 | * @throws InvalidArgumentException 92 | * If the range isn't valid. 93 | */ 94 | private function validateRange(int $firstBytePosition, int $lastBytePosition, int $instanceLength) : void 95 | { 96 | if (! ($firstBytePosition <= $lastBytePosition)) { 97 | throw new InvalidArgumentException( 98 | 'The "first-byte-pos" value of the content range ' . 99 | 'must be less than or equal to the "last-byte-pos" value' 100 | ); 101 | } 102 | 103 | if (! ($lastBytePosition < $instanceLength)) { 104 | throw new InvalidArgumentException( 105 | 'The "last-byte-pos" value of the content range ' . 106 | 'must be less than the "instance-length" value' 107 | ); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/HeaderContentSecurityPolicy.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function sprintf; 19 | 20 | /** 21 | * @link https://www.w3.org/TR/CSP3/#csp-header 22 | */ 23 | class HeaderContentSecurityPolicy extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var array 28 | */ 29 | protected $parameters; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param array $parameters 35 | */ 36 | public function __construct(array $parameters = []) 37 | { 38 | /** @var array */ 39 | $parameters = $this->validateParameters($parameters); 40 | 41 | $this->parameters = $parameters; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Content-Security-Policy'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | $directives = []; 58 | foreach ($this->parameters as $directive => $value) { 59 | // the directive can be without value... 60 | // e.g. sandbox, upgrade-insecure-requests, etc. 61 | if ($value === '') { 62 | $directives[] = $directive; 63 | continue; 64 | } 65 | 66 | $directives[] = sprintf('%s %s', $directive, $value); 67 | } 68 | 69 | return implode('; ', $directives); 70 | } 71 | 72 | /** 73 | * {@inheritdoc} 74 | * 75 | * @link https://www.w3.org/TR/CSP3/#framework-directives 76 | */ 77 | protected function getParameterNameValidationRegularExpression() : string 78 | { 79 | return '/^[0-9A-Za-z\-]+$/'; 80 | } 81 | 82 | /** 83 | * {@inheritdoc} 84 | * 85 | * @link https://www.w3.org/TR/CSP3/#framework-directives 86 | */ 87 | protected function getParameterValueValidationRegularExpression() : string 88 | { 89 | return '/^[\x09\x20-\x2B\x2D-\x3A\x3C-\x7E]*$/'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/HeaderContentSecurityPolicyReportOnly.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * @link https://www.w3.org/TR/CSP3/#cspro-header 16 | */ 17 | class HeaderContentSecurityPolicyReportOnly extends HeaderContentSecurityPolicy 18 | { 19 | 20 | /** 21 | * {@inheritdoc} 22 | */ 23 | public function getFieldName() : string 24 | { 25 | return 'Content-Security-Policy-Report-Only'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/HeaderContentType.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function sprintf; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 21 | */ 22 | class HeaderContentType extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var string 27 | */ 28 | protected $type; 29 | 30 | /** 31 | * @var array 32 | */ 33 | protected $parameters; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param string $type 39 | * @param array $parameters 40 | */ 41 | public function __construct(string $type, array $parameters = []) 42 | { 43 | $this->validateToken($type); 44 | 45 | /** @var array */ 46 | $parameters = $this->validateParameters($parameters); 47 | 48 | $this->type = $type; 49 | $this->parameters = $parameters; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldName() : string 56 | { 57 | return 'Content-Type'; 58 | } 59 | 60 | /** 61 | * {@inheritdoc} 62 | */ 63 | public function getFieldValue() : string 64 | { 65 | $v = $this->type; 66 | foreach ($this->parameters as $name => $value) { 67 | $v .= sprintf('; %s="%s"', $name, $value); 68 | } 69 | 70 | return $v; 71 | } 72 | 73 | /** 74 | * {@inheritdoc} 75 | * 76 | * @link https://tools.ietf.org/html/rfc6838#section-4.2 77 | */ 78 | protected function getTokenValidationRegularExpression() : string 79 | { 80 | return '/^[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*(?:\/[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*)?$/'; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/HeaderCookie.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function http_build_query; 18 | 19 | /** 20 | * Import constants 21 | */ 22 | use const PHP_QUERY_RFC3986; 23 | 24 | /** 25 | * @link https://tools.ietf.org/html/rfc6265.html#section-5.4 26 | */ 27 | class HeaderCookie extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var array 32 | */ 33 | protected $value; 34 | 35 | /** 36 | * Constructor of the class 37 | * 38 | * @param array $value 39 | */ 40 | public function __construct(array $value = []) 41 | { 42 | $this->value = $value; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFieldName() : string 49 | { 50 | return 'Cookie'; 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function getFieldValue() : string 57 | { 58 | return http_build_query($this->value, '', '; ', PHP_QUERY_RFC3986); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/HeaderCustom.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | 19 | /** 20 | * Custom header 21 | */ 22 | class HeaderCustom extends AbstractHeader 23 | { 24 | 25 | /** 26 | * The header field-name 27 | * 28 | * @var string 29 | * 30 | * @readonly 31 | */ 32 | private $fieldName; 33 | 34 | /** 35 | * The header field-value 36 | * 37 | * @var string 38 | * 39 | * @readonly 40 | */ 41 | private $fieldValue; 42 | 43 | /** 44 | * Constructor of the class 45 | * 46 | * @param string $fieldName 47 | * @param string $fieldValue 48 | * 49 | * @throws InvalidArgumentException 50 | * If the header field name of value isn't valid. 51 | */ 52 | public function __construct($fieldName, $fieldValue) 53 | { 54 | $this->validateFieldName($fieldName); 55 | $this->validateFieldValue($fieldValue); 56 | 57 | $this->fieldName = $fieldName; 58 | $this->fieldValue = $fieldValue; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function getFieldName() : string 65 | { 66 | return $this->fieldName; 67 | } 68 | 69 | /** 70 | * {@inheritdoc} 71 | */ 72 | public function getFieldValue() : string 73 | { 74 | return $this->fieldValue; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/HeaderDate.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.18 21 | * @link https://tools.ietf.org/html/rfc822#section-5 22 | */ 23 | class HeaderDate extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var DateTimeInterface 28 | */ 29 | protected $timestamp; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param DateTimeInterface $timestamp 35 | */ 36 | public function __construct(DateTimeInterface $timestamp) 37 | { 38 | $this->timestamp = $timestamp; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getFieldName() : string 45 | { 46 | return 'Date'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function getFieldValue() : string 53 | { 54 | return $this->formatDateTime($this->timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HeaderEtag.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function sprintf; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.19 21 | */ 22 | class HeaderEtag extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var string 27 | */ 28 | protected $value; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param string $value 34 | */ 35 | public function __construct(string $value) 36 | { 37 | $this->validateQuotedString($value); 38 | 39 | $this->value = $value; 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public function getFieldName() : string 46 | { 47 | return 'ETag'; 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function getFieldValue() : string 54 | { 55 | return sprintf('"%s"', $this->value); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/HeaderExpires.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.21 21 | * @link https://tools.ietf.org/html/rfc822#section-5 22 | */ 23 | class HeaderExpires extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var DateTimeInterface 28 | */ 29 | protected $timestamp; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param DateTimeInterface $timestamp 35 | */ 36 | public function __construct(DateTimeInterface $timestamp) 37 | { 38 | $this->timestamp = $timestamp; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getFieldName() : string 45 | { 46 | return 'Expires'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function getFieldValue() : string 53 | { 54 | return $this->formatDateTime($this->timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HeaderKeepAlive.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function sprintf; 19 | 20 | /** 21 | * @link https://tools.ietf.org/html/rfc2068#section-19.7.1.1 22 | */ 23 | class HeaderKeepAlive extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var array 28 | */ 29 | protected $parameters; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param array $parameters 35 | */ 36 | public function __construct(array $parameters = []) 37 | { 38 | /** @var array */ 39 | $parameters = $this->validateParameters($parameters); 40 | 41 | $this->parameters = $parameters; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Keep-Alive'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | $segments = []; 58 | foreach ($this->parameters as $name => $value) { 59 | // the construction isn't valid... 60 | if ($value === '') { 61 | $segments[] = $name; 62 | continue; 63 | } 64 | 65 | $format = $this->isToken($value) ? '%s=%s' : '%s="%s"'; 66 | 67 | $segments[] = sprintf($format, $name, $value); 68 | } 69 | 70 | return implode(', ', $segments); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/HeaderLastModified.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.29 21 | * @link https://tools.ietf.org/html/rfc822#section-5 22 | */ 23 | class HeaderLastModified extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var DateTimeInterface 28 | */ 29 | protected $timestamp; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param DateTimeInterface $timestamp 35 | */ 36 | public function __construct(DateTimeInterface $timestamp) 37 | { 38 | $this->timestamp = $timestamp; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getFieldName() : string 45 | { 46 | return 'Last-Modified'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function getFieldValue() : string 53 | { 54 | return $this->formatDateTime($this->timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HeaderLink.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use Psr\Http\Message\UriInterface; 18 | 19 | /** 20 | * Import functions 21 | */ 22 | use function sprintf; 23 | 24 | /** 25 | * @link https://tools.ietf.org/html/rfc5988 26 | */ 27 | class HeaderLink extends AbstractHeader 28 | { 29 | 30 | /** 31 | * @var UriInterface 32 | */ 33 | protected $uri; 34 | 35 | /** 36 | * @var array 37 | */ 38 | protected $parameters; 39 | 40 | /** 41 | * Constructor of the class 42 | * 43 | * @param UriInterface $uri 44 | * @param array $parameters 45 | */ 46 | public function __construct(UriInterface $uri, array $parameters = []) 47 | { 48 | /** @var array */ 49 | $parameters = $this->validateParameters($parameters); 50 | 51 | $this->uri = $uri; 52 | $this->parameters = $parameters; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldName() : string 59 | { 60 | return 'Link'; 61 | } 62 | 63 | /** 64 | * {@inheritdoc} 65 | */ 66 | public function getFieldValue() : string 67 | { 68 | $v = sprintf('<%s>', $this->uri->__toString()); 69 | foreach ($this->parameters as $name => $value) { 70 | $v .= sprintf('; %s="%s"', $name, $value); 71 | } 72 | 73 | return $v; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/HeaderLocation.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use Psr\Http\Message\UriInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.30 21 | */ 22 | class HeaderLocation extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var UriInterface 27 | */ 28 | protected $uri; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param UriInterface $uri 34 | */ 35 | public function __construct(UriInterface $uri) 36 | { 37 | $this->uri = $uri; 38 | } 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | public function getFieldName() : string 44 | { 45 | return 'Location'; 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getFieldValue() : string 52 | { 53 | return $this->uri->__toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/HeaderRefresh.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use InvalidArgumentException; 18 | use Psr\Http\Message\UriInterface; 19 | 20 | /** 21 | * Import functions 22 | */ 23 | use function sprintf; 24 | 25 | /** 26 | * @link https://en.wikipedia.org/wiki/Meta_refresh 27 | */ 28 | class HeaderRefresh extends AbstractHeader 29 | { 30 | 31 | /** 32 | * @var int 33 | */ 34 | protected $delay; 35 | 36 | /** 37 | * @var UriInterface 38 | */ 39 | protected $uri; 40 | 41 | /** 42 | * Constructor of the class 43 | * 44 | * @param int $delay 45 | * @param UriInterface $uri 46 | */ 47 | public function __construct(int $delay, UriInterface $uri) 48 | { 49 | $this->validateDelay($delay); 50 | 51 | $this->delay = $delay; 52 | $this->uri = $uri; 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getFieldName() : string 59 | { 60 | return 'Refresh'; 61 | } 62 | 63 | /** 64 | * {@inheritdoc} 65 | */ 66 | public function getFieldValue() : string 67 | { 68 | return sprintf('%d; url=%s', $this->delay, $this->uri->__toString()); 69 | } 70 | 71 | /** 72 | * Validates the redirection delay 73 | * 74 | * @param int $delay 75 | * 76 | * @return void 77 | * 78 | * @throws InvalidArgumentException 79 | * If the delay isn't valid. 80 | */ 81 | private function validateDelay(int $delay) : void 82 | { 83 | if (! ($delay >= 0)) { 84 | throw new InvalidArgumentException(sprintf( 85 | 'The delay "%2$d" for the header "%1$s" is not valid', 86 | $this->getFieldName(), 87 | $delay 88 | )); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/HeaderRetryAfter.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.37 21 | * @link https://tools.ietf.org/html/rfc822#section-5 22 | */ 23 | class HeaderRetryAfter extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var DateTimeInterface 28 | */ 29 | protected $timestamp; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param DateTimeInterface $timestamp 35 | */ 36 | public function __construct(DateTimeInterface $timestamp) 37 | { 38 | $this->timestamp = $timestamp; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getFieldName() : string 45 | { 46 | return 'Retry-After'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function getFieldValue() : string 53 | { 54 | return $this->formatDateTime($this->timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HeaderSetCookie.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeImmutable; 18 | use DateTimeInterface; 19 | use InvalidArgumentException; 20 | 21 | /** 22 | * Import functions 23 | */ 24 | use function rawurlencode; 25 | use function sprintf; 26 | use function strpbrk; 27 | use function time; 28 | 29 | /** 30 | * @link https://tools.ietf.org/html/rfc6265#section-4.1 31 | * @link https://github.com/php/php-src/blob/master/ext/standard/head.c 32 | */ 33 | class HeaderSetCookie extends AbstractHeader 34 | { 35 | 36 | /** 37 | * @var string 38 | * 39 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#lax 40 | */ 41 | public const SAME_SITE_LAX = 'Lax'; 42 | 43 | /** 44 | * @var string 45 | * 46 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#strict 47 | */ 48 | public const SAME_SITE_STRICT = 'Strict'; 49 | 50 | /** 51 | * @var string 52 | * 53 | * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#none 54 | */ 55 | public const SAME_SITE_NONE = 'None'; 56 | 57 | /** 58 | * @var string 59 | */ 60 | protected $name; 61 | 62 | /** 63 | * @var string 64 | */ 65 | protected $value; 66 | 67 | /** 68 | * @var DateTimeInterface|null 69 | */ 70 | protected $expires; 71 | 72 | /** 73 | * @var array{path?: ?string, domain?: ?string, secure?: ?bool, httpOnly?: ?bool, sameSite?: ?string} 74 | */ 75 | protected $options; 76 | 77 | /** 78 | * @var array{path?: ?string, domain?: ?string, secure?: ?bool, httpOnly?: ?bool, sameSite?: ?string} 79 | */ 80 | protected static $defaultOptions = [ 81 | 'path' => '/', 82 | 'domain' => null, 83 | 'secure' => null, 84 | 'httpOnly' => true, 85 | 'sameSite' => self::SAME_SITE_LAX, 86 | ]; 87 | 88 | /** 89 | * Constructor of the class 90 | * 91 | * @param string $name 92 | * @param string $value 93 | * @param DateTimeInterface|null $expires 94 | * @param array{path?: ?string, domain?: ?string, secure?: ?bool, httpOnly?: ?bool, sameSite?: ?string} $options 95 | */ 96 | public function __construct(string $name, string $value, ?DateTimeInterface $expires = null, array $options = []) 97 | { 98 | $options += static::$defaultOptions; 99 | 100 | $this->validateCookieName($name); 101 | 102 | if (isset($options['path'])) { 103 | $this->validateCookieStringOption('path', $options['path']); 104 | } 105 | 106 | if (isset($options['domain'])) { 107 | $this->validateCookieStringOption('domain', $options['domain']); 108 | } 109 | 110 | if (isset($options['sameSite'])) { 111 | $this->validateCookieStringOption('sameSite', $options['sameSite']); 112 | } 113 | 114 | if ($value === '') { 115 | $value = 'deleted'; 116 | $expires = new DateTimeImmutable('1 year ago'); 117 | } 118 | 119 | $this->name = $name; 120 | $this->value = $value; 121 | $this->expires = $expires; 122 | $this->options = $options; 123 | } 124 | 125 | /** 126 | * {@inheritdoc} 127 | */ 128 | public function getFieldName() : string 129 | { 130 | return 'Set-Cookie'; 131 | } 132 | 133 | /** 134 | * {@inheritdoc} 135 | */ 136 | public function getFieldValue() : string 137 | { 138 | $name = rawurlencode($this->name); 139 | $value = rawurlencode($this->value); 140 | 141 | $result = sprintf('%s=%s', $name, $value); 142 | 143 | if (isset($this->expires)) { 144 | $result .= '; Expires=' . $this->formatDateTime($this->expires); 145 | 146 | $maxAge = $this->expires->getTimestamp() - time(); 147 | 148 | // cannot be less than zero... 149 | if ($maxAge < 0) { 150 | $maxAge = 0; 151 | } 152 | 153 | $result .= '; Max-Age=' . $maxAge; 154 | } 155 | 156 | if (isset($this->options['path'])) { 157 | $result .= '; Path=' . $this->options['path']; 158 | } 159 | 160 | if (isset($this->options['domain'])) { 161 | $result .= '; Domain=' . $this->options['domain']; 162 | } 163 | 164 | if (isset($this->options['secure']) && $this->options['secure']) { 165 | $result .= '; Secure'; 166 | } 167 | 168 | if (isset($this->options['httpOnly']) && $this->options['httpOnly']) { 169 | $result .= '; HttpOnly'; 170 | } 171 | 172 | if (isset($this->options['sameSite'])) { 173 | $result .= '; SameSite=' . $this->options['sameSite']; 174 | } 175 | 176 | return $result; 177 | } 178 | 179 | /** 180 | * Validates the cookie name 181 | * 182 | * @param string $name 183 | * 184 | * @return void 185 | * 186 | * @throws InvalidArgumentException 187 | * If a cookie's name isn't valid. 188 | */ 189 | private function validateCookieName(string $name) : void 190 | { 191 | if ('' === $name) { 192 | throw new InvalidArgumentException('Cookie name cannot be empty'); 193 | } 194 | 195 | if (strpbrk($name, "=,; \t\r\n\013\014") !== false) { 196 | throw new InvalidArgumentException(sprintf( 197 | 'The cookie "%s" contains prohibited characters', 198 | $name 199 | )); 200 | } 201 | } 202 | 203 | /** 204 | * Validates the cookie's string option 205 | * 206 | * @param string $key 207 | * @param mixed $value 208 | * 209 | * @return void 210 | * 211 | * @throws InvalidArgumentException 212 | * If a cookie's string option isn't valid. 213 | */ 214 | private function validateCookieStringOption(string $key, $value) : void 215 | { 216 | if (!is_string($value)) { 217 | throw new InvalidArgumentException(sprintf( 218 | 'The cookie option "%s" must be a string', 219 | $key 220 | )); 221 | } 222 | 223 | if (strpbrk($value, ",; \t\r\n\013\014") !== false) { 224 | throw new InvalidArgumentException(sprintf( 225 | 'The cookie option "%s" contains prohibited characters', 226 | $key 227 | )); 228 | } 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/HeaderSunset.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | 19 | /** 20 | * @link https://tools.ietf.org/id/draft-wilde-sunset-header-03.html 21 | * @link https://github.com/sunrise-php/http-header-kit/issues/1#issuecomment-457043527 22 | */ 23 | class HeaderSunset extends AbstractHeader 24 | { 25 | 26 | /** 27 | * @var DateTimeInterface 28 | */ 29 | protected $timestamp; 30 | 31 | /** 32 | * Constructor of the class 33 | * 34 | * @param DateTimeInterface $timestamp 35 | */ 36 | public function __construct(DateTimeInterface $timestamp) 37 | { 38 | $this->timestamp = $timestamp; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getFieldName() : string 45 | { 46 | return 'Sunset'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function getFieldValue() : string 53 | { 54 | return $this->formatDateTime($this->timestamp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/HeaderTrailer.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * @link https://tools.ietf.org/html/rfc2616#section-14.40 16 | */ 17 | class HeaderTrailer extends AbstractHeader 18 | { 19 | 20 | /** 21 | * @var string 22 | */ 23 | protected $value; 24 | 25 | /** 26 | * Constructor of the class 27 | * 28 | * @param string $value 29 | */ 30 | public function __construct(string $value) 31 | { 32 | $this->validateToken($value); 33 | 34 | $this->value = $value; 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function getFieldName() : string 41 | { 42 | return 'Trailer'; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFieldValue() : string 49 | { 50 | return $this->value; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/HeaderTransferEncoding.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.41 21 | */ 22 | class HeaderTransferEncoding extends AbstractHeader 23 | { 24 | 25 | /** 26 | * Directives 27 | * 28 | * @var string 29 | */ 30 | public const CHUNKED = 'chunked'; 31 | public const COMPRESS = 'compress'; 32 | public const DEFLATE = 'deflate'; 33 | public const GZIP = 'gzip'; 34 | 35 | /** 36 | * @var list 37 | */ 38 | protected $directives; 39 | 40 | /** 41 | * Constructor of the class 42 | * 43 | * @param string ...$directives 44 | */ 45 | public function __construct(string ...$directives) 46 | { 47 | /** @var list $directives */ 48 | 49 | $this->validateToken(...$directives); 50 | 51 | $this->directives = $directives; 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function getFieldName() : string 58 | { 59 | return 'Transfer-Encoding'; 60 | } 61 | 62 | /** 63 | * {@inheritdoc} 64 | */ 65 | public function getFieldValue() : string 66 | { 67 | return implode(', ', $this->directives); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/HeaderVary.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | 19 | /** 20 | * @link https://tools.ietf.org/html/rfc2616#section-14.44 21 | */ 22 | class HeaderVary extends AbstractHeader 23 | { 24 | 25 | /** 26 | * @var list 27 | */ 28 | protected $value; 29 | 30 | /** 31 | * Constructor of the class 32 | * 33 | * @param string ...$value 34 | */ 35 | public function __construct(string ...$value) 36 | { 37 | /** @var list $value */ 38 | 39 | $this->validateToken(...$value); 40 | 41 | $this->value = $value; 42 | } 43 | 44 | /** 45 | * {@inheritdoc} 46 | */ 47 | public function getFieldName() : string 48 | { 49 | return 'Vary'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFieldValue() : string 56 | { 57 | return implode(', ', $this->value); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/HeaderWWWAuthenticate.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import functions 16 | */ 17 | use function implode; 18 | use function sprintf; 19 | 20 | /** 21 | * @link https://tools.ietf.org/html/rfc7235#section-4.1 22 | */ 23 | class HeaderWWWAuthenticate extends AbstractHeader 24 | { 25 | 26 | /** 27 | * HTTP Authentication Schemes 28 | * 29 | * @link https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml 30 | */ 31 | public const HTTP_AUTHENTICATE_SCHEME_BASIC = 'Basic'; 32 | public const HTTP_AUTHENTICATE_SCHEME_BEARER = 'Bearer'; 33 | public const HTTP_AUTHENTICATE_SCHEME_DIGEST = 'Digest'; 34 | public const HTTP_AUTHENTICATE_SCHEME_HOBA = 'HOBA'; 35 | public const HTTP_AUTHENTICATE_SCHEME_MUTUAL = 'Mutual'; 36 | public const HTTP_AUTHENTICATE_SCHEME_NEGOTIATE = 'Negotiate'; 37 | public const HTTP_AUTHENTICATE_SCHEME_OAUTH = 'OAuth'; 38 | public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_1 = 'SCRAM-SHA-1'; 39 | public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_256 = 'SCRAM-SHA-256'; 40 | public const HTTP_AUTHENTICATE_SCHEME_VAPID = 'vapid'; 41 | 42 | /** 43 | * @var string 44 | */ 45 | protected $scheme; 46 | 47 | /** 48 | * @var array 49 | */ 50 | protected $parameters; 51 | 52 | /** 53 | * Constructor of the class 54 | * 55 | * @param string $scheme 56 | * @param array $parameters 57 | */ 58 | public function __construct(string $scheme, array $parameters = []) 59 | { 60 | $this->validateToken($scheme); 61 | 62 | /** @var array */ 63 | $parameters = $this->validateParameters($parameters); 64 | 65 | $this->scheme = $scheme; 66 | $this->parameters = $parameters; 67 | } 68 | 69 | /** 70 | * {@inheritdoc} 71 | */ 72 | public function getFieldName() : string 73 | { 74 | return 'WWW-Authenticate'; 75 | } 76 | 77 | /** 78 | * {@inheritdoc} 79 | */ 80 | public function getFieldValue() : string 81 | { 82 | $v = $this->scheme; 83 | 84 | $challenge = []; 85 | foreach ($this->parameters as $name => $value) { 86 | $challenge[] = sprintf(' %s="%s"', $name, $value); 87 | } 88 | 89 | if (!empty($challenge)) { 90 | $v .= implode(',', $challenge); 91 | } 92 | 93 | return $v; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/HeaderWarning.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) 2018, Anatoly Fenric 8 | * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE 9 | * @link https://github.com/sunrise-php/http-header-kit 10 | */ 11 | 12 | namespace Sunrise\Http\Header; 13 | 14 | /** 15 | * Import classes 16 | */ 17 | use DateTimeInterface; 18 | use InvalidArgumentException; 19 | 20 | /** 21 | * Import functions 22 | */ 23 | use function sprintf; 24 | 25 | /** 26 | * @link https://tools.ietf.org/html/rfc2616#section-14.46 27 | */ 28 | class HeaderWarning extends AbstractHeader 29 | { 30 | 31 | /** 32 | * HTTP Warning Codes 33 | * 34 | * @link https://www.iana.org/assignments/http-warn-codes/http-warn-codes.xhtml 35 | */ 36 | public const HTTP_WARNING_CODE_RESPONSE_IS_STALE = 110; 37 | public const HTTP_WARNING_CODE_REVALIDATION_FAILED = 111; 38 | public const HTTP_WARNING_CODE_DISCONNECTED_OPERATION = 112; 39 | public const HTTP_WARNING_CODE_HEURISTIC_EXPIRATION = 113; 40 | public const HTTP_WARNING_CODE_MISCELLANEOUS_WARNING = 199; 41 | public const HTTP_WARNING_CODE_TRANSFORMATION_APPLIED = 214; 42 | public const HTTP_WARNING_CODE_MISCELLANEOUS_PERSISTENT_WARNING = 299; 43 | 44 | /** 45 | * @var int 46 | */ 47 | protected $code; 48 | 49 | /** 50 | * @var string 51 | */ 52 | protected $agent; 53 | 54 | /** 55 | * @var string 56 | */ 57 | protected $text; 58 | 59 | /** 60 | * @var DateTimeInterface|null 61 | */ 62 | protected $date; 63 | 64 | /** 65 | * Constructor of the class 66 | * 67 | * @param int $code 68 | * @param string $agent 69 | * @param string $text 70 | * @param DateTimeInterface|null $date 71 | */ 72 | public function __construct(int $code, string $agent, string $text, ?DateTimeInterface $date = null) 73 | { 74 | $this->validateCode($code); 75 | $this->validateToken($agent); 76 | $this->validateQuotedString($text); 77 | 78 | $this->code = $code; 79 | $this->agent = $agent; 80 | $this->text = $text; 81 | $this->date = $date; 82 | } 83 | 84 | /** 85 | * {@inheritdoc} 86 | */ 87 | public function getFieldName() : string 88 | { 89 | return 'Warning'; 90 | } 91 | 92 | /** 93 | * {@inheritdoc} 94 | */ 95 | public function getFieldValue() : string 96 | { 97 | $value = sprintf('%s %s "%s"', $this->code, $this->agent, $this->text); 98 | 99 | if (isset($this->date)) { 100 | $value .= sprintf(' "%s"', $this->formatDateTime($this->date)); 101 | } 102 | 103 | return $value; 104 | } 105 | 106 | /** 107 | * Validates the given code 108 | * 109 | * @param int $code 110 | * 111 | * @return void 112 | * 113 | * @throws InvalidArgumentException 114 | * If the code isn't valid. 115 | */ 116 | private function validateCode(int $code) : void 117 | { 118 | if (! ($code >= 100 && $code <= 999)) { 119 | throw new InvalidArgumentException(sprintf( 120 | 'The code "%2$d" for the header "%1$s" is not valid', 121 | $this->getFieldName(), 122 | $code 123 | )); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /tests/AbstractHeaderTest.php: -------------------------------------------------------------------------------- 1 | createMock(AbstractHeader::class); 14 | 15 | $this->assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testBuild() 19 | { 20 | $header = $this->createMock(AbstractHeader::class); 21 | 22 | $header->method('getFieldName')->willReturn('x-foo'); 23 | $header->method('getFieldValue')->willReturn('bar'); 24 | 25 | $this->assertSame('x-foo: bar', $header->__toString()); 26 | } 27 | 28 | public function testIterator() 29 | { 30 | $header = $this->createMock(AbstractHeader::class); 31 | 32 | $header->method('getFieldName')->willReturn('x-foo'); 33 | $header->method('getFieldValue')->willReturn('bar'); 34 | 35 | $iterator = $header->getIterator(); 36 | 37 | $iterator->rewind(); 38 | $this->assertSame('x-foo', $iterator->current()); 39 | 40 | $iterator->next(); 41 | $this->assertSame('bar', $iterator->current()); 42 | 43 | $iterator->next(); 44 | $this->assertFalse($iterator->valid()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlAllowCredentialsTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAccessControlAllowCredentials(); 21 | 22 | $this->assertSame('Access-Control-Allow-Credentials', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAccessControlAllowCredentials(); 28 | 29 | $this->assertSame('true', $header->getFieldValue()); 30 | } 31 | 32 | public function testBuild() 33 | { 34 | $header = new HeaderAccessControlAllowCredentials(); 35 | 36 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 37 | 38 | $this->assertSame($expected, $header->__toString()); 39 | } 40 | 41 | public function testIterator() 42 | { 43 | $header = new HeaderAccessControlAllowCredentials(); 44 | $iterator = $header->getIterator(); 45 | 46 | $iterator->rewind(); 47 | $this->assertSame($header->getFieldName(), $iterator->current()); 48 | 49 | $iterator->next(); 50 | $this->assertSame($header->getFieldValue(), $iterator->current()); 51 | 52 | $iterator->next(); 53 | $this->assertFalse($iterator->valid()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlAllowHeadersTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAccessControlAllowHeaders('x-foo'); 21 | 22 | $this->assertSame('Access-Control-Allow-Headers', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAccessControlAllowHeaders('x-foo'); 28 | 29 | $this->assertSame('x-foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderAccessControlAllowHeaders('x-foo', 'x-bar', 'x-baz'); 35 | 36 | $this->assertSame('x-foo, x-bar, x-baz', $header->getFieldValue()); 37 | } 38 | 39 | public function testEmptyValue() 40 | { 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Allow-Headers" is not valid'); 43 | 44 | new HeaderAccessControlAllowHeaders(''); 45 | } 46 | 47 | public function testEmptyValueAmongOthers() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Allow-Headers" is not valid'); 51 | 52 | new HeaderAccessControlAllowHeaders('x-foo', '', 'x-bar'); 53 | } 54 | 55 | public function testInvalidValue() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "x-foo=" for the header "Access-Control-Allow-Headers" is not valid'); 59 | 60 | // a token cannot contain the "=" character... 61 | new HeaderAccessControlAllowHeaders('x-foo='); 62 | } 63 | 64 | public function testInvalidValueAmongOthers() 65 | { 66 | $this->expectException(\InvalidArgumentException::class); 67 | $this->expectExceptionMessage('The value "x-bar=" for the header "Access-Control-Allow-Headers" is not valid'); 68 | 69 | // a token cannot contain the "=" character... 70 | new HeaderAccessControlAllowHeaders('x-foo', 'x-bar=', 'x-bar'); 71 | } 72 | 73 | public function testBuild() 74 | { 75 | $header = new HeaderAccessControlAllowHeaders('x-foo'); 76 | 77 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 78 | 79 | $this->assertSame($expected, $header->__toString()); 80 | } 81 | 82 | public function testIterator() 83 | { 84 | $header = new HeaderAccessControlAllowHeaders('x-foo'); 85 | $iterator = $header->getIterator(); 86 | 87 | $iterator->rewind(); 88 | $this->assertSame($header->getFieldName(), $iterator->current()); 89 | 90 | $iterator->next(); 91 | $this->assertSame($header->getFieldValue(), $iterator->current()); 92 | 93 | $iterator->next(); 94 | $this->assertFalse($iterator->valid()); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlAllowMethodsTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAccessControlAllowMethods('GET'); 21 | 22 | $this->assertSame('Access-Control-Allow-Methods', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAccessControlAllowMethods('GET'); 28 | 29 | $this->assertSame('GET', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderAccessControlAllowMethods('HEAD', 'GET', 'POST'); 35 | 36 | $this->assertSame('HEAD, GET, POST', $header->getFieldValue()); 37 | } 38 | 39 | public function testValueCapitalizing() 40 | { 41 | $header = new HeaderAccessControlAllowMethods('head', 'get', 'post'); 42 | 43 | $this->assertSame('HEAD, GET, POST', $header->getFieldValue()); 44 | } 45 | 46 | public function testEmptyValue() 47 | { 48 | $this->expectException(\InvalidArgumentException::class); 49 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Allow-Methods" is not valid'); 50 | 51 | new HeaderAccessControlAllowMethods(''); 52 | } 53 | 54 | public function testEmptyValueAmongOthers() 55 | { 56 | $this->expectException(\InvalidArgumentException::class); 57 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Allow-Methods" is not valid'); 58 | 59 | new HeaderAccessControlAllowMethods('head', '', 'post'); 60 | } 61 | 62 | public function testInvalidValue() 63 | { 64 | $this->expectException(\InvalidArgumentException::class); 65 | $this->expectExceptionMessage('The value "@" for the header "Access-Control-Allow-Methods" is not valid'); 66 | 67 | // isn't a token... 68 | new HeaderAccessControlAllowMethods('@'); 69 | } 70 | 71 | public function testInvalidValueAmongOthers() 72 | { 73 | $this->expectException(\InvalidArgumentException::class); 74 | $this->expectExceptionMessage('The value "@" for the header "Access-Control-Allow-Methods" is not valid'); 75 | 76 | // isn't a token... 77 | new HeaderAccessControlAllowMethods('head', '@', 'post'); 78 | } 79 | 80 | public function testBuild() 81 | { 82 | $header = new HeaderAccessControlAllowMethods('GET'); 83 | 84 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 85 | 86 | $this->assertSame($expected, $header->__toString()); 87 | } 88 | 89 | public function testIterator() 90 | { 91 | $header = new HeaderAccessControlAllowMethods('GET'); 92 | $iterator = $header->getIterator(); 93 | 94 | $iterator->rewind(); 95 | $this->assertSame($header->getFieldName(), $iterator->current()); 96 | 97 | $iterator->next(); 98 | $this->assertSame($header->getFieldValue(), $iterator->current()); 99 | 100 | $iterator->next(); 101 | $this->assertFalse($iterator->valid()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlAllowOriginTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $uri = new Uri('http://localhost'); 23 | $header = new HeaderAccessControlAllowOrigin($uri); 24 | 25 | $this->assertSame('Access-Control-Allow-Origin', $header->getFieldName()); 26 | } 27 | 28 | public function testFieldValue() 29 | { 30 | $uri = new Uri('http://localhost'); 31 | $header = new HeaderAccessControlAllowOrigin($uri); 32 | 33 | $this->assertSame('http://localhost', $header->getFieldValue()); 34 | } 35 | 36 | public function testFieldValueWithoutUri() 37 | { 38 | $header = new HeaderAccessControlAllowOrigin(null); 39 | 40 | $this->assertSame('*', $header->getFieldValue()); 41 | } 42 | 43 | public function testIgnoringUnnecessaryUriComponents() 44 | { 45 | $uri = new Uri('http://user:pass@localhost:3000/index.php?q#h'); 46 | $header = new HeaderAccessControlAllowOrigin($uri); 47 | 48 | $this->assertSame('http://localhost:3000', $header->getFieldValue()); 49 | } 50 | 51 | public function testUriWithPort() 52 | { 53 | $uri = new Uri('http://localhost:3000'); 54 | $header = new HeaderAccessControlAllowOrigin($uri); 55 | 56 | $this->assertSame('http://localhost:3000', $header->getFieldValue()); 57 | } 58 | 59 | public function testUriWithoutScheme() 60 | { 61 | $this->expectException(\InvalidArgumentException::class); 62 | 63 | $this->expectExceptionMessage( 64 | 'The URI "//localhost" for the header "Access-Control-Allow-Origin" is not valid' 65 | ); 66 | 67 | new HeaderAccessControlAllowOrigin(new Uri('//localhost')); 68 | } 69 | 70 | public function testUriWithoutHost() 71 | { 72 | $this->expectException(\InvalidArgumentException::class); 73 | 74 | $this->expectExceptionMessage( 75 | 'The URI "http:" for the header "Access-Control-Allow-Origin" is not valid' 76 | ); 77 | 78 | new HeaderAccessControlAllowOrigin(new Uri('http:')); 79 | } 80 | 81 | public function testBuild() 82 | { 83 | $uri = new Uri('http://localhost'); 84 | $header = new HeaderAccessControlAllowOrigin($uri); 85 | 86 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 87 | 88 | $this->assertSame($expected, $header->__toString()); 89 | } 90 | 91 | public function testIterator() 92 | { 93 | $uri = new Uri('http://localhost'); 94 | $header = new HeaderAccessControlAllowOrigin($uri); 95 | $iterator = $header->getIterator(); 96 | 97 | $iterator->rewind(); 98 | $this->assertSame($header->getFieldName(), $iterator->current()); 99 | 100 | $iterator->next(); 101 | $this->assertSame($header->getFieldValue(), $iterator->current()); 102 | 103 | $iterator->next(); 104 | $this->assertFalse($iterator->valid()); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlExposeHeadersTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAccessControlExposeHeaders('x-foo'); 21 | 22 | $this->assertSame('Access-Control-Expose-Headers', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAccessControlExposeHeaders('x-foo'); 28 | 29 | $this->assertSame('x-foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderAccessControlExposeHeaders('x-foo', 'x-bar', 'x-baz'); 35 | 36 | $this->assertSame('x-foo, x-bar, x-baz', $header->getFieldValue()); 37 | } 38 | 39 | public function testEmptyValue() 40 | { 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Expose-Headers" is not valid'); 43 | 44 | new HeaderAccessControlExposeHeaders(''); 45 | } 46 | 47 | public function testEmptyValueAmongOthers() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Access-Control-Expose-Headers" is not valid'); 51 | 52 | new HeaderAccessControlExposeHeaders('x-foo', '', 'x-baz'); 53 | } 54 | 55 | public function testInvalidValue() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "@" for the header "Access-Control-Expose-Headers" is not valid'); 59 | 60 | // isn't a token... 61 | new HeaderAccessControlExposeHeaders('@'); 62 | } 63 | 64 | public function testInvalidValueAmongOthers() 65 | { 66 | $this->expectException(\InvalidArgumentException::class); 67 | $this->expectExceptionMessage('The value "@" for the header "Access-Control-Expose-Headers" is not valid'); 68 | 69 | // isn't a token... 70 | new HeaderAccessControlExposeHeaders('x-foo', '@', 'x-baz'); 71 | } 72 | 73 | public function testBuild() 74 | { 75 | $header = new HeaderAccessControlExposeHeaders('x-foo'); 76 | 77 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 78 | 79 | $this->assertSame($expected, $header->__toString()); 80 | } 81 | 82 | public function testIterator() 83 | { 84 | $header = new HeaderAccessControlExposeHeaders('x-foo'); 85 | $iterator = $header->getIterator(); 86 | 87 | $iterator->rewind(); 88 | $this->assertSame($header->getFieldName(), $iterator->current()); 89 | 90 | $iterator->next(); 91 | $this->assertSame($header->getFieldValue(), $iterator->current()); 92 | 93 | $iterator->next(); 94 | $this->assertFalse($iterator->valid()); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /tests/HeaderAccessControlMaxAgeTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAccessControlMaxAge(-1); 21 | 22 | $this->assertSame('Access-Control-Max-Age', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAccessControlMaxAge(-1); 28 | 29 | $this->assertSame('-1', $header->getFieldValue()); 30 | } 31 | 32 | /** 33 | * @dataProvider validValueDataProvider 34 | */ 35 | public function testValidValue(int $validValue) 36 | { 37 | $header = new HeaderAccessControlMaxAge($validValue); 38 | 39 | $this->assertEquals($validValue, $header->getFieldValue()); 40 | } 41 | 42 | public function validValueDataProvider() : array 43 | { 44 | return [[-1], [1], [2]]; 45 | } 46 | 47 | /** 48 | * @dataProvider invalidValueDataProvider 49 | */ 50 | public function testInvalidValue(int $invalidValue) 51 | { 52 | $this->expectException(\InvalidArgumentException::class); 53 | 54 | $this->expectExceptionMessage(sprintf( 55 | 'The value "%d" for the header "Access-Control-Max-Age" is not valid', 56 | $invalidValue 57 | )); 58 | 59 | new HeaderAccessControlMaxAge($invalidValue); 60 | } 61 | 62 | public function invalidValueDataProvider() : array 63 | { 64 | return [[-3], [-2], [0]]; 65 | } 66 | 67 | public function testBuild() 68 | { 69 | $header = new HeaderAccessControlMaxAge(-1); 70 | 71 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 72 | 73 | $this->assertSame($expected, $header->__toString()); 74 | } 75 | 76 | public function testIterator() 77 | { 78 | $header = new HeaderAccessControlMaxAge(-1); 79 | $iterator = $header->getIterator(); 80 | 81 | $iterator->rewind(); 82 | $this->assertSame($header->getFieldName(), $iterator->current()); 83 | 84 | $iterator->next(); 85 | $this->assertSame($header->getFieldValue(), $iterator->current()); 86 | 87 | $iterator->next(); 88 | $this->assertFalse($iterator->valid()); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/HeaderAgeTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAge(0); 21 | 22 | $this->assertSame('Age', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAge(0); 28 | 29 | $this->assertSame('0', $header->getFieldValue()); 30 | } 31 | 32 | /** 33 | * @dataProvider validValueDataProvider 34 | */ 35 | public function testValidValue(int $validValue) 36 | { 37 | $header = new HeaderAge($validValue); 38 | 39 | $this->assertEquals($validValue, $header->getFieldValue()); 40 | } 41 | 42 | public function validValueDataProvider() : array 43 | { 44 | return [[0], [1], [2]]; 45 | } 46 | 47 | /** 48 | * @dataProvider invalidValueDataProvider 49 | */ 50 | public function testInvalidValue(int $invalidValue) 51 | { 52 | $this->expectException(\InvalidArgumentException::class); 53 | 54 | $this->expectExceptionMessage(sprintf( 55 | 'The value "%d" for the header "Age" is not valid', 56 | $invalidValue 57 | )); 58 | 59 | new HeaderAge($invalidValue); 60 | } 61 | 62 | public function invalidValueDataProvider() : array 63 | { 64 | return [[-3], [-2], [-1]]; 65 | } 66 | 67 | public function testBuild() 68 | { 69 | $header = new HeaderAge(0); 70 | 71 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 72 | 73 | $this->assertSame($expected, $header->__toString()); 74 | } 75 | 76 | public function testIterator() 77 | { 78 | $header = new HeaderAge(0); 79 | $iterator = $header->getIterator(); 80 | 81 | $iterator->rewind(); 82 | $this->assertSame($header->getFieldName(), $iterator->current()); 83 | 84 | $iterator->next(); 85 | $this->assertSame($header->getFieldValue(), $iterator->current()); 86 | 87 | $iterator->next(); 88 | $this->assertFalse($iterator->valid()); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/HeaderAllowTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderAllow('GET'); 21 | 22 | $this->assertSame('Allow', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderAllow('GET'); 28 | 29 | $this->assertSame('GET', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderAllow('HEAD', 'GET', 'POST'); 35 | 36 | $this->assertSame('HEAD, GET, POST', $header->getFieldValue()); 37 | } 38 | 39 | public function testValueCapitalizing() 40 | { 41 | $header = new HeaderAllow('head', 'get', 'post'); 42 | 43 | $this->assertSame('HEAD, GET, POST', $header->getFieldValue()); 44 | } 45 | 46 | public function testEmptyValue() 47 | { 48 | $this->expectException(\InvalidArgumentException::class); 49 | $this->expectExceptionMessage('The value "" for the header "Allow" is not valid'); 50 | 51 | new HeaderAllow(''); 52 | } 53 | 54 | public function testEmptyValueAmongOthers() 55 | { 56 | $this->expectException(\InvalidArgumentException::class); 57 | $this->expectExceptionMessage('The value "" for the header "Allow" is not valid'); 58 | 59 | new HeaderAllow('head', '', 'post'); 60 | } 61 | 62 | public function testInvalidValue() 63 | { 64 | $this->expectException(\InvalidArgumentException::class); 65 | $this->expectExceptionMessage('The value "@" for the header "Allow" is not valid'); 66 | 67 | // isn't a token... 68 | new HeaderAllow('@'); 69 | } 70 | 71 | public function testInvalidValueAmongOthers() 72 | { 73 | $this->expectException(\InvalidArgumentException::class); 74 | $this->expectExceptionMessage('The value "@" for the header "Allow" is not valid'); 75 | 76 | // isn't a token... 77 | new HeaderAllow('head', '@', 'post'); 78 | } 79 | 80 | public function testBuild() 81 | { 82 | $header = new HeaderAllow('GET'); 83 | 84 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 85 | 86 | $this->assertSame($expected, $header->__toString()); 87 | } 88 | 89 | public function testIterator() 90 | { 91 | $header = new HeaderAllow('GET'); 92 | $iterator = $header->getIterator(); 93 | 94 | $iterator->rewind(); 95 | $this->assertSame($header->getFieldName(), $iterator->current()); 96 | 97 | $iterator->next(); 98 | $this->assertSame($header->getFieldValue(), $iterator->current()); 99 | 100 | $iterator->next(); 101 | $this->assertFalse($iterator->valid()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/HeaderCacheControlTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderCacheControl([]); 21 | 22 | $this->assertSame('Cache-Control', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderCacheControl([]); 28 | 29 | $this->assertSame('', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithEmptyValue() 33 | { 34 | $header = new HeaderCacheControl([ 35 | 'foo' => '', 36 | ]); 37 | 38 | $this->assertSame('foo', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithToken() 42 | { 43 | $header = new HeaderCacheControl([ 44 | 'foo' => 'token', 45 | ]); 46 | 47 | $this->assertSame('foo=token', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithQuotedString() 51 | { 52 | $header = new HeaderCacheControl([ 53 | 'foo' => 'quoted string', 54 | ]); 55 | 56 | $this->assertSame('foo="quoted string"', $header->getFieldValue()); 57 | } 58 | 59 | public function testParameterWithInteger() 60 | { 61 | $header = new HeaderCacheControl([ 62 | 'foo' => 1, 63 | ]); 64 | 65 | $this->assertSame('foo=1', $header->getFieldValue()); 66 | } 67 | 68 | public function testSeveralParameters() 69 | { 70 | $header = new HeaderCacheControl([ 71 | 'foo' => '', 72 | 'bar' => 'token', 73 | 'baz' => 'quoted string', 74 | 'qux' => 1, 75 | ]); 76 | 77 | $this->assertSame('foo, bar=token, baz="quoted string", qux=1', $header->getFieldValue()); 78 | } 79 | 80 | public function testInvalidParameterName() 81 | { 82 | $this->expectException(\InvalidArgumentException::class); 83 | 84 | $this->expectExceptionMessage( 85 | 'The parameter-name "invalid name" for the header "Cache-Control" is not valid' 86 | ); 87 | 88 | new HeaderCacheControl(['invalid name' => 'value']); 89 | } 90 | 91 | public function testInvalidParameterValue() 92 | { 93 | $this->expectException(\InvalidArgumentException::class); 94 | 95 | $this->expectExceptionMessage( 96 | 'The parameter-value ""invalid value"" for the header "Cache-Control" is not valid' 97 | ); 98 | 99 | new HeaderCacheControl(['name' => '"invalid value"']); 100 | } 101 | 102 | public function testInvalidParameterNameType() 103 | { 104 | $this->expectException(\InvalidArgumentException::class); 105 | 106 | $this->expectExceptionMessage( 107 | 'The parameter-name "" for the header "Cache-Control" is not valid' 108 | ); 109 | 110 | new HeaderCacheControl([0 => 'value']); 111 | } 112 | 113 | public function testInvalidParameterValueType() 114 | { 115 | $this->expectException(\InvalidArgumentException::class); 116 | 117 | $this->expectExceptionMessage( 118 | 'The parameter-value "" for the header "Cache-Control" is not valid' 119 | ); 120 | 121 | new HeaderCacheControl(['name' => []]); 122 | } 123 | 124 | public function testBuild() 125 | { 126 | $header = new HeaderCacheControl(['foo' => 'bar']); 127 | 128 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 129 | 130 | $this->assertSame($expected, $header->__toString()); 131 | } 132 | 133 | public function testIterator() 134 | { 135 | $header = new HeaderCacheControl(['foo' => 'bar']); 136 | $iterator = $header->getIterator(); 137 | 138 | $iterator->rewind(); 139 | $this->assertSame($header->getFieldName(), $iterator->current()); 140 | 141 | $iterator->next(); 142 | $this->assertSame($header->getFieldValue(), $iterator->current()); 143 | 144 | $iterator->next(); 145 | $this->assertFalse($iterator->valid()); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /tests/HeaderClearSiteDataTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderClearSiteData('foo'); 21 | 22 | $this->assertSame('Clear-Site-Data', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderClearSiteData('foo'); 28 | 29 | $this->assertSame('"foo"', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderClearSiteData('foo', 'bar', 'baz'); 35 | 36 | $this->assertSame('"foo", "bar", "baz"', $header->getFieldValue()); 37 | } 38 | 39 | public function testEmptyValue() 40 | { 41 | $header = new HeaderClearSiteData(''); 42 | 43 | $this->assertSame('""', $header->getFieldValue()); 44 | } 45 | 46 | public function testEmptyValueAmongOthers() 47 | { 48 | $header = new HeaderClearSiteData('foo', '', 'baz'); 49 | 50 | $this->assertSame('"foo", "", "baz"', $header->getFieldValue()); 51 | } 52 | 53 | public function testInvalidValue() 54 | { 55 | $this->expectException(\InvalidArgumentException::class); 56 | $this->expectExceptionMessage('The value ""invalid value"" for the header "Clear-Site-Data" is not valid'); 57 | 58 | // cannot contain quotes... 59 | new HeaderClearSiteData('"invalid value"'); 60 | } 61 | 62 | public function testInvalidValueAmongOthers() 63 | { 64 | $this->expectException(\InvalidArgumentException::class); 65 | $this->expectExceptionMessage('The value ""bar"" for the header "Clear-Site-Data" is not valid'); 66 | 67 | // cannot contain quotes... 68 | new HeaderClearSiteData('foo', '"bar"', 'baz'); 69 | } 70 | 71 | public function testBuild() 72 | { 73 | $header = new HeaderClearSiteData('foo'); 74 | 75 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 76 | 77 | $this->assertSame($expected, $header->__toString()); 78 | } 79 | 80 | public function testIterator() 81 | { 82 | $header = new HeaderClearSiteData('foo'); 83 | $iterator = $header->getIterator(); 84 | 85 | $iterator->rewind(); 86 | $this->assertSame($header->getFieldName(), $iterator->current()); 87 | 88 | $iterator->next(); 89 | $this->assertSame($header->getFieldValue(), $iterator->current()); 90 | 91 | $iterator->next(); 92 | $this->assertFalse($iterator->valid()); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tests/HeaderConnectionTest.php: -------------------------------------------------------------------------------- 1 | assertSame('close', HeaderConnection::CONNECTION_CLOSE); 14 | $this->assertSame('keep-alive', HeaderConnection::CONNECTION_KEEP_ALIVE); 15 | } 16 | 17 | public function testContracts() 18 | { 19 | $header = new HeaderConnection('foo'); 20 | 21 | $this->assertInstanceOf(HeaderInterface::class, $header); 22 | } 23 | 24 | public function testFieldName() 25 | { 26 | $header = new HeaderConnection('foo'); 27 | 28 | $this->assertSame('Connection', $header->getFieldName()); 29 | } 30 | 31 | public function testFieldValue() 32 | { 33 | $header = new HeaderConnection('foo'); 34 | 35 | $this->assertSame('foo', $header->getFieldValue()); 36 | } 37 | 38 | public function testEmptyValue() 39 | { 40 | $this->expectException(\InvalidArgumentException::class); 41 | $this->expectExceptionMessage('The value "" for the header "Connection" is not valid'); 42 | 43 | new HeaderConnection(''); 44 | } 45 | 46 | public function testInvalidValue() 47 | { 48 | $this->expectException(\InvalidArgumentException::class); 49 | $this->expectExceptionMessage('The value "@" for the header "Connection" is not valid'); 50 | 51 | // isn't a token... 52 | new HeaderConnection('@'); 53 | } 54 | 55 | public function testBuild() 56 | { 57 | $header = new HeaderConnection('foo'); 58 | 59 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 60 | 61 | $this->assertSame($expected, $header->__toString()); 62 | } 63 | 64 | public function testIterator() 65 | { 66 | $header = new HeaderConnection('foo'); 67 | $iterator = $header->getIterator(); 68 | 69 | $iterator->rewind(); 70 | $this->assertSame($header->getFieldName(), $iterator->current()); 71 | 72 | $iterator->next(); 73 | $this->assertSame($header->getFieldValue(), $iterator->current()); 74 | 75 | $iterator->next(); 76 | $this->assertFalse($iterator->valid()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/HeaderContentDispositionTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentDisposition('foo'); 21 | 22 | $this->assertSame('Content-Disposition', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentDisposition('foo'); 28 | 29 | $this->assertSame('foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithEmptyValue() 33 | { 34 | $header = new HeaderContentDisposition('foo', [ 35 | 'bar' => '', 36 | ]); 37 | 38 | $this->assertSame('foo; bar=""', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithToken() 42 | { 43 | $header = new HeaderContentDisposition('foo', [ 44 | 'bar' => 'token', 45 | ]); 46 | 47 | $this->assertSame('foo; bar="token"', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithQuotedString() 51 | { 52 | $header = new HeaderContentDisposition('foo', [ 53 | 'bar' => 'quoted string', 54 | ]); 55 | 56 | $this->assertSame('foo; bar="quoted string"', $header->getFieldValue()); 57 | } 58 | 59 | public function testParameterWithInteger() 60 | { 61 | $header = new HeaderContentDisposition('foo', [ 62 | 'bar' => 1, 63 | ]); 64 | 65 | $this->assertSame('foo; bar="1"', $header->getFieldValue()); 66 | } 67 | 68 | public function testSeveralParameters() 69 | { 70 | $header = new HeaderContentDisposition('foo', [ 71 | 'bar' => '', 72 | 'baz' => 'token', 73 | 'bat' => 'quoted string', 74 | 'qux' => 1, 75 | ]); 76 | 77 | $this->assertSame('foo; bar=""; baz="token"; bat="quoted string"; qux="1"', $header->getFieldValue()); 78 | } 79 | 80 | public function testEmptyValue() 81 | { 82 | $this->expectException(\InvalidArgumentException::class); 83 | $this->expectExceptionMessage('The value "" for the header "Content-Disposition" is not valid'); 84 | 85 | new HeaderContentDisposition(''); 86 | } 87 | 88 | public function testInvalidValue() 89 | { 90 | $this->expectException(\InvalidArgumentException::class); 91 | $this->expectExceptionMessage('The value "@" for the header "Content-Disposition" is not valid'); 92 | 93 | // isn't a token... 94 | new HeaderContentDisposition('@'); 95 | } 96 | 97 | public function testInvalidParameterName() 98 | { 99 | $this->expectException(\InvalidArgumentException::class); 100 | 101 | $this->expectExceptionMessage( 102 | 'The parameter-name "invalid name" for the header "Content-Disposition" is not valid' 103 | ); 104 | 105 | // cannot contain spaces... 106 | new HeaderContentDisposition('foo', ['invalid name' => 'value']); 107 | } 108 | 109 | public function testInvalidParameterNameType() 110 | { 111 | $this->expectException(\InvalidArgumentException::class); 112 | 113 | $this->expectExceptionMessage( 114 | 'The parameter-name "" for the header "Content-Disposition" is not valid' 115 | ); 116 | 117 | new HeaderContentDisposition('foo', [0 => 'value']); 118 | } 119 | 120 | public function testInvalidParameterValue() 121 | { 122 | $this->expectException(\InvalidArgumentException::class); 123 | 124 | $this->expectExceptionMessage( 125 | 'The parameter-value ""invalid value"" for the header "Content-Disposition" is not valid' 126 | ); 127 | 128 | // cannot contain quotes... 129 | new HeaderContentDisposition('foo', ['name' => '"invalid value"']); 130 | } 131 | 132 | public function testInvalidParameterValueType() 133 | { 134 | $this->expectException(\InvalidArgumentException::class); 135 | 136 | $this->expectExceptionMessage( 137 | 'The parameter-value "" for the header "Content-Disposition" is not valid' 138 | ); 139 | 140 | new HeaderContentDisposition('foo', ['name' => []]); 141 | } 142 | 143 | public function testBuild() 144 | { 145 | $header = new HeaderContentDisposition('foo', ['bar' => 'baz']); 146 | 147 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 148 | 149 | $this->assertSame($expected, $header->__toString()); 150 | } 151 | 152 | public function testIterator() 153 | { 154 | $header = new HeaderContentDisposition('foo', ['bar' => 'baz']); 155 | $iterator = $header->getIterator(); 156 | 157 | $iterator->rewind(); 158 | $this->assertSame($header->getFieldName(), $iterator->current()); 159 | 160 | $iterator->next(); 161 | $this->assertSame($header->getFieldValue(), $iterator->current()); 162 | 163 | $iterator->next(); 164 | $this->assertFalse($iterator->valid()); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /tests/HeaderContentEncodingTest.php: -------------------------------------------------------------------------------- 1 | assertSame('gzip', HeaderContentEncoding::GZIP); 14 | $this->assertSame('compress', HeaderContentEncoding::COMPRESS); 15 | $this->assertSame('deflate', HeaderContentEncoding::DEFLATE); 16 | $this->assertSame('br', HeaderContentEncoding::BR); 17 | } 18 | 19 | public function testContracts() 20 | { 21 | $header = new HeaderContentEncoding('foo'); 22 | 23 | $this->assertInstanceOf(HeaderInterface::class, $header); 24 | } 25 | 26 | public function testFieldName() 27 | { 28 | $header = new HeaderContentEncoding('foo'); 29 | 30 | $this->assertSame('Content-Encoding', $header->getFieldName()); 31 | } 32 | 33 | public function testFieldValue() 34 | { 35 | $header = new HeaderContentEncoding('foo'); 36 | 37 | $this->assertSame('foo', $header->getFieldValue()); 38 | } 39 | 40 | public function testSeveralValues() 41 | { 42 | $header = new HeaderContentEncoding('foo', 'bar', 'baz'); 43 | 44 | $this->assertSame('foo, bar, baz', $header->getFieldValue()); 45 | } 46 | 47 | public function testEmptyValue() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Content-Encoding" is not valid'); 51 | 52 | new HeaderContentEncoding(''); 53 | } 54 | 55 | public function testEmptyValueAmongOthers() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "" for the header "Content-Encoding" is not valid'); 59 | 60 | new HeaderContentEncoding('foo', '', 'bar'); 61 | } 62 | 63 | public function testInvalidValue() 64 | { 65 | $this->expectException(\InvalidArgumentException::class); 66 | $this->expectExceptionMessage('The value "foo=" for the header "Content-Encoding" is not valid'); 67 | 68 | // a token cannot contain the "=" character... 69 | new HeaderContentEncoding('foo='); 70 | } 71 | 72 | public function testInvalidValueAmongOthers() 73 | { 74 | $this->expectException(\InvalidArgumentException::class); 75 | $this->expectExceptionMessage('The value "bar=" for the header "Content-Encoding" is not valid'); 76 | 77 | // a token cannot contain the "=" character... 78 | new HeaderContentEncoding('foo', 'bar=', 'bar'); 79 | } 80 | 81 | public function testBuild() 82 | { 83 | $header = new HeaderContentEncoding('foo'); 84 | 85 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 86 | 87 | $this->assertSame($expected, $header->__toString()); 88 | } 89 | 90 | public function testIterator() 91 | { 92 | $header = new HeaderContentEncoding('foo'); 93 | $iterator = $header->getIterator(); 94 | 95 | $iterator->rewind(); 96 | $this->assertSame($header->getFieldName(), $iterator->current()); 97 | 98 | $iterator->next(); 99 | $this->assertSame($header->getFieldValue(), $iterator->current()); 100 | 101 | $iterator->next(); 102 | $this->assertFalse($iterator->valid()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/HeaderContentLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentLanguage('foo'); 21 | 22 | $this->assertSame('Content-Language', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentLanguage('foo'); 28 | 29 | $this->assertSame('foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderContentLanguage('foo', 'bar', 'baz'); 35 | 36 | $this->assertSame('foo, bar, baz', $header->getFieldValue()); 37 | } 38 | 39 | public function testEmptyValue() 40 | { 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage('The value "" for the header "Content-Language" is not valid'); 43 | 44 | new HeaderContentLanguage(''); 45 | } 46 | 47 | public function testEmptyValueAmongOthers() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Content-Language" is not valid'); 51 | 52 | new HeaderContentLanguage('foo', '', 'baz'); 53 | } 54 | 55 | public function testInvalidValue() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "@" for the header "Content-Language" is not valid'); 59 | 60 | // isn't a token... 61 | new HeaderContentLanguage('@'); 62 | } 63 | 64 | public function testInvalidValueAmongOthers() 65 | { 66 | $this->expectException(\InvalidArgumentException::class); 67 | $this->expectExceptionMessage('The value "@" for the header "Content-Language" is not valid'); 68 | 69 | // isn't a token... 70 | new HeaderContentLanguage('foo', '@', 'baz'); 71 | } 72 | 73 | public function testInvalidValueLength() 74 | { 75 | $this->expectException(\InvalidArgumentException::class); 76 | $this->expectExceptionMessage('The value "VERYLONGWORD" for the header "Content-Language" is not valid'); 77 | 78 | // isn't a token... 79 | new HeaderContentLanguage('VERYLONGWORD'); 80 | } 81 | 82 | public function testInvalidValueLengthAmongOthers() 83 | { 84 | $this->expectException(\InvalidArgumentException::class); 85 | $this->expectExceptionMessage('The value "VERYLONGWORD" for the header "Content-Language" is not valid'); 86 | 87 | // isn't a token... 88 | new HeaderContentLanguage('foo', 'VERYLONGWORD', 'baz'); 89 | } 90 | 91 | public function testBuild() 92 | { 93 | $header = new HeaderContentLanguage('foo'); 94 | 95 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 96 | 97 | $this->assertSame($expected, $header->__toString()); 98 | } 99 | 100 | public function testIterator() 101 | { 102 | $header = new HeaderContentLanguage('foo'); 103 | $iterator = $header->getIterator(); 104 | 105 | $iterator->rewind(); 106 | $this->assertSame($header->getFieldName(), $iterator->current()); 107 | 108 | $iterator->next(); 109 | $this->assertSame($header->getFieldValue(), $iterator->current()); 110 | 111 | $iterator->next(); 112 | $this->assertFalse($iterator->valid()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /tests/HeaderContentLengthTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentLength(0); 21 | 22 | $this->assertSame('Content-Length', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentLength(0); 28 | 29 | $this->assertSame('0', $header->getFieldValue()); 30 | } 31 | 32 | public function testInvalidValue() 33 | { 34 | $this->expectException(\InvalidArgumentException::class); 35 | $this->expectExceptionMessage('The value "-1" for the header "Content-Length" is not valid'); 36 | 37 | new HeaderContentLength(-1); 38 | } 39 | 40 | public function testBuild() 41 | { 42 | $header = new HeaderContentLength(0); 43 | 44 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 45 | 46 | $this->assertSame($expected, $header->__toString()); 47 | } 48 | 49 | public function testIterator() 50 | { 51 | $header = new HeaderContentLength(0); 52 | $iterator = $header->getIterator(); 53 | 54 | $iterator->rewind(); 55 | $this->assertSame($header->getFieldName(), $iterator->current()); 56 | 57 | $iterator->next(); 58 | $this->assertSame($header->getFieldValue(), $iterator->current()); 59 | 60 | $iterator->next(); 61 | $this->assertFalse($iterator->valid()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/HeaderContentLocationTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $uri = new Uri('/'); 23 | $header = new HeaderContentLocation($uri); 24 | 25 | $this->assertSame('Content-Location', $header->getFieldName()); 26 | } 27 | 28 | public function testFieldValue() 29 | { 30 | $uri = new Uri('/'); 31 | $header = new HeaderContentLocation($uri); 32 | 33 | $this->assertSame('/', $header->getFieldValue()); 34 | } 35 | 36 | public function testBuild() 37 | { 38 | $uri = new Uri('/'); 39 | $header = new HeaderContentLocation($uri); 40 | 41 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 42 | 43 | $this->assertSame($expected, $header->__toString()); 44 | } 45 | 46 | public function testIterator() 47 | { 48 | $uri = new Uri('/'); 49 | $header = new HeaderContentLocation($uri); 50 | $iterator = $header->getIterator(); 51 | 52 | $iterator->rewind(); 53 | $this->assertSame($header->getFieldName(), $iterator->current()); 54 | 55 | $iterator->next(); 56 | $this->assertSame($header->getFieldValue(), $iterator->current()); 57 | 58 | $iterator->next(); 59 | $this->assertFalse($iterator->valid()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/HeaderContentMD5Test.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $header = new HeaderContentMD5(self::TEST_MD5_DIGEST); 23 | 24 | $this->assertSame('Content-MD5', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $header = new HeaderContentMD5(self::TEST_MD5_DIGEST); 30 | 31 | $this->assertSame(self::TEST_MD5_DIGEST, $header->getFieldValue()); 32 | } 33 | 34 | public function testEmptyValue() 35 | { 36 | $this->expectException(\InvalidArgumentException::class); 37 | 38 | $this->expectExceptionMessage( 39 | 'The value "" for the header "Content-MD5" is not valid' 40 | ); 41 | 42 | new HeaderContentMD5(''); 43 | } 44 | 45 | public function testInvalidValue() 46 | { 47 | $this->expectException(\InvalidArgumentException::class); 48 | 49 | $this->expectExceptionMessage( 50 | 'The value "=invalid md5 digest=" for the header "Content-MD5" is not valid' 51 | ); 52 | 53 | new HeaderContentMD5('=invalid md5 digest='); 54 | } 55 | 56 | public function testBuild() 57 | { 58 | $header = new HeaderContentMD5(self::TEST_MD5_DIGEST); 59 | 60 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 61 | 62 | $this->assertSame($expected, $header->__toString()); 63 | } 64 | 65 | public function testIterator() 66 | { 67 | $header = new HeaderContentMD5(self::TEST_MD5_DIGEST); 68 | $iterator = $header->getIterator(); 69 | 70 | $iterator->rewind(); 71 | $this->assertSame($header->getFieldName(), $iterator->current()); 72 | 73 | $iterator->next(); 74 | $this->assertSame($header->getFieldValue(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertFalse($iterator->valid()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/HeaderContentRangeTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentRange(0, 1, 2); 21 | 22 | $this->assertSame('Content-Range', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentRange(0, 1, 2); 28 | 29 | $this->assertSame('bytes 0-1/2', $header->getFieldValue()); 30 | } 31 | 32 | public function testInvalidFirstBytePosition() 33 | { 34 | $this->expectException(\InvalidArgumentException::class); 35 | 36 | $this->expectExceptionMessage( 37 | 'The "first-byte-pos" value of the content range ' . 38 | 'must be less than or equal to the "last-byte-pos" value' 39 | ); 40 | 41 | new HeaderContentRange(2, 1, 2); 42 | } 43 | 44 | public function testInvalidLastBytePosition() 45 | { 46 | $this->expectException(\InvalidArgumentException::class); 47 | 48 | $this->expectExceptionMessage( 49 | 'The "last-byte-pos" value of the content range ' . 50 | 'must be less than the "instance-length" value' 51 | ); 52 | 53 | new HeaderContentRange(0, 2, 2); 54 | } 55 | 56 | public function testInvalidInstanceLength() 57 | { 58 | $this->expectException(\InvalidArgumentException::class); 59 | 60 | $this->expectExceptionMessage( 61 | 'The "last-byte-pos" value of the content range ' . 62 | 'must be less than the "instance-length" value' 63 | ); 64 | 65 | new HeaderContentRange(0, 1, 1); 66 | } 67 | 68 | public function testBuild() 69 | { 70 | $header = new HeaderContentRange(0, 1, 2); 71 | 72 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 73 | 74 | $this->assertSame($expected, $header->__toString()); 75 | } 76 | 77 | public function testIterator() 78 | { 79 | $header = new HeaderContentRange(0, 1, 2); 80 | $iterator = $header->getIterator(); 81 | 82 | $iterator->rewind(); 83 | $this->assertSame($header->getFieldName(), $iterator->current()); 84 | 85 | $iterator->next(); 86 | $this->assertSame($header->getFieldValue(), $iterator->current()); 87 | 88 | $iterator->next(); 89 | $this->assertFalse($iterator->valid()); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /tests/HeaderContentSecurityPolicyReportOnlyTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentSecurityPolicyReportOnly([]); 21 | 22 | $this->assertSame('Content-Security-Policy-Report-Only', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentSecurityPolicyReportOnly([]); 28 | 29 | $this->assertSame('', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithoutValue() 33 | { 34 | $header = new HeaderContentSecurityPolicyReportOnly([ 35 | 'foo' => '', 36 | ]); 37 | 38 | $this->assertSame('foo', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithValue() 42 | { 43 | $header = new HeaderContentSecurityPolicyReportOnly([ 44 | 'foo' => 'bar', 45 | ]); 46 | 47 | $this->assertSame('foo bar', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithInteger() 51 | { 52 | $header = new HeaderContentSecurityPolicyReportOnly([ 53 | 'foo' => 1, 54 | ]); 55 | 56 | $this->assertSame('foo 1', $header->getFieldValue()); 57 | } 58 | 59 | public function testSeveralParameters() 60 | { 61 | $header = new HeaderContentSecurityPolicyReportOnly([ 62 | 'foo' => '', 63 | 'bar' => 'bat', 64 | 'baz' => 1, 65 | ]); 66 | 67 | $this->assertSame('foo; bar bat; baz 1', $header->getFieldValue()); 68 | } 69 | 70 | public function testInvalidParameterName() 71 | { 72 | $this->expectException(\InvalidArgumentException::class); 73 | 74 | $this->expectExceptionMessage( 75 | 'The parameter-name "name=" for the header "Content-Security-Policy-Report-Only" is not valid' 76 | ); 77 | 78 | new HeaderContentSecurityPolicyReportOnly(['name=' => 'value']); 79 | } 80 | 81 | public function testInvalidParameterNameType() 82 | { 83 | $this->expectException(\InvalidArgumentException::class); 84 | 85 | $this->expectExceptionMessage( 86 | 'The parameter-name "" for the header "Content-Security-Policy-Report-Only" is not valid' 87 | ); 88 | 89 | new HeaderContentSecurityPolicyReportOnly([0 => 'value']); 90 | } 91 | 92 | public function testInvalidParameterValue() 93 | { 94 | $this->expectException(\InvalidArgumentException::class); 95 | 96 | $this->expectExceptionMessage( 97 | 'The parameter-value ";value" for the header "Content-Security-Policy-Report-Only" is not valid' 98 | ); 99 | 100 | new HeaderContentSecurityPolicyReportOnly(['name' => ';value']); 101 | } 102 | 103 | public function testInvalidParameterValueType() 104 | { 105 | $this->expectException(\InvalidArgumentException::class); 106 | 107 | $this->expectExceptionMessage( 108 | 'The parameter-value "" for the header "Content-Security-Policy-Report-Only" is not valid' 109 | ); 110 | 111 | new HeaderContentSecurityPolicyReportOnly(['name' => []]); 112 | } 113 | 114 | public function testBuild() 115 | { 116 | $header = new HeaderContentSecurityPolicyReportOnly(['foo' => 'bar']); 117 | 118 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 119 | 120 | $this->assertSame($expected, $header->__toString()); 121 | } 122 | 123 | public function testIterator() 124 | { 125 | $header = new HeaderContentSecurityPolicyReportOnly(['foo' => 'bar']); 126 | $iterator = $header->getIterator(); 127 | 128 | $iterator->rewind(); 129 | $this->assertSame($header->getFieldName(), $iterator->current()); 130 | 131 | $iterator->next(); 132 | $this->assertSame($header->getFieldValue(), $iterator->current()); 133 | 134 | $iterator->next(); 135 | $this->assertFalse($iterator->valid()); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /tests/HeaderContentSecurityPolicyTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentSecurityPolicy([]); 21 | 22 | $this->assertSame('Content-Security-Policy', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentSecurityPolicy([]); 28 | 29 | $this->assertSame('', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithoutValue() 33 | { 34 | $header = new HeaderContentSecurityPolicy([ 35 | 'foo' => '', 36 | ]); 37 | 38 | $this->assertSame('foo', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithValue() 42 | { 43 | $header = new HeaderContentSecurityPolicy([ 44 | 'foo' => 'bar', 45 | ]); 46 | 47 | $this->assertSame('foo bar', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithInteger() 51 | { 52 | $header = new HeaderContentSecurityPolicy([ 53 | 'foo' => 1, 54 | ]); 55 | 56 | $this->assertSame('foo 1', $header->getFieldValue()); 57 | } 58 | 59 | public function testSeveralParameters() 60 | { 61 | $header = new HeaderContentSecurityPolicy([ 62 | 'foo' => '', 63 | 'bar' => 'bat', 64 | 'baz' => 1, 65 | ]); 66 | 67 | $this->assertSame('foo; bar bat; baz 1', $header->getFieldValue()); 68 | } 69 | 70 | public function testInvalidParameterName() 71 | { 72 | $this->expectException(\InvalidArgumentException::class); 73 | 74 | $this->expectExceptionMessage( 75 | 'The parameter-name "name=" for the header "Content-Security-Policy" is not valid' 76 | ); 77 | 78 | new HeaderContentSecurityPolicy(['name=' => 'value']); 79 | } 80 | 81 | public function testInvalidParameterNameType() 82 | { 83 | $this->expectException(\InvalidArgumentException::class); 84 | 85 | $this->expectExceptionMessage( 86 | 'The parameter-name "" for the header "Content-Security-Policy" is not valid' 87 | ); 88 | 89 | new HeaderContentSecurityPolicy([0 => 'value']); 90 | } 91 | 92 | public function testInvalidParameterValue() 93 | { 94 | $this->expectException(\InvalidArgumentException::class); 95 | 96 | $this->expectExceptionMessage( 97 | 'The parameter-value ";value" for the header "Content-Security-Policy" is not valid' 98 | ); 99 | 100 | new HeaderContentSecurityPolicy(['name' => ';value']); 101 | } 102 | 103 | public function testInvalidParameterValueType() 104 | { 105 | $this->expectException(\InvalidArgumentException::class); 106 | 107 | $this->expectExceptionMessage( 108 | 'The parameter-value "" for the header "Content-Security-Policy" is not valid' 109 | ); 110 | 111 | new HeaderContentSecurityPolicy(['name' => []]); 112 | } 113 | 114 | public function testBuild() 115 | { 116 | $header = new HeaderContentSecurityPolicy(['foo' => 'bar']); 117 | 118 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 119 | 120 | $this->assertSame($expected, $header->__toString()); 121 | } 122 | 123 | public function testIterator() 124 | { 125 | $header = new HeaderContentSecurityPolicy(['foo' => 'bar']); 126 | $iterator = $header->getIterator(); 127 | 128 | $iterator->rewind(); 129 | $this->assertSame($header->getFieldName(), $iterator->current()); 130 | 131 | $iterator->next(); 132 | $this->assertSame($header->getFieldValue(), $iterator->current()); 133 | 134 | $iterator->next(); 135 | $this->assertFalse($iterator->valid()); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /tests/HeaderContentTypeTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderContentType('foo'); 21 | 22 | $this->assertSame('Content-Type', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderContentType('foo'); 28 | 29 | $this->assertSame('foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithEmptyValue() 33 | { 34 | $header = new HeaderContentType('foo', [ 35 | 'bar' => '', 36 | ]); 37 | 38 | $this->assertSame('foo; bar=""', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithToken() 42 | { 43 | $header = new HeaderContentType('foo', [ 44 | 'bar' => 'token', 45 | ]); 46 | 47 | $this->assertSame('foo; bar="token"', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithQuotedString() 51 | { 52 | $header = new HeaderContentType('foo', [ 53 | 'bar' => 'quoted string', 54 | ]); 55 | 56 | $this->assertSame('foo; bar="quoted string"', $header->getFieldValue()); 57 | } 58 | 59 | public function testParameterWithInteger() 60 | { 61 | $header = new HeaderContentType('foo', [ 62 | 'bar' => 1, 63 | ]); 64 | 65 | $this->assertSame('foo; bar="1"', $header->getFieldValue()); 66 | } 67 | 68 | public function testSeveralParameters() 69 | { 70 | $header = new HeaderContentType('foo', [ 71 | 'bar' => '', 72 | 'baz' => 'token', 73 | 'bat' => 'quoted string', 74 | 'qux' => 1, 75 | ]); 76 | 77 | $this->assertSame('foo; bar=""; baz="token"; bat="quoted string"; qux="1"', $header->getFieldValue()); 78 | } 79 | 80 | public function testEmptyValue() 81 | { 82 | $this->expectException(\InvalidArgumentException::class); 83 | $this->expectExceptionMessage('The value "" for the header "Content-Type" is not valid'); 84 | 85 | new HeaderContentType(''); 86 | } 87 | 88 | public function testInvalidValue() 89 | { 90 | $this->expectException(\InvalidArgumentException::class); 91 | $this->expectExceptionMessage('The value "@" for the header "Content-Type" is not valid'); 92 | 93 | // isn't a token... 94 | new HeaderContentType('@'); 95 | } 96 | 97 | public function testInvalidParameterName() 98 | { 99 | $this->expectException(\InvalidArgumentException::class); 100 | 101 | $this->expectExceptionMessage( 102 | 'The parameter-name "invalid name" for the header "Content-Type" is not valid' 103 | ); 104 | 105 | // cannot contain spaces... 106 | new HeaderContentType('foo', ['invalid name' => 'value']); 107 | } 108 | 109 | public function testInvalidParameterNameType() 110 | { 111 | $this->expectException(\InvalidArgumentException::class); 112 | 113 | $this->expectExceptionMessage( 114 | 'The parameter-name "" for the header "Content-Type" is not valid' 115 | ); 116 | 117 | // cannot contain spaces... 118 | new HeaderContentType('foo', [0 => 'value']); 119 | } 120 | 121 | public function testInvalidParameterValue() 122 | { 123 | $this->expectException(\InvalidArgumentException::class); 124 | 125 | $this->expectExceptionMessage( 126 | 'The parameter-value ""invalid value"" for the header "Content-Type" is not valid' 127 | ); 128 | 129 | // cannot contain quotes... 130 | new HeaderContentType('foo', ['name' => '"invalid value"']); 131 | } 132 | 133 | public function testInvalidParameterValueType() 134 | { 135 | $this->expectException(\InvalidArgumentException::class); 136 | 137 | $this->expectExceptionMessage( 138 | 'The parameter-value "" for the header "Content-Type" is not valid' 139 | ); 140 | 141 | // cannot contain quotes... 142 | new HeaderContentType('foo', ['name' => []]); 143 | } 144 | 145 | public function testBuild() 146 | { 147 | $header = new HeaderContentType('foo', ['bar' => 'baz']); 148 | 149 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 150 | 151 | $this->assertSame($expected, $header->__toString()); 152 | } 153 | 154 | public function testIterator() 155 | { 156 | $header = new HeaderContentType('foo', ['bar' => 'baz']); 157 | $iterator = $header->getIterator(); 158 | 159 | $iterator->rewind(); 160 | $this->assertSame($header->getFieldName(), $iterator->current()); 161 | 162 | $iterator->next(); 163 | $this->assertSame($header->getFieldValue(), $iterator->current()); 164 | 165 | $iterator->next(); 166 | $this->assertFalse($iterator->valid()); 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /tests/HeaderCookieTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderCookie(); 21 | 22 | $this->assertSame('Cookie', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderCookie([ 28 | 'foo' => 'bar', 29 | 'bar' => 'baz', 30 | 'baz' => [ 31 | 'qux', 32 | ], 33 | ]); 34 | 35 | $this->assertSame('foo=bar; bar=baz; baz%5B0%5D=qux', $header->getFieldValue()); 36 | } 37 | 38 | public function testBuild() 39 | { 40 | $header = new HeaderCookie(['foo' => 'bar']); 41 | 42 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 43 | 44 | $this->assertSame($expected, $header->__toString()); 45 | } 46 | 47 | public function testIterator() 48 | { 49 | $header = new HeaderCookie(['foo' => 'bar']); 50 | $iterator = $header->getIterator(); 51 | 52 | $iterator->rewind(); 53 | $this->assertSame($header->getFieldName(), $iterator->current()); 54 | 55 | $iterator->next(); 56 | $this->assertSame($header->getFieldValue(), $iterator->current()); 57 | 58 | $iterator->next(); 59 | $this->assertFalse($iterator->valid()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/HeaderCustomTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderCustom('foo', 'bar'); 21 | 22 | $this->assertEquals('foo', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderCustom('foo', 'bar'); 28 | 29 | $this->assertEquals('bar', $header->getFieldValue()); 30 | } 31 | 32 | public function testInvalidFieldName() 33 | { 34 | $this->expectException(\InvalidArgumentException::class); 35 | $this->expectExceptionMessage('Header field-name is invalid'); 36 | 37 | new HeaderCustom('@', 'value'); 38 | } 39 | 40 | public function testInvalidFieldNameType() 41 | { 42 | $this->expectException(\InvalidArgumentException::class); 43 | $this->expectExceptionMessage('Header field-name must be a string'); 44 | 45 | new HeaderCustom([], 'value'); 46 | } 47 | 48 | public function testEmptyFieldValue() 49 | { 50 | $header = new HeaderCustom('foo', ''); 51 | 52 | $this->assertEquals('', $header->getFieldValue()); 53 | } 54 | 55 | public function testInvalidFieldValue() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('Header field-value is invalid'); 59 | 60 | new HeaderCustom('foo', "\0"); 61 | } 62 | 63 | public function testInvalidFieldValueType() 64 | { 65 | $this->expectException(\InvalidArgumentException::class); 66 | $this->expectExceptionMessage('Header field-value must be a string'); 67 | 68 | new HeaderCustom('foo', []); 69 | } 70 | 71 | public function testBuild() 72 | { 73 | $header = new HeaderCustom('foo', 'bar'); 74 | 75 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 76 | 77 | $this->assertSame($expected, $header->__toString()); 78 | } 79 | 80 | public function testIterator() 81 | { 82 | $header = new HeaderCustom('foo', 'bar'); 83 | $iterator = $header->getIterator(); 84 | 85 | $iterator->rewind(); 86 | $this->assertSame($header->getFieldName(), $iterator->current()); 87 | 88 | $iterator->next(); 89 | $this->assertSame($header->getFieldValue(), $iterator->current()); 90 | 91 | $iterator->next(); 92 | $this->assertFalse($iterator->valid()); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tests/HeaderDateTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 17 | } 18 | 19 | public function testFieldName() 20 | { 21 | $utc = new \DateTime('utc'); 22 | $header = new HeaderDate($utc); 23 | 24 | $this->assertSame('Date', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $utc = new \DateTime('utc'); 30 | $header = new HeaderDate($utc); 31 | 32 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 33 | } 34 | 35 | public function testFieldValueWithMutableDateTime() 36 | { 37 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 38 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 39 | 40 | $header = new HeaderDate($now); 41 | 42 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 43 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 44 | } 45 | 46 | public function testFieldValueWithImmutableDateTime() 47 | { 48 | $now = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow')); 49 | $utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); 50 | 51 | $header = new HeaderDate($now); 52 | 53 | $this->assertSame($utc->format(\DateTimeImmutable::RFC822), $header->getFieldValue()); 54 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 55 | } 56 | 57 | public function testBuild() 58 | { 59 | $now = new \DateTime('now'); 60 | $header = new HeaderDate($now); 61 | 62 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 63 | 64 | $this->assertSame($expected, $header->__toString()); 65 | } 66 | 67 | public function testIterator() 68 | { 69 | $now = new \DateTime('now'); 70 | $header = new HeaderDate($now); 71 | $iterator = $header->getIterator(); 72 | 73 | $iterator->rewind(); 74 | $this->assertSame($header->getFieldName(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertSame($header->getFieldValue(), $iterator->current()); 78 | 79 | $iterator->next(); 80 | $this->assertFalse($iterator->valid()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/HeaderEtagTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderEtag('foo'); 21 | 22 | $this->assertSame('ETag', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderEtag('foo'); 28 | 29 | $this->assertSame('"foo"', $header->getFieldValue()); 30 | } 31 | 32 | public function testEmptyValue() 33 | { 34 | $header = new HeaderEtag(''); 35 | 36 | $this->assertSame('""', $header->getFieldValue()); 37 | } 38 | 39 | public function testInvalidValue() 40 | { 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage('The value ""invalid value"" for the header "ETag" is not valid'); 43 | 44 | // cannot contain quotes... 45 | new HeaderEtag('"invalid value"'); 46 | } 47 | 48 | public function testBuild() 49 | { 50 | $header = new HeaderEtag('foo'); 51 | 52 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 53 | 54 | $this->assertSame($expected, $header->__toString()); 55 | } 56 | 57 | public function testIterator() 58 | { 59 | $header = new HeaderEtag('foo'); 60 | $iterator = $header->getIterator(); 61 | 62 | $iterator->rewind(); 63 | $this->assertSame($header->getFieldName(), $iterator->current()); 64 | 65 | $iterator->next(); 66 | $this->assertSame($header->getFieldValue(), $iterator->current()); 67 | 68 | $iterator->next(); 69 | $this->assertFalse($iterator->valid()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tests/HeaderExpiresTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 17 | } 18 | 19 | public function testFieldName() 20 | { 21 | $utc = new \DateTime('utc'); 22 | $header = new HeaderExpires($utc); 23 | 24 | $this->assertSame('Expires', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $utc = new \DateTime('utc'); 30 | $header = new HeaderExpires($utc); 31 | 32 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 33 | } 34 | 35 | public function testFieldValueWithMutableDateTime() 36 | { 37 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 38 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 39 | 40 | $header = new HeaderExpires($now); 41 | 42 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 43 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 44 | } 45 | 46 | public function testFieldValueWithImmutableDateTime() 47 | { 48 | $now = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow')); 49 | $utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); 50 | 51 | $header = new HeaderExpires($now); 52 | 53 | $this->assertSame($utc->format(\DateTimeImmutable::RFC822), $header->getFieldValue()); 54 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 55 | } 56 | 57 | public function testBuild() 58 | { 59 | $now = new \DateTime('now'); 60 | $header = new HeaderExpires($now); 61 | 62 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 63 | 64 | $this->assertSame($expected, $header->__toString()); 65 | } 66 | 67 | public function testIterator() 68 | { 69 | $now = new \DateTime('now'); 70 | $header = new HeaderExpires($now); 71 | $iterator = $header->getIterator(); 72 | 73 | $iterator->rewind(); 74 | $this->assertSame($header->getFieldName(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertSame($header->getFieldValue(), $iterator->current()); 78 | 79 | $iterator->next(); 80 | $this->assertFalse($iterator->valid()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/HeaderKeepAliveTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderKeepAlive(); 21 | 22 | $this->assertSame('Keep-Alive', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderKeepAlive(); 28 | 29 | $this->assertSame('', $header->getFieldValue()); 30 | } 31 | 32 | public function testParameterWithEmptyValue() 33 | { 34 | $header = new HeaderKeepAlive([ 35 | 'foo' => '', 36 | ]); 37 | 38 | $this->assertSame('foo', $header->getFieldValue()); 39 | } 40 | 41 | public function testParameterWithToken() 42 | { 43 | $header = new HeaderKeepAlive([ 44 | 'foo' => 'token', 45 | ]); 46 | 47 | $this->assertSame('foo=token', $header->getFieldValue()); 48 | } 49 | 50 | public function testParameterWithQuotedString() 51 | { 52 | $header = new HeaderKeepAlive([ 53 | 'foo' => 'quoted string', 54 | ]); 55 | 56 | $this->assertSame('foo="quoted string"', $header->getFieldValue()); 57 | } 58 | 59 | public function testParameterWithInteger() 60 | { 61 | $header = new HeaderKeepAlive([ 62 | 'foo' => 1, 63 | ]); 64 | 65 | $this->assertSame('foo=1', $header->getFieldValue()); 66 | } 67 | 68 | public function testSeveralParameters() 69 | { 70 | $header = new HeaderKeepAlive([ 71 | 'foo' => '', 72 | 'bar' => 'token', 73 | 'baz' => 'quoted string', 74 | 'qux' => 1, 75 | ]); 76 | 77 | $this->assertSame('foo, bar=token, baz="quoted string", qux=1', $header->getFieldValue()); 78 | } 79 | 80 | public function testInvalidParameterName() 81 | { 82 | $this->expectException(\InvalidArgumentException::class); 83 | 84 | $this->expectExceptionMessage( 85 | 'The parameter-name "invalid name" for the header "Keep-Alive" is not valid' 86 | ); 87 | 88 | // cannot contain spaces... 89 | new HeaderKeepAlive(['invalid name' => 'value']); 90 | } 91 | 92 | public function testInvalidParameterNameType() 93 | { 94 | $this->expectException(\InvalidArgumentException::class); 95 | 96 | $this->expectExceptionMessage( 97 | 'The parameter-name "" for the header "Keep-Alive" is not valid' 98 | ); 99 | 100 | new HeaderKeepAlive([0 => 'value']); 101 | } 102 | 103 | public function testInvalidParameterValue() 104 | { 105 | $this->expectException(\InvalidArgumentException::class); 106 | 107 | $this->expectExceptionMessage( 108 | 'The parameter-value ""invalid value"" for the header "Keep-Alive" is not valid' 109 | ); 110 | 111 | // cannot contain quotes... 112 | new HeaderKeepAlive(['name' => '"invalid value"']); 113 | } 114 | 115 | public function testInvalidParameterValueType() 116 | { 117 | $this->expectException(\InvalidArgumentException::class); 118 | 119 | $this->expectExceptionMessage( 120 | 'The parameter-value "" for the header "Keep-Alive" is not valid' 121 | ); 122 | 123 | new HeaderKeepAlive(['name' => []]); 124 | } 125 | 126 | public function testBuild() 127 | { 128 | $header = new HeaderKeepAlive(['foo' => 'bar']); 129 | 130 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 131 | 132 | $this->assertSame($expected, $header->__toString()); 133 | } 134 | 135 | public function testIterator() 136 | { 137 | $header = new HeaderKeepAlive(['foo' => 'bar']); 138 | $iterator = $header->getIterator(); 139 | 140 | $iterator->rewind(); 141 | $this->assertSame($header->getFieldName(), $iterator->current()); 142 | 143 | $iterator->next(); 144 | $this->assertSame($header->getFieldValue(), $iterator->current()); 145 | 146 | $iterator->next(); 147 | $this->assertFalse($iterator->valid()); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /tests/HeaderLastModifiedTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 17 | } 18 | 19 | public function testFieldName() 20 | { 21 | $utc = new \DateTime('utc'); 22 | $header = new HeaderLastModified($utc); 23 | 24 | $this->assertSame('Last-Modified', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $utc = new \DateTime('utc'); 30 | $header = new HeaderLastModified($utc); 31 | 32 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 33 | } 34 | 35 | public function testFieldValueWithMutableDateTime() 36 | { 37 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 38 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 39 | 40 | $header = new HeaderLastModified($now); 41 | 42 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 43 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 44 | } 45 | 46 | public function testFieldValueWithImmutableDateTime() 47 | { 48 | $now = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow')); 49 | $utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); 50 | 51 | $header = new HeaderLastModified($now); 52 | 53 | $this->assertSame($utc->format(\DateTimeImmutable::RFC822), $header->getFieldValue()); 54 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 55 | } 56 | 57 | public function testBuild() 58 | { 59 | $now = new \DateTime('now'); 60 | $header = new HeaderLastModified($now); 61 | 62 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 63 | 64 | $this->assertSame($expected, $header->__toString()); 65 | } 66 | 67 | public function testIterator() 68 | { 69 | $now = new \DateTime('now'); 70 | $header = new HeaderLastModified($now); 71 | $iterator = $header->getIterator(); 72 | 73 | $iterator->rewind(); 74 | $this->assertSame($header->getFieldName(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertSame($header->getFieldValue(), $iterator->current()); 78 | 79 | $iterator->next(); 80 | $this->assertFalse($iterator->valid()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/HeaderLinkTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $uri = new Uri('/'); 23 | $header = new HeaderLink($uri); 24 | 25 | $this->assertSame('Link', $header->getFieldName()); 26 | } 27 | 28 | public function testFieldValue() 29 | { 30 | $uri = new Uri('/'); 31 | $header = new HeaderLink($uri); 32 | 33 | $this->assertSame('', $header->getFieldValue()); 34 | } 35 | 36 | public function testParameterWithEmptyValue() 37 | { 38 | $uri = new Uri('/'); 39 | 40 | $header = new HeaderLink($uri, [ 41 | 'foo' => '', 42 | ]); 43 | 44 | $this->assertSame('; foo=""', $header->getFieldValue()); 45 | } 46 | 47 | public function testParameterWithToken() 48 | { 49 | $uri = new Uri('/'); 50 | 51 | $header = new HeaderLink($uri, [ 52 | 'foo' => 'token', 53 | ]); 54 | 55 | $this->assertSame('; foo="token"', $header->getFieldValue()); 56 | } 57 | 58 | public function testParameterWithQuotedString() 59 | { 60 | $uri = new Uri('/'); 61 | 62 | $header = new HeaderLink($uri, [ 63 | 'foo' => 'quoted string', 64 | ]); 65 | 66 | $this->assertSame('; foo="quoted string"', $header->getFieldValue()); 67 | } 68 | 69 | public function testParameterWithInteger() 70 | { 71 | $uri = new Uri('/'); 72 | 73 | $header = new HeaderLink($uri, [ 74 | 'foo' => 1, 75 | ]); 76 | 77 | $this->assertSame('; foo="1"', $header->getFieldValue()); 78 | } 79 | 80 | public function testSeveralParameters() 81 | { 82 | $uri = new Uri('/'); 83 | 84 | $header = new HeaderLink($uri, [ 85 | 'foo' => '', 86 | 'bar' => 'token', 87 | 'baz' => 'quoted string', 88 | 'qux' => 1, 89 | ]); 90 | 91 | $this->assertSame('; foo=""; bar="token"; baz="quoted string"; qux="1"', $header->getFieldValue()); 92 | } 93 | 94 | public function testInvalidParameterName() 95 | { 96 | $uri = new Uri('/'); 97 | 98 | $this->expectException(\InvalidArgumentException::class); 99 | 100 | $this->expectExceptionMessage( 101 | 'The parameter-name "invalid name" for the header "Link" is not valid' 102 | ); 103 | 104 | // cannot contain spaces... 105 | new HeaderLink($uri, ['invalid name' => 'value']); 106 | } 107 | 108 | public function testInvalidParameterNameType() 109 | { 110 | $uri = new Uri('/'); 111 | 112 | $this->expectException(\InvalidArgumentException::class); 113 | 114 | $this->expectExceptionMessage( 115 | 'The parameter-name "" for the header "Link" is not valid' 116 | ); 117 | 118 | new HeaderLink($uri, [0 => 'value']); 119 | } 120 | 121 | public function testInvalidParameterValue() 122 | { 123 | $uri = new Uri('/'); 124 | 125 | $this->expectException(\InvalidArgumentException::class); 126 | 127 | $this->expectExceptionMessage( 128 | 'The parameter-value ""invalid value"" for the header "Link" is not valid' 129 | ); 130 | 131 | // cannot contain quotes... 132 | new HeaderLink($uri, ['name' => '"invalid value"']); 133 | } 134 | 135 | public function testInvalidParameterValueType() 136 | { 137 | $uri = new Uri('/'); 138 | 139 | $this->expectException(\InvalidArgumentException::class); 140 | 141 | $this->expectExceptionMessage( 142 | 'The parameter-value "" for the header "Link" is not valid' 143 | ); 144 | 145 | // cannot contain quotes... 146 | new HeaderLink($uri, ['name' => []]); 147 | } 148 | 149 | public function testBuild() 150 | { 151 | $uri = new Uri('/'); 152 | $header = new HeaderLink($uri, ['foo' => 'bar']); 153 | 154 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 155 | 156 | $this->assertSame($expected, $header->__toString()); 157 | } 158 | 159 | public function testIterator() 160 | { 161 | $uri = new Uri('/'); 162 | $header = new HeaderLink($uri, ['foo' => 'bar']); 163 | $iterator = $header->getIterator(); 164 | 165 | $iterator->rewind(); 166 | $this->assertSame($header->getFieldName(), $iterator->current()); 167 | 168 | $iterator->next(); 169 | $this->assertSame($header->getFieldValue(), $iterator->current()); 170 | 171 | $iterator->next(); 172 | $this->assertFalse($iterator->valid()); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /tests/HeaderLocationTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $uri = new Uri('/'); 23 | $header = new HeaderLocation($uri); 24 | 25 | $this->assertSame('Location', $header->getFieldName()); 26 | } 27 | 28 | public function testFieldValue() 29 | { 30 | $uri = new Uri('/'); 31 | $header = new HeaderLocation($uri); 32 | 33 | $this->assertSame('/', $header->getFieldValue()); 34 | } 35 | 36 | public function testBuild() 37 | { 38 | $uri = new Uri('/'); 39 | $header = new HeaderLocation($uri); 40 | 41 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 42 | 43 | $this->assertSame($expected, $header->__toString()); 44 | } 45 | 46 | public function testIterator() 47 | { 48 | $uri = new Uri('/'); 49 | $header = new HeaderLocation($uri); 50 | $iterator = $header->getIterator(); 51 | 52 | $iterator->rewind(); 53 | $this->assertSame($header->getFieldName(), $iterator->current()); 54 | 55 | $iterator->next(); 56 | $this->assertSame($header->getFieldValue(), $iterator->current()); 57 | 58 | $iterator->next(); 59 | $this->assertFalse($iterator->valid()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/HeaderRefreshTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 18 | } 19 | 20 | public function testFieldName() 21 | { 22 | $uri = new Uri('/'); 23 | $header = new HeaderRefresh(0, $uri); 24 | 25 | $this->assertSame('Refresh', $header->getFieldName()); 26 | } 27 | 28 | public function testFieldValue() 29 | { 30 | $uri = new Uri('/'); 31 | $header = new HeaderRefresh(0, $uri); 32 | 33 | $this->assertSame('0; url=/', $header->getFieldValue()); 34 | } 35 | 36 | public function testInvalidDelay() 37 | { 38 | $this->expectException(\InvalidArgumentException::class); 39 | $this->expectExceptionMessage('The delay "-1" for the header "Refresh" is not valid'); 40 | 41 | $uri = new Uri('/'); 42 | 43 | new HeaderRefresh(-1, $uri); 44 | } 45 | 46 | public function testBuild() 47 | { 48 | $uri = new Uri('/'); 49 | $header = new HeaderRefresh(0, $uri); 50 | 51 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 52 | 53 | $this->assertSame($expected, $header->__toString()); 54 | } 55 | 56 | public function testIterator() 57 | { 58 | $uri = new Uri('/'); 59 | $header = new HeaderRefresh(0, $uri); 60 | $iterator = $header->getIterator(); 61 | 62 | $iterator->rewind(); 63 | $this->assertSame($header->getFieldName(), $iterator->current()); 64 | 65 | $iterator->next(); 66 | $this->assertSame($header->getFieldValue(), $iterator->current()); 67 | 68 | $iterator->next(); 69 | $this->assertFalse($iterator->valid()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /tests/HeaderRetryAfterTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 17 | } 18 | 19 | public function testFieldName() 20 | { 21 | $utc = new \DateTime('utc'); 22 | $header = new HeaderRetryAfter($utc); 23 | 24 | $this->assertSame('Retry-After', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $utc = new \DateTime('utc'); 30 | $header = new HeaderRetryAfter($utc); 31 | 32 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 33 | } 34 | 35 | public function testFieldValueWithMutableDateTime() 36 | { 37 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 38 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 39 | 40 | $header = new HeaderRetryAfter($now); 41 | 42 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 43 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 44 | } 45 | 46 | public function testFieldValueWithImmutableDateTime() 47 | { 48 | $now = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow')); 49 | $utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); 50 | 51 | $header = new HeaderRetryAfter($now); 52 | 53 | $this->assertSame($utc->format(\DateTimeImmutable::RFC822), $header->getFieldValue()); 54 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 55 | } 56 | 57 | public function testBuild() 58 | { 59 | $now = new \DateTime('now'); 60 | $header = new HeaderRetryAfter($now); 61 | 62 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 63 | 64 | $this->assertSame($expected, $header->__toString()); 65 | } 66 | 67 | public function testIterator() 68 | { 69 | $now = new \DateTime('now'); 70 | $header = new HeaderRetryAfter($now); 71 | $iterator = $header->getIterator(); 72 | 73 | $iterator->rewind(); 74 | $this->assertSame($header->getFieldName(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertSame($header->getFieldValue(), $iterator->current()); 78 | 79 | $iterator->next(); 80 | $this->assertFalse($iterator->valid()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/HeaderSunsetTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 17 | } 18 | 19 | public function testFieldName() 20 | { 21 | $utc = new \DateTime('utc'); 22 | $header = new HeaderSunset($utc); 23 | 24 | $this->assertSame('Sunset', $header->getFieldName()); 25 | } 26 | 27 | public function testFieldValue() 28 | { 29 | $utc = new \DateTime('utc'); 30 | $header = new HeaderSunset($utc); 31 | 32 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 33 | } 34 | 35 | public function testFieldValueWithMutableDateTime() 36 | { 37 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 38 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 39 | 40 | $header = new HeaderSunset($now); 41 | 42 | $this->assertSame($utc->format(\DateTime::RFC822), $header->getFieldValue()); 43 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 44 | } 45 | 46 | public function testFieldValueWithImmutableDateTime() 47 | { 48 | $now = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow')); 49 | $utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC')); 50 | 51 | $header = new HeaderSunset($now); 52 | 53 | $this->assertSame($utc->format(\DateTimeImmutable::RFC822), $header->getFieldValue()); 54 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 55 | } 56 | 57 | public function testBuild() 58 | { 59 | $now = new \DateTime('now'); 60 | $header = new HeaderSunset($now); 61 | 62 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 63 | 64 | $this->assertSame($expected, $header->__toString()); 65 | } 66 | 67 | public function testIterator() 68 | { 69 | $now = new \DateTime('now'); 70 | $header = new HeaderSunset($now); 71 | $iterator = $header->getIterator(); 72 | 73 | $iterator->rewind(); 74 | $this->assertSame($header->getFieldName(), $iterator->current()); 75 | 76 | $iterator->next(); 77 | $this->assertSame($header->getFieldValue(), $iterator->current()); 78 | 79 | $iterator->next(); 80 | $this->assertFalse($iterator->valid()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/HeaderTrailerTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderTrailer('foo'); 21 | 22 | $this->assertSame('Trailer', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderTrailer('foo'); 28 | 29 | $this->assertSame('foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testEmptyValue() 33 | { 34 | $this->expectException(\InvalidArgumentException::class); 35 | 36 | $this->expectExceptionMessage( 37 | 'The value "" for the header "Trailer" is not valid' 38 | ); 39 | 40 | new HeaderTrailer(''); 41 | } 42 | 43 | public function testInvalidValue() 44 | { 45 | $this->expectException(\InvalidArgumentException::class); 46 | 47 | $this->expectExceptionMessage( 48 | 'The value "@" for the header "Trailer" is not valid' 49 | ); 50 | 51 | // isn't a token... 52 | new HeaderTrailer('@'); 53 | } 54 | 55 | public function testBuild() 56 | { 57 | $header = new HeaderTrailer('foo'); 58 | 59 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 60 | 61 | $this->assertSame($expected, $header->__toString()); 62 | } 63 | 64 | public function testIterator() 65 | { 66 | $header = new HeaderTrailer('foo'); 67 | $iterator = $header->getIterator(); 68 | 69 | $iterator->rewind(); 70 | $this->assertSame($header->getFieldName(), $iterator->current()); 71 | 72 | $iterator->next(); 73 | $this->assertSame($header->getFieldValue(), $iterator->current()); 74 | 75 | $iterator->next(); 76 | $this->assertFalse($iterator->valid()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/HeaderTransferEncodingTest.php: -------------------------------------------------------------------------------- 1 | assertSame('chunked', HeaderTransferEncoding::CHUNKED); 14 | $this->assertSame('compress', HeaderTransferEncoding::COMPRESS); 15 | $this->assertSame('deflate', HeaderTransferEncoding::DEFLATE); 16 | $this->assertSame('gzip', HeaderTransferEncoding::GZIP); 17 | } 18 | 19 | public function testContracts() 20 | { 21 | $header = new HeaderTransferEncoding('foo'); 22 | 23 | $this->assertInstanceOf(HeaderInterface::class, $header); 24 | } 25 | 26 | public function testFieldName() 27 | { 28 | $header = new HeaderTransferEncoding('foo'); 29 | 30 | $this->assertSame('Transfer-Encoding', $header->getFieldName()); 31 | } 32 | 33 | public function testFieldValue() 34 | { 35 | $header = new HeaderTransferEncoding('foo'); 36 | 37 | $this->assertSame('foo', $header->getFieldValue()); 38 | } 39 | 40 | public function testSeveralValues() 41 | { 42 | $header = new HeaderTransferEncoding('foo', 'bar', 'baz'); 43 | 44 | $this->assertSame('foo, bar, baz', $header->getFieldValue()); 45 | } 46 | 47 | public function testEmptyValue() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Transfer-Encoding" is not valid'); 51 | 52 | new HeaderTransferEncoding(''); 53 | } 54 | 55 | public function testEmptyValueAmongOthers() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "" for the header "Transfer-Encoding" is not valid'); 59 | 60 | new HeaderTransferEncoding('foo', '', 'baz'); 61 | } 62 | 63 | public function testInvalidValue() 64 | { 65 | $this->expectException(\InvalidArgumentException::class); 66 | $this->expectExceptionMessage('The value "@" for the header "Transfer-Encoding" is not valid'); 67 | 68 | // isn't a token... 69 | new HeaderTransferEncoding('@'); 70 | } 71 | 72 | public function testInvalidValueAmongOthers() 73 | { 74 | $this->expectException(\InvalidArgumentException::class); 75 | $this->expectExceptionMessage('The value "@" for the header "Transfer-Encoding" is not valid'); 76 | 77 | // isn't a token... 78 | new HeaderTransferEncoding('foo', '@', 'baz'); 79 | } 80 | 81 | public function testBuild() 82 | { 83 | $header = new HeaderTransferEncoding('foo'); 84 | 85 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 86 | 87 | $this->assertSame($expected, $header->__toString()); 88 | } 89 | 90 | public function testIterator() 91 | { 92 | $header = new HeaderTransferEncoding('foo'); 93 | $iterator = $header->getIterator(); 94 | 95 | $iterator->rewind(); 96 | $this->assertSame($header->getFieldName(), $iterator->current()); 97 | 98 | $iterator->next(); 99 | $this->assertSame($header->getFieldValue(), $iterator->current()); 100 | 101 | $iterator->next(); 102 | $this->assertFalse($iterator->valid()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/HeaderVaryTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(HeaderInterface::class, $header); 16 | } 17 | 18 | public function testFieldName() 19 | { 20 | $header = new HeaderVary('foo'); 21 | 22 | $this->assertSame('Vary', $header->getFieldName()); 23 | } 24 | 25 | public function testFieldValue() 26 | { 27 | $header = new HeaderVary('foo'); 28 | 29 | $this->assertSame('foo', $header->getFieldValue()); 30 | } 31 | 32 | public function testSeveralValues() 33 | { 34 | $header = new HeaderVary('foo', 'bar', 'baz'); 35 | 36 | $this->assertSame('foo, bar, baz', $header->getFieldValue()); 37 | } 38 | 39 | public function testEmptyValue() 40 | { 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage('The value "" for the header "Vary" is not valid'); 43 | 44 | new HeaderVary(''); 45 | } 46 | 47 | public function testEmptyValueAmongOthers() 48 | { 49 | $this->expectException(\InvalidArgumentException::class); 50 | $this->expectExceptionMessage('The value "" for the header "Vary" is not valid'); 51 | 52 | new HeaderVary('foo', '', 'baz'); 53 | } 54 | 55 | public function testInvalidValue() 56 | { 57 | $this->expectException(\InvalidArgumentException::class); 58 | $this->expectExceptionMessage('The value "@" for the header "Vary" is not valid'); 59 | 60 | // isn't a token... 61 | new HeaderVary('@'); 62 | } 63 | 64 | public function testInvalidValueAmongOthers() 65 | { 66 | $this->expectException(\InvalidArgumentException::class); 67 | $this->expectExceptionMessage('The value "@" for the header "Vary" is not valid'); 68 | 69 | // isn't a token... 70 | new HeaderVary('foo', '@', 'baz'); 71 | } 72 | 73 | public function testBuild() 74 | { 75 | $header = new HeaderVary('foo'); 76 | 77 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 78 | 79 | $this->assertSame($expected, $header->__toString()); 80 | } 81 | 82 | public function testIterator() 83 | { 84 | $header = new HeaderVary('foo'); 85 | $iterator = $header->getIterator(); 86 | 87 | $iterator->rewind(); 88 | $this->assertSame($header->getFieldName(), $iterator->current()); 89 | 90 | $iterator->next(); 91 | $this->assertSame($header->getFieldValue(), $iterator->current()); 92 | 93 | $iterator->next(); 94 | $this->assertFalse($iterator->valid()); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /tests/HeaderWWWAuthenticateTest.php: -------------------------------------------------------------------------------- 1 | assertSame('Basic', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_BASIC); 14 | $this->assertSame('Bearer', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_BEARER); 15 | $this->assertSame('Digest', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_DIGEST); 16 | $this->assertSame('HOBA', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_HOBA); 17 | $this->assertSame('Mutual', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_MUTUAL); 18 | $this->assertSame('Negotiate', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_NEGOTIATE); 19 | $this->assertSame('OAuth', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_OAUTH); 20 | $this->assertSame('SCRAM-SHA-1', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_1); 21 | $this->assertSame('SCRAM-SHA-256', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_256); 22 | $this->assertSame('vapid', HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_VAPID); 23 | } 24 | 25 | public function testContracts() 26 | { 27 | $header = new HeaderWWWAuthenticate('foo'); 28 | 29 | $this->assertInstanceOf(HeaderInterface::class, $header); 30 | } 31 | 32 | public function testFieldName() 33 | { 34 | $header = new HeaderWWWAuthenticate('foo'); 35 | 36 | $this->assertSame('WWW-Authenticate', $header->getFieldName()); 37 | } 38 | 39 | public function testFieldValue() 40 | { 41 | $header = new HeaderWWWAuthenticate('foo'); 42 | 43 | $this->assertSame('foo', $header->getFieldValue()); 44 | } 45 | 46 | public function testParameterWithEmptyValue() 47 | { 48 | $header = new HeaderWWWAuthenticate('foo', [ 49 | 'bar' => '', 50 | ]); 51 | 52 | $this->assertSame('foo bar=""', $header->getFieldValue()); 53 | } 54 | 55 | public function testParameterWithToken() 56 | { 57 | $header = new HeaderWWWAuthenticate('foo', [ 58 | 'bar' => 'token', 59 | ]); 60 | 61 | $this->assertSame('foo bar="token"', $header->getFieldValue()); 62 | } 63 | 64 | public function testParameterWithQuotedString() 65 | { 66 | $header = new HeaderWWWAuthenticate('foo', [ 67 | 'bar' => 'quoted string', 68 | ]); 69 | 70 | $this->assertSame('foo bar="quoted string"', $header->getFieldValue()); 71 | } 72 | 73 | public function testParameterWithInteger() 74 | { 75 | $header = new HeaderWWWAuthenticate('foo', [ 76 | 'bar' => 1, 77 | ]); 78 | 79 | $this->assertSame('foo bar="1"', $header->getFieldValue()); 80 | } 81 | 82 | public function testSeveralParameters() 83 | { 84 | $header = new HeaderWWWAuthenticate('foo', [ 85 | 'bar' => '', 86 | 'baz' => 'token', 87 | 'bat' => 'quoted string', 88 | 'qux' => 1, 89 | ]); 90 | 91 | $this->assertSame('foo bar="", baz="token", bat="quoted string", qux="1"', $header->getFieldValue()); 92 | } 93 | 94 | public function testEmptyScheme() 95 | { 96 | $this->expectException(\InvalidArgumentException::class); 97 | 98 | new HeaderWWWAuthenticate(''); 99 | } 100 | 101 | public function testInvalidScheme() 102 | { 103 | $this->expectException(\InvalidArgumentException::class); 104 | 105 | // isn't a token... 106 | new HeaderWWWAuthenticate('@'); 107 | } 108 | 109 | public function testInvalidParameterName() 110 | { 111 | $this->expectException(\InvalidArgumentException::class); 112 | 113 | $this->expectExceptionMessage( 114 | 'The parameter-name "invalid name" for the header "WWW-Authenticate" is not valid' 115 | ); 116 | 117 | // cannot contain spaces... 118 | new HeaderWWWAuthenticate('foo', ['invalid name' => 'value']); 119 | } 120 | 121 | public function testInvalidParameterNameType() 122 | { 123 | $this->expectException(\InvalidArgumentException::class); 124 | 125 | $this->expectExceptionMessage( 126 | 'The parameter-name "" for the header "WWW-Authenticate" is not valid' 127 | ); 128 | 129 | // cannot contain spaces... 130 | new HeaderWWWAuthenticate('foo', [0 => 'value']); 131 | } 132 | 133 | public function testInvalidParameterValue() 134 | { 135 | $this->expectException(\InvalidArgumentException::class); 136 | 137 | $this->expectExceptionMessage( 138 | 'The parameter-value ""invalid value"" for the header "WWW-Authenticate" is not valid' 139 | ); 140 | 141 | // cannot contain quotes... 142 | new HeaderWWWAuthenticate('foo', ['name' => '"invalid value"']); 143 | } 144 | 145 | public function testInvalidParameterValueType() 146 | { 147 | $this->expectException(\InvalidArgumentException::class); 148 | 149 | $this->expectExceptionMessage( 150 | 'The parameter-value "" for the header "WWW-Authenticate" is not valid' 151 | ); 152 | 153 | // cannot contain quotes... 154 | new HeaderWWWAuthenticate('foo', ['name' => []]); 155 | } 156 | 157 | public function testBuild() 158 | { 159 | $header = new HeaderWWWAuthenticate('foo', ['bar' => 'baz']); 160 | 161 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 162 | 163 | $this->assertSame($expected, $header->__toString()); 164 | } 165 | 166 | public function testIterator() 167 | { 168 | $header = new HeaderWWWAuthenticate('foo', ['bar' => 'baz']); 169 | $iterator = $header->getIterator(); 170 | 171 | $iterator->rewind(); 172 | $this->assertSame($header->getFieldName(), $iterator->current()); 173 | 174 | $iterator->next(); 175 | $this->assertSame($header->getFieldValue(), $iterator->current()); 176 | 177 | $iterator->next(); 178 | $this->assertFalse($iterator->valid()); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /tests/HeaderWarningTest.php: -------------------------------------------------------------------------------- 1 | assertSame(110, HeaderWarning::HTTP_WARNING_CODE_RESPONSE_IS_STALE); 14 | $this->assertSame(111, HeaderWarning::HTTP_WARNING_CODE_REVALIDATION_FAILED); 15 | $this->assertSame(112, HeaderWarning::HTTP_WARNING_CODE_DISCONNECTED_OPERATION); 16 | $this->assertSame(113, HeaderWarning::HTTP_WARNING_CODE_HEURISTIC_EXPIRATION); 17 | $this->assertSame(199, HeaderWarning::HTTP_WARNING_CODE_MISCELLANEOUS_WARNING); 18 | $this->assertSame(214, HeaderWarning::HTTP_WARNING_CODE_TRANSFORMATION_APPLIED); 19 | $this->assertSame(299, HeaderWarning::HTTP_WARNING_CODE_MISCELLANEOUS_PERSISTENT_WARNING); 20 | } 21 | 22 | public function testContracts() 23 | { 24 | $header = new HeaderWarning(199, 'agent', 'text'); 25 | 26 | $this->assertInstanceOf(HeaderInterface::class, $header); 27 | } 28 | 29 | public function testFieldName() 30 | { 31 | $header = new HeaderWarning(199, 'agent', 'text'); 32 | 33 | $this->assertSame('Warning', $header->getFieldName()); 34 | } 35 | 36 | public function testFieldValue() 37 | { 38 | $header = new HeaderWarning(199, 'agent', 'text'); 39 | 40 | $this->assertSame('199 agent "text"', $header->getFieldValue()); 41 | } 42 | 43 | public function testFieldValueWithDate() 44 | { 45 | $now = new \DateTime('now', new \DateTimeZone('Europe/Moscow')); 46 | $utc = new \DateTime('now', new \DateTimeZone('UTC')); 47 | 48 | $header = new HeaderWarning(199, 'agent', 'text', $now); 49 | 50 | $this->assertSame( 51 | \sprintf( 52 | '199 agent "text" "%s"', 53 | $utc->format(\DateTime::RFC822) 54 | ), 55 | $header->getFieldValue() 56 | ); 57 | 58 | // cannot be modified... 59 | $this->assertSame('Europe/Moscow', $now->getTimezone()->getName()); 60 | } 61 | 62 | public function testCodeLessThat100() 63 | { 64 | $this->expectException(\InvalidArgumentException::class); 65 | $this->expectExceptionMessage('The code "99" for the header "Warning" is not valid'); 66 | 67 | new HeaderWarning(99, 'agent', 'text'); 68 | } 69 | 70 | public function testCodeGreaterThat999() 71 | { 72 | $this->expectException(\InvalidArgumentException::class); 73 | $this->expectExceptionMessage('The code "1000" for the header "Warning" is not valid'); 74 | 75 | new HeaderWarning(1000, 'agent', 'text'); 76 | } 77 | 78 | public function testEmptyAgent() 79 | { 80 | $this->expectException(\InvalidArgumentException::class); 81 | $this->expectExceptionMessage('The value "" for the header "Warning" is not valid'); 82 | 83 | new HeaderWarning(199, '', 'text'); 84 | } 85 | 86 | public function testInvalidAgent() 87 | { 88 | $this->expectException(\InvalidArgumentException::class); 89 | $this->expectExceptionMessage('The value "@" for the header "Warning" is not valid'); 90 | 91 | // isn't a token... 92 | new HeaderWarning(199, '@', 'text'); 93 | } 94 | 95 | public function testEmptyText() 96 | { 97 | $header = new HeaderWarning(199, 'agent', ''); 98 | 99 | $this->assertSame('199 agent ""', $header->getFieldValue()); 100 | } 101 | 102 | public function testInvalidText() 103 | { 104 | $this->expectException(\InvalidArgumentException::class); 105 | $this->expectExceptionMessage('The value ""text"" for the header "Warning" is not valid'); 106 | 107 | // cannot contain quotes... 108 | new HeaderWarning(199, 'agent', '"text"'); 109 | } 110 | 111 | public function testBuild() 112 | { 113 | $header = new HeaderWarning(199, 'agent', 'text'); 114 | 115 | $expected = \sprintf('%s: %s', $header->getFieldName(), $header->getFieldValue()); 116 | 117 | $this->assertSame($expected, $header->__toString()); 118 | } 119 | 120 | public function testIterator() 121 | { 122 | $header = new HeaderWarning(199, 'agent', 'text'); 123 | $iterator = $header->getIterator(); 124 | 125 | $iterator->rewind(); 126 | $this->assertSame($header->getFieldName(), $iterator->current()); 127 | 128 | $iterator->next(); 129 | $this->assertSame($header->getFieldValue(), $iterator->current()); 130 | 131 | $iterator->next(); 132 | $this->assertFalse($iterator->valid()); 133 | } 134 | } 135 | --------------------------------------------------------------------------------