Skip to content

Commit

Permalink
minor #22357 [HttpFoundation] Fix and test status codes according to …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Apr 10, 2017
2 parents 34655e6 + 72d25cc commit 6928f12
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -158,10 +158,10 @@ class Response
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot', // RFC2324
421 => 'Misdirected Request', // RFC7540
Expand All @@ -180,7 +180,7 @@ class Response
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates (Experimental)', // RFC2295
506 => 'Variant Also Negotiates', // RFC2295
507 => 'Insufficient Storage', // RFC4918
508 => 'Loop Detected', // RFC5842
510 => 'Not Extended', // RFC2774
Expand Down
61 changes: 61 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -882,6 +882,67 @@ protected function provideResponse()
{
return new Response();
}

/**
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
*
* @author Fábio Pacheco
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
public function ianaCodesReasonPhrasesProvider()
{
if (!in_array('https', stream_get_wrappers(), true)) {
$this->markTestSkipped('The "https" wrapper is not available');
}

$ianaHttpStatusCodes = new \DOMDocument();

libxml_set_streams_context(stream_context_create(array(
'http' => array(
'method' => 'GET',
'timeout' => 30,
),
)));

$ianaHttpStatusCodes->load('https://www.iana.org/assignments/http-status-codes/http-status-codes.xml');
if (!$ianaHttpStatusCodes->relaxNGValidate('https://www.iana.org/assignments/http-status-codes/http-status-codes.rng')) {
self::fail('Invalid IANA\'s HTTP status code list.');
}

$ianaCodesReasonPhrases = array();

$xpath = new \DomXPath($ianaHttpStatusCodes);
$xpath->registerNamespace('ns', 'http://www.iana.org/assignments');

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

if (in_array($description, array('Unassigned', '(Unused)'), true)) {
continue;
}

if (preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches)) {
for ($value = $matches[1]; $value <= $matches[2]; ++$value) {
$ianaCodesReasonPhrases[] = array($value, $description);
}
} else {
$ianaCodesReasonPhrases[] = array($value, $description);
}
}

return $ianaCodesReasonPhrases;
}

/**
* @dataProvider ianaCodesReasonPhrasesProvider
*/
public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase)
{
$this->assertEquals($reasonPhrase, Response::$statusTexts[$code]);
}
}

class StringableObject
Expand Down

0 comments on commit 6928f12

Please sign in to comment.