From 768db22fba1dbaefb557ac51d1b506f4ebd55dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Wed, 4 Dec 2019 20:26:45 +0000 Subject: [PATCH 1/3] Hotfix: default value for headers is string not null As per specification default value for headers should be string, not null. In many cases it was invalid as we return null instead. Also, when creating new instance with empty string, again - null was returned instead. Fixes #202 --- src/Header/AuthenticationInfo.php | 8 +- src/Header/Authorization.php | 8 +- src/Header/ContentDisposition.php | 8 +- src/Header/ContentEncoding.php | 8 +- src/Header/ContentLanguage.php | 8 +- src/Header/ContentLength.php | 6 +- src/Header/ContentMD5.php | 8 +- src/Header/ContentRange.php | 8 +- src/Header/ContentTransferEncoding.php | 8 +- src/Header/ContentType.php | 6 +- src/Header/Etag.php | 8 +- src/Header/Expect.php | 8 +- src/Header/From.php | 8 +- src/Header/Host.php | 8 +- src/Header/IfMatch.php | 8 +- src/Header/IfNoneMatch.php | 8 +- src/Header/IfRange.php | 8 +- src/Header/KeepAlive.php | 8 +- src/Header/MaxForwards.php | 8 +- src/Header/Origin.php | 4 +- src/Header/Pragma.php | 8 +- src/Header/ProxyAuthenticate.php | 8 +- src/Header/ProxyAuthorization.php | 8 +- src/Header/Range.php | 8 +- src/Header/Refresh.php | 8 +- src/Header/Server.php | 10 +- src/Header/TE.php | 8 +- src/Header/Trailer.php | 8 +- src/Header/TransferEncoding.php | 8 +- src/Header/Upgrade.php | 8 +- src/Header/UserAgent.php | 8 +- src/Header/Vary.php | 8 +- src/Header/Via.php | 8 +- src/Header/WWWAuthenticate.php | 8 +- src/Header/Warning.php | 8 +- test/HeaderTest.php | 144 +++++++++++++++++++++++++ 36 files changed, 248 insertions(+), 170 deletions(-) create mode 100644 test/HeaderTest.php diff --git a/src/Header/AuthenticationInfo.php b/src/Header/AuthenticationInfo.php index cf75caed7c..b9f24aa8e1 100644 --- a/src/Header/AuthenticationInfo.php +++ b/src/Header/AuthenticationInfo.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Authorization.php b/src/Header/Authorization.php index ccdfb3ed9a..caf3f22483 100644 --- a/src/Header/Authorization.php +++ b/src/Header/Authorization.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentDisposition.php b/src/Header/ContentDisposition.php index 747505af36..19d7132102 100644 --- a/src/Header/ContentDisposition.php +++ b/src/Header/ContentDisposition.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentEncoding.php b/src/Header/ContentEncoding.php index daeb260e0e..73d9b91865 100644 --- a/src/Header/ContentEncoding.php +++ b/src/Header/ContentEncoding.php @@ -30,14 +30,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -50,7 +48,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentLanguage.php b/src/Header/ContentLanguage.php index 320b727321..9d0016d314 100644 --- a/src/Header/ContentLanguage.php +++ b/src/Header/ContentLanguage.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentLength.php b/src/Header/ContentLength.php index 6eee98693a..bea93e322f 100644 --- a/src/Header/ContentLength.php +++ b/src/Header/ContentLength.php @@ -31,9 +31,7 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentMD5.php b/src/Header/ContentMD5.php index c551e39dfb..db56ffd0a5 100644 --- a/src/Header/ContentMD5.php +++ b/src/Header/ContentMD5.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentRange.php b/src/Header/ContentRange.php index 077f2be835..536fd9e4d6 100644 --- a/src/Header/ContentRange.php +++ b/src/Header/ContentRange.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentTransferEncoding.php b/src/Header/ContentTransferEncoding.php index 949fc7fe9d..2134297667 100644 --- a/src/Header/ContentTransferEncoding.php +++ b/src/Header/ContentTransferEncoding.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static(strtolower($value)); - - return $header; + return new static(strtolower($value)); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ContentType.php b/src/Header/ContentType.php index b5b2522a64..86a7666144 100644 --- a/src/Header/ContentType.php +++ b/src/Header/ContentType.php @@ -69,7 +69,7 @@ public static function fromString($headerLine) public function __construct($value = null, $mediaType = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -146,7 +146,7 @@ public function getFieldName() public function getFieldValue() { if (null !== $this->value) { - return $this->value; + return (string) $this->value; } return $this->assembleValue(); } @@ -172,7 +172,7 @@ public function setMediaType($mediaType) */ public function getMediaType() { - return $this->mediaType; + return (string) $this->mediaType; } /** diff --git a/src/Header/Etag.php b/src/Header/Etag.php index 395fbfc97e..8d553b5b9b 100644 --- a/src/Header/Etag.php +++ b/src/Header/Etag.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Expect.php b/src/Header/Expect.php index 1dca3f3063..37f934a7ad 100644 --- a/src/Header/Expect.php +++ b/src/Header/Expect.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/From.php b/src/Header/From.php index 5da35fa3ae..277001945f 100644 --- a/src/Header/From.php +++ b/src/Header/From.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Host.php b/src/Header/Host.php index f796f01181..dbf5c27dc2 100644 --- a/src/Header/Host.php +++ b/src/Header/Host.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/IfMatch.php b/src/Header/IfMatch.php index 6724093074..d2d6e6ccb3 100644 --- a/src/Header/IfMatch.php +++ b/src/Header/IfMatch.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/IfNoneMatch.php b/src/Header/IfNoneMatch.php index 3514e99b29..9e00f9d8aa 100644 --- a/src/Header/IfNoneMatch.php +++ b/src/Header/IfNoneMatch.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/IfRange.php b/src/Header/IfRange.php index 3ed5a3823a..172e71d62f 100644 --- a/src/Header/IfRange.php +++ b/src/Header/IfRange.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/KeepAlive.php b/src/Header/KeepAlive.php index 26f6337443..3d72e4a7bf 100644 --- a/src/Header/KeepAlive.php +++ b/src/Header/KeepAlive.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/MaxForwards.php b/src/Header/MaxForwards.php index 7ec7d780fa..622b659cb7 100644 --- a/src/Header/MaxForwards.php +++ b/src/Header/MaxForwards.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Origin.php b/src/Header/Origin.php index 98c8ba9662..c8143d52a6 100644 --- a/src/Header/Origin.php +++ b/src/Header/Origin.php @@ -42,7 +42,7 @@ public static function fromString($headerLine) */ public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -55,7 +55,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Pragma.php b/src/Header/Pragma.php index 8017c1b7cb..a23bf15227 100644 --- a/src/Header/Pragma.php +++ b/src/Header/Pragma.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ProxyAuthenticate.php b/src/Header/ProxyAuthenticate.php index d39740b8dc..9e0026b5e3 100644 --- a/src/Header/ProxyAuthenticate.php +++ b/src/Header/ProxyAuthenticate.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/ProxyAuthorization.php b/src/Header/ProxyAuthorization.php index e7add19ffb..f3f01e468a 100644 --- a/src/Header/ProxyAuthorization.php +++ b/src/Header/ProxyAuthorization.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Range.php b/src/Header/Range.php index b0112b2c57..289da79444 100644 --- a/src/Header/Range.php +++ b/src/Header/Range.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Refresh.php b/src/Header/Refresh.php index 3d3a250b4f..0f47b901be 100644 --- a/src/Header/Refresh.php +++ b/src/Header/Refresh.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Server.php b/src/Header/Server.php index 645c1150ab..b2cc69b752 100644 --- a/src/Header/Server.php +++ b/src/Header/Server.php @@ -1,7 +1,7 @@ value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/TE.php b/src/Header/TE.php index 0151f741f6..4e05625547 100644 --- a/src/Header/TE.php +++ b/src/Header/TE.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Trailer.php b/src/Header/Trailer.php index 5e1147bfab..fed56485c8 100644 --- a/src/Header/Trailer.php +++ b/src/Header/Trailer.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/TransferEncoding.php b/src/Header/TransferEncoding.php index b6b830f18b..28f0f0ed89 100644 --- a/src/Header/TransferEncoding.php +++ b/src/Header/TransferEncoding.php @@ -30,14 +30,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -50,7 +48,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Upgrade.php b/src/Header/Upgrade.php index 1f853430ab..a9e03cf93e 100644 --- a/src/Header/Upgrade.php +++ b/src/Header/Upgrade.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/UserAgent.php b/src/Header/UserAgent.php index dc2e3efb1d..33f9216d31 100644 --- a/src/Header/UserAgent.php +++ b/src/Header/UserAgent.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Vary.php b/src/Header/Vary.php index 7342e22683..a77ffb2796 100644 --- a/src/Header/Vary.php +++ b/src/Header/Vary.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Via.php b/src/Header/Via.php index 05cd752c07..f38f7df537 100644 --- a/src/Header/Via.php +++ b/src/Header/Via.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/WWWAuthenticate.php b/src/Header/WWWAuthenticate.php index ddc5bd656d..a6c016d8d1 100644 --- a/src/Header/WWWAuthenticate.php +++ b/src/Header/WWWAuthenticate.php @@ -31,14 +31,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -51,7 +49,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/src/Header/Warning.php b/src/Header/Warning.php index 65b6cfa0c7..6ad277f19f 100644 --- a/src/Header/Warning.php +++ b/src/Header/Warning.php @@ -28,14 +28,12 @@ public static function fromString($headerLine) } // @todo implementation details - $header = new static($value); - - return $header; + return new static($value); } public function __construct($value = null) { - if ($value) { + if ($value !== null) { HeaderValue::assertValid($value); $this->value = $value; } @@ -48,7 +46,7 @@ public function getFieldName() public function getFieldValue() { - return $this->value; + return (string) $this->value; } public function toString() diff --git a/test/HeaderTest.php b/test/HeaderTest.php new file mode 100644 index 0000000000..23a1231db5 --- /dev/null +++ b/test/HeaderTest.php @@ -0,0 +1,144 @@ + [Header\AuthenticationInfo::class, 'Authentication-Info']; + yield Header\Authorization::class => [Header\Authorization::class, 'Authorization']; + yield Header\ContentDisposition::class => [Header\ContentDisposition::class, 'Content-Disposition']; + yield Header\ContentEncoding::class => [Header\ContentEncoding::class, 'Content-Encoding']; + yield Header\ContentLanguage::class => [Header\ContentLanguage::class, 'Content-Language']; + yield Header\ContentLength::class => [Header\ContentLength::class, 'Content-Length']; + yield Header\ContentMD5::class => [Header\ContentMD5::class, 'Content-MD5']; + yield Header\ContentRange::class => [Header\ContentRange::class, 'Content-Range']; + yield Header\ContentTransferEncoding::class => [Header\ContentTransferEncoding::class, 'Content-Transfer-Encoding']; + yield Header\ContentType::class => [Header\ContentType::class, 'Content-Type']; + yield Header\Etag::class => [Header\Etag::class, 'Etag']; + yield Header\Expect::class => [Header\Expect::class, 'Expect']; + yield Header\From::class => [Header\From::class, 'From']; + yield Header\Host::class => [Header\Host::class, 'Host']; + yield Header\IfMatch::class => [Header\IfMatch::class, 'If-Match']; + yield Header\IfNoneMatch::class => [Header\IfNoneMatch::class, 'If-None-Match']; + yield Header\IfRange::class => [Header\IfRange::class, 'If-Range']; + yield Header\KeepAlive::class => [Header\KeepAlive::class, 'Keep-Alive']; + yield Header\MaxForwards::class => [Header\MaxForwards::class, 'Max-Forwards']; + yield Header\Origin::class => [Header\Origin::class, 'Origin']; + yield Header\Pragma::class => [Header\Pragma::class, 'Pragma']; + yield Header\ProxyAuthenticate::class => [Header\ProxyAuthenticate::class, 'Proxy-Authenticate']; + yield Header\ProxyAuthorization::class => [Header\ProxyAuthorization::class, 'Proxy-Authorization']; + yield Header\Range::class => [Header\Range::class, 'Range']; + yield Header\Refresh::class => [Header\Refresh::class, 'Refresh']; + yield Header\Server::class => [Header\Server::class, 'Server']; + yield Header\TE::class => [Header\TE::class, 'TE']; + yield Header\Trailer::class => [Header\Trailer::class, 'Trailer']; + yield Header\TransferEncoding::class => [Header\TransferEncoding::class, 'Transfer-Encoding']; + yield Header\Upgrade::class => [Header\Upgrade::class, 'Upgrade']; + yield Header\UserAgent::class => [Header\UserAgent::class, 'User-Agent']; + yield Header\Vary::class => [Header\Vary::class, 'Vary']; + yield Header\Via::class => [Header\Via::class, 'Via']; + yield Header\Warning::class => [Header\Warning::class, 'Warning']; + yield Header\WWWAuthenticate::class => [Header\WWWAuthenticate::class, 'WWW-Authenticate']; + // @codingStandardsIgnoreEnd + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testThrowsExceptionIfInvalidHeaderLine($class, $name) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid header line for ' . $name . ' string'); + $class::fromString($name . '-Foo: bar'); + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testCaseInsensitiveHeaderName($class, $name) + { + $header1 = $class::fromString(strtoupper($name) . ': foo'); + self::assertSame('foo', $header1->getFieldValue()); + + $header2 = $class::fromString(strtolower($name) . ': bar'); + self::assertSame('bar', $header2->getFieldValue()); + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testDefaultValues($class, $name) + { + $header = new $class(); + + self::assertSame('', $header->getFieldValue()); + self::assertSame($name, $header->getFieldName()); + self::assertSame($name . ': ', $header->toString()); + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testSetValueViaConstructor($class, $name) + { + $header = new $class('foo-bar'); + + self::assertSame('foo-bar', $header->getFieldValue()); + self::assertSame($name . ': foo-bar', $header->toString()); + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + * + * Note: in theory this is invalid, as we would expect value to be string|null. + * Null is default value but it is converted to string. + */ + public function testSetIntValueViaConstructor($class, $name) + { + $header = new $class(100); + + self::assertSame('100', $header->getFieldValue()); + self::assertSame($name . ': 100', $header->toString()); + } + + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testFromStringWithNumber($class, $name) + { + $header = $class::fromString($name . ': 100'); + + self::assertSame('100', $header->getFieldValue()); + self::assertSame($name . ': 100', $header->toString()); + } +} From a72b62ad1c0f1368b46007c31165f2ee910fb58f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Wed, 4 Dec 2019 20:52:43 +0000 Subject: [PATCH 2/3] Add test with zero string value --- test/HeaderTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/HeaderTest.php b/test/HeaderTest.php index 23a1231db5..047f3e0a7a 100644 --- a/test/HeaderTest.php +++ b/test/HeaderTest.php @@ -128,6 +128,20 @@ public function testSetIntValueViaConstructor($class, $name) self::assertSame($name . ': 100', $header->toString()); } + /** + * @dataProvider header + * + * @param string $class + * @param string $name + */ + public function testSetZeroStringValueViaConstructor($class, $name) + { + $header = new $class('0'); + + self::assertSame('0', $header->getFieldValue()); + self::assertSame($name . ': 0', $header->toString()); + } + /** * @dataProvider header * From c18aca6c5faa6c0d634cb605a7aed2616dcc49e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Wed, 4 Dec 2019 20:53:07 +0000 Subject: [PATCH 3/3] Update two more headers to return string value: Accept-Range and Age --- src/Header/AcceptRanges.php | 8 +++----- src/Header/Age.php | 10 ++++------ test/HeaderTest.php | 1 + 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Header/AcceptRanges.php b/src/Header/AcceptRanges.php index 5b094a5033..4ae61a6761 100644 --- a/src/Header/AcceptRanges.php +++ b/src/Header/AcceptRanges.php @@ -27,14 +27,12 @@ public static function fromString($headerLine) ); } - $header = new static($value); - - return $header; + return new static($value); } public function __construct($rangeUnit = null) { - if ($rangeUnit) { + if ($rangeUnit !== null) { $this->setRangeUnit($rangeUnit); } } @@ -58,7 +56,7 @@ public function setRangeUnit($rangeUnit) public function getRangeUnit() { - return $this->rangeUnit; + return (string) $this->rangeUnit; } public function toString() diff --git a/src/Header/Age.php b/src/Header/Age.php index 7d0bab82e1..2419a99289 100644 --- a/src/Header/Age.php +++ b/src/Header/Age.php @@ -37,14 +37,12 @@ public static function fromString($headerLine) throw new Exception\InvalidArgumentException('Invalid header line for Age string: "' . $name . '"'); } - $header = new static($value); - - return $header; + return new static($value); } public function __construct($deltaSeconds = null) { - if ($deltaSeconds) { + if ($deltaSeconds !== null) { $this->setDeltaSeconds($deltaSeconds); } } @@ -62,11 +60,11 @@ public function getFieldName() /** * Get header value (number of seconds) * - * @return int + * @return string */ public function getFieldValue() { - return $this->getDeltaSeconds(); + return (string) $this->getDeltaSeconds(); } /** diff --git a/test/HeaderTest.php b/test/HeaderTest.php index 047f3e0a7a..f80a3f8d07 100644 --- a/test/HeaderTest.php +++ b/test/HeaderTest.php @@ -16,6 +16,7 @@ class HeaderTest extends TestCase public function header() { // @codingStandardsIgnoreStart + yield Header\AcceptRanges::class => [Header\AcceptRanges::class, 'Accept-Ranges']; yield Header\AuthenticationInfo::class => [Header\AuthenticationInfo::class, 'Authentication-Info']; yield Header\Authorization::class => [Header\Authorization::class, 'Authorization']; yield Header\ContentDisposition::class => [Header\ContentDisposition::class, 'Content-Disposition'];