From 41053e89a72a7b0d9cf15110bfe62fa794e5daff Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 20 Jun 2020 15:07:45 -0400 Subject: [PATCH] Tests for HTTP-level client exceptions --- lib/HttpClient/Exception.php | 4 ++- tests/cases/HttpClientTest.php | 61 ++++++++++++++++++++++++++++++++++ tests/phpunit.dist.xml | 3 ++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/HttpClient/Exception.php b/lib/HttpClient/Exception.php index f8ff24a..385d438 100644 --- a/lib/HttpClient/Exception.php +++ b/lib/HttpClient/Exception.php @@ -13,12 +13,14 @@ class Exception extends BaseException { if (preg_match("/^httpStatus(\d+)$/", $symbol, $match)) { $code = (int) $match[1]; assert($code >= 400, "HTTP status codes under 400 should not produce exceptions"); - if (($code < 500 && $code > 431 && $code !== 451) || in_array($code, [418, 419, 420, 430])) { + if (($code < 500 && $code > 431 && $code !== 451) || in_array($code, [418, 419, 420, 427, 430])) { // unassigned 4xx code are changed to 400 $symbol = "httpStatus400"; } elseif ($code > 511 || $code === 509) { // unassigned 5xx codes and anything 600 or above is changed to 500 $symbol = "httpStatus500"; + } else { + $symbol = "httpStatus".$code; } } parent::__construct($symbol, $e); diff --git a/tests/cases/HttpClientTest.php b/tests/cases/HttpClientTest.php index e69de29..75a1b35 100644 --- a/tests/cases/HttpClientTest.php +++ b/tests/cases/HttpClientTest.php @@ -0,0 +1,61 @@ +expectExceptionObject($exp); + throw $act; + } + + public function provideCodes(): iterable { + // see https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml + $exceptions = [418, 419, 420, 427, 430, 451, 509]; + $cutoff = [400 => 432, 500 => 512]; + for ($a = 400; $a < $cutoff[400]; $a++) { + $out = $a; + if (in_array($a, $exceptions)) { + $out = 400; + } + yield "httpStatus".$a => ["httpStatus".$a, $out]; + } + for ($a = $cutoff[400]; $a < 500; $a++) { + $out = 400; + if (in_array($a, $exceptions)) { + $out = $a; + } + yield "httpStatus".$a => ["httpStatus".$a, $out]; + } + for ($a = 500; $a < $cutoff[500]; $a++) { + $out = $a; + if (in_array($a, $exceptions)) { + $out = 500; + } + yield "httpStatus".$a => ["httpStatus".$a, $out]; + } + for ($a = $cutoff[500]; $a < 600; $a++) { + $out = 500; + if (in_array($a, $exceptions)) { + $out = $a; + } + yield "httpStatus".$a => ["httpStatus".$a, $out]; + } + foreach ([600, 999, 1000, 1401] as $a) { + yield "httpStatus".$a => ["httpStatus".$a, 500]; + } + yield "httpStatus000401" => ["httpStatus000401", 401]; + } +} \ No newline at end of file diff --git a/tests/phpunit.dist.xml b/tests/phpunit.dist.xml index 62adf57..a62d513 100644 --- a/tests/phpunit.dist.xml +++ b/tests/phpunit.dist.xml @@ -25,5 +25,8 @@ cases/Parser + + cases/HttpClientTest.php +