Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Adds response reason phrases test against IANA. #234

Merged
merged 10 commits into from Apr 6, 2017

Conversation

fcabralpacheco
Copy link
Contributor

@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

@@ -0,0 +1,368 @@
<?xml version='1.0' encoding='UTF-8'?>
Copy link
Member

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 :-)

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius

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?

Copy link
Member

@Ocramius Ocramius Feb 20, 2017

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius

I removed unused code. And I tried to make it simpler and clear.

Copy link
Member

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

@samsonasik
Copy link
Contributor

@fcabralpacheco could you create alternate PR against zend-http also in favor of zendframework/zend-http#110 ?

@fcabralpacheco
Copy link
Contributor Author

@samsonasik Yes. I will create an alternate PR.

{
set_error_handler(function ($errno, $errstr) {
throw new \ErrorException($errstr, 0, $errno);
});

$updateError = null;
$ianaHttpStatusCodes = new \DOMDocument();
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires ext-dom.

}
}
} catch (\Exception $e) {
$updateError = $e->getMessage();
$errorMessage = $e->getMessage();
Copy link
Member

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)

@@ -0,0 +1,368 @@
<?xml version='1.0' encoding='UTF-8'?>
Copy link
Member

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'?>
Copy link
Member

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

Copy link
Member

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

$errorMessage = null;
$httpStatus = 0;

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be removed


if (! $validXml) {
if ($errorMessage) {
print 'Error on IANA "http-status-codes.xml" update. Error: ' . $errorMessage . "\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::fail() is sufficient :-\

restore_error_handler();

if (! $validXml) {
$this->markTestIncomplete(
Copy link
Member

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.

{
$ianaHttpStatusCodes = $this->updateAndLoadIanaHttpStatusCodes();

if (! $ianaHttpStatusCodes) {
Copy link
Member

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

$value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
$description = $xpath->query('.//ns:description', $record)->item(0)->nodeValue;

if ($description === 'Unassigned' || $description === '(Unused)') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in_array()


$range = preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches);

if (! $range) {
Copy link
Member

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

@fcabralpacheco
Copy link
Contributor Author

@Ocramius

Removed file fallback and error handling logic.

PHPUnit handles all errors/warnings/notices nicely, just self:fail() worked very well.

@fcabralpacheco
Copy link
Contributor Author

@Ocramius

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();
Copy link
Member

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();
Copy link
Member

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);
Copy link
Member

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)

@weierophinney weierophinney added this to the 1.3.11 milestone Apr 6, 2017
@weierophinney weierophinney merged commit 15c2d96 into zendframework:master Apr 6, 2017
weierophinney added a commit that referenced this pull request Apr 6, 2017
Adds response reason phrases test against IANA.
weierophinney added a commit that referenced this pull request Apr 6, 2017
weierophinney added a commit that referenced this pull request Apr 6, 2017
weierophinney added a commit that referenced this pull request Apr 6, 2017
@weierophinney
Copy link
Member

Thanks, @fcabralpacheco

fabpot added a commit to symfony/symfony that referenced this pull request Apr 10, 2017
…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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants