-
Notifications
You must be signed in to change notification settings - Fork 150
Adds response reason phrases test against IANA. #234
Adds response reason phrases test against IANA. #234
Conversation
test/TestAsset/http-status-codes.xml
Outdated
@@ -0,0 +1,368 @@ | |||
<?xml version='1.0' encoding='UTF-8'?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this file come from? And can we instead request it via HTTPS? That's what I was asking for :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, the test should just download them by pinging https://www.iana.org/assignments/http-status-codes/http-status-codes.xml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the file name by URL in ResponseTest.php:77, works just fine.
However, this can be a problem.
The test will depend on an external online resource.
And besides this, imagine if all implementations decides to do the same thing, requesting the file when the test runs.
I believe this may be end up leading to a case of service misuse.
One solution would be to add a pre-release script, something like this in composer.json ("scripts" block).
"pre-release-tasks": [
"@update-test-asset"
],
"update-test-asset": "curl -o test/TestAsset/http-status-codes.xml https://www.iana.org/assignments/http-status-codes/http-status-codes.xml"
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this may be end up leading to a case of service misuse.
One http request a day will not trigger misuse :-)
One solution would be to add a pre-release script, something like this in composer.json ("scripts" block).
"pre-release-tasks": [
"@update-test-asset"
],
"update-test-asset": "curl -o test/TestAsset/http-status-codes.xml https://www.iana.org/assignments/http-status-codes/http-status-codes.xml"What do you think?
If it's not automated, it doesn't exist. Let's please push it to the test suite, and run it every time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I understand.
Check new version, I may remove "last modified" stuff to make it more simpler.
Apparently IANA server don't support "If-Modified-Since" (or I made a mistake).
However I think what timeout and fallback to file is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed unused code. And I tried to make it simpler and clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is to be removed from the test suite
@fcabralpacheco could you create alternate PR against zend-http also in favor of zendframework/zend-http#110 ? |
@samsonasik Yes. I will create an alternate PR. |
test/ResponseTest.php
Outdated
{ | ||
set_error_handler(function ($errno, $errstr) { | ||
throw new \ErrorException($errstr, 0, $errno); | ||
}); | ||
|
||
$updateError = null; | ||
$ianaHttpStatusCodes = new \DOMDocument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requires ext-xml
in the dev dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires ext-dom.
test/ResponseTest.php
Outdated
} | ||
} | ||
} catch (\Exception $e) { | ||
$updateError = $e->getMessage(); | ||
$errorMessage = $e->getMessage(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this logic (the entire try/catch)
test/TestAsset/http-status-codes.xml
Outdated
@@ -0,0 +1,368 @@ | |||
<?xml version='1.0' encoding='UTF-8'?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is to be removed from the test suite
@@ -0,0 +1,198 @@ | |||
<?xml version='1.0'?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file to be removed from the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, never mind, this seems to be required for validity checks
test/ResponseTest.php
Outdated
$errorMessage = null; | ||
$httpStatus = 0; | ||
|
||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be removed
test/ResponseTest.php
Outdated
|
||
if (! $validXml) { | ||
if ($errorMessage) { | ||
print 'Error on IANA "http-status-codes.xml" update. Error: ' . $errorMessage . "\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self::fail()
is sufficient :-\
test/ResponseTest.php
Outdated
restore_error_handler(); | ||
|
||
if (! $validXml) { | ||
$this->markTestIncomplete( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self::fail()
is better here.
test/ResponseTest.php
Outdated
{ | ||
$ianaHttpStatusCodes = $this->updateAndLoadIanaHttpStatusCodes(); | ||
|
||
if (! $ianaHttpStatusCodes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method should never return null
test/ResponseTest.php
Outdated
$value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue; | ||
$description = $xpath->query('.//ns:description', $record)->item(0)->nodeValue; | ||
|
||
if ($description === 'Unassigned' || $description === '(Unused)') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in_array()
test/ResponseTest.php
Outdated
|
||
$range = preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches); | ||
|
||
if (! $range) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swap the conditional to avoid the negation. Also, $range
variable is redundant
Removed file fallback and error handling logic. PHPUnit handles all errors/warnings/notices nicely, just self:fail() worked very well. |
I have not updated the composer.lock file. Do I need to do this? |
$ianaHttpStatusCodes->load('https://www.iana.org/assignments/http-status-codes/http-status-codes.xml'); | ||
|
||
if (! $ianaHttpStatusCodes->relaxNGValidate(__DIR__ . '/TestAsset/http-status-codes.rng')) { | ||
self::fail(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have a useful message, to make it clear why the failure occurred (timeout reaching the validation file and/or invalid content in the file fetched). I can add that at merge, however.
@@ -66,6 +66,63 @@ public function testReasonPhraseDefaultsToStandards() | |||
$this->assertEquals('Unprocessable Entity', $response->getReasonPhrase()); | |||
} | |||
|
|||
public function ianaCodesReasonPhrasesProvider() | |||
{ | |||
$ianaHttpStatusCodes = new \DOMDocument(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import DOMDocument
. (I can do that at merge)
|
||
$ianaCodesReasonPhrases = []; | ||
|
||
$xpath = new \DomXPath($ianaHttpStatusCodes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import DOMXPath
. (I can do that at merge)
Adds response reason phrases test against IANA.
Thanks, @fcabralpacheco |
…IANA's data (dunglas) This PR was merged into the 2.7 branch. Discussion ---------- [HttpFoundation] Fix and test status codes according to IANA's data | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This PR updates HTTP status code according to values [standardized by the IANA](https://www.iana.org/assignments/http-status-codes/http-status-codes.xml). It also add a test to check the conformance (we'll also be notified when a code is added, ~~removed~~ or modified). All credits go to @fcabralpacheco for is work in Zend Diactoros: zendframework/zend-diactoros#234 Commits ------- 72d25cc [HttpFoundation] Fix and test status codes according to IANA's data
@Ocramius
This is a possible implementation of response reason phrases test against the registry maintained by IANA.
I chose XML because of the possibility of validating before starting the test.
The test is marked as incomplete in case of invalid XML.
I'm not sure how is the best way to update it, but I believe a pre-release script would be the best alternative.
Something like this:
curl -o test/TestAsset/http-status-codes.xml http://www.iana.org/assignments/http-status-codes/http-status-codes.xml
About getting the xml when the test runs, I believe is overkill (and adds dependency of external resource), and reminds me of cases of service misuse.
One last comment, the IANA registry is the authoritative list for HTTP status codes (RFC7231, Section 8.2.)
Ref: #230, #208