Adds response reason phrases test against IANA. #234
Changes from 6 commits
f65a0ea
be87948
107fb39
cf80bc9
1290307
3272bf7
a2fe140
131631c
d83e713
15c2d96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,122 @@ public function testReasonPhraseDefaultsToStandards() | |
$this->assertEquals('Unprocessable Entity', $response->getReasonPhrase()); | ||
} | ||
|
||
protected function updateAndLoadIanaHttpStatusCodes() | ||
{ | ||
set_error_handler(function ($errno, $errstr) { | ||
throw new \ErrorException($errstr, 0, $errno); | ||
}); | ||
|
||
$ianaHttpStatusCodes = new \DOMDocument(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. requires There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requires ext-dom. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import |
||
$validXml = false; | ||
$errorMessage = null; | ||
$httpStatus = 0; | ||
|
||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be removed |
||
$options = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need to be in a variable |
||
'http' => [ | ||
'method' => 'GET', | ||
'timeout' => 30, | ||
], | ||
]; | ||
|
||
$contents = file_get_contents( | ||
'https://www.iana.org/assignments/http-status-codes/http-status-codes.xml', | ||
false, | ||
stream_context_create($options) | ||
); | ||
|
||
if ($http_response_header) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable wasn't defined before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (preg_match('/^HTTP\/[0-9\.]+\s*([0-9]+)\s*.+$/i', $http_response_header[0], $matches) > 0) { | ||
$httpStatus = $matches[1]; | ||
} | ||
} | ||
|
||
if ($httpStatus == 200) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$ianaHttpStatusCodes->loadXml($contents); | ||
$validXml = $ianaHttpStatusCodes->relaxNGValidate(__DIR__ . '/TestAsset/http-status-codes.rng'); | ||
|
||
if ($validXml) { | ||
file_put_contents(__DIR__ . '/TestAsset/http-status-codes.xml', $contents); | ||
print 'IANA "http-status-codes.xml" updated successful' . "\n"; | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
$errorMessage = $e->getMessage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this logic (the entire try/catch) |
||
} | ||
|
||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
try { | ||
$ianaHttpStatusCodes->load(__DIR__ . '/TestAsset/http-status-codes.xml'); | ||
$validXml = $ianaHttpStatusCodes->relaxNGValidate(__DIR__ . '/TestAsset/http-status-codes.rng'); | ||
} catch (\Exception $e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove - let it bubble up |
||
$errorMessage = $e->getMessage(); | ||
} | ||
} | ||
|
||
restore_error_handler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
|
||
if (! $validXml) { | ||
$this->markTestIncomplete( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
'Invalid IANA "http-status-codes.xml". Error: ' . $errorMessage | ||
); | ||
$ianaHttpStatusCodes = null; | ||
} | ||
|
||
return $ianaHttpStatusCodes; | ||
} | ||
|
||
public function ianaCodesReasonPhrasesProvider() | ||
{ | ||
$ianaHttpStatusCodes = $this->updateAndLoadIanaHttpStatusCodes(); | ||
|
||
if (! $ianaHttpStatusCodes) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method should never return |
||
return null; | ||
} | ||
|
||
$ianaCodesReasonPhrases = []; | ||
|
||
$xpath = new \DomXPath($ianaHttpStatusCodes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import |
||
$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 ($description === 'Unassigned' || $description === '(Unused)') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
continue; | ||
} | ||
|
||
$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 commentThe reason will be displayed to describe this comment to others. Learn more. Swap the conditional to avoid the negation. Also, |
||
$ianaCodesReasonPhrases[] = [$value, $description]; | ||
} else { | ||
for ($value = $matches[1]; $value <= $matches[2]; $value++) { | ||
$ianaCodesReasonPhrases[] = [$value, $description]; | ||
} | ||
} | ||
} | ||
|
||
return $ianaCodesReasonPhrases; | ||
} | ||
|
||
/** | ||
* @dataProvider ianaCodesReasonPhrasesProvider | ||
*/ | ||
public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase) | ||
{ | ||
$response = $this->response->withStatus($code); | ||
$this->assertEquals($reasonPhrase, $response->getReasonPhrase()); | ||
} | ||
|
||
public function testCanSetCustomReasonPhrase() | ||
{ | ||
$response = $this->response->withStatus(422, 'Foo Bar!'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version='1.0'?> | ||
<grammar xmlns="http://relaxng.org/ns/structure/1.0" | ||
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" | ||
ns="http://www.iana.org/assignments"> | ||
|
||
<include href="iana-registry.rng"/> | ||
|
||
<start> | ||
<element name="registry"> | ||
<ref name="registryMeta"/> | ||
<element name="registry"> | ||
<ref name="registryMeta"/> | ||
<zeroOrMore> | ||
<element name="record"> | ||
<optional> | ||
<attribute name="date"><ref name="genericDate"/></attribute> | ||
</optional> | ||
<optional> | ||
<attribute name="updated"><ref name="genericDate"/></attribute> | ||
</optional> | ||
<element name="value"><ref name="genericRange"/></element> | ||
<element name="description"><text/></element> | ||
<ref name="references"/> | ||
</element> | ||
</zeroOrMore> | ||
</element> | ||
<ref name="people"/> | ||
</element> | ||
</start> | ||
|
||
</grammar> |
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 block - phpunit has a flag for raising exceptions on notices