* Added ConditionalHeaderUtil, a conditional request helper class meant for composition into Handler. I evaluated the composer package micheh/psr7-cache for this role but I decided that I prefer DIY code rather than some rather ugly glue. * Check conditional request headers prior to entry into Handler::execute(). Contrary to what was previously documented, use the results of getLastModified() and getETag() to set headers in the response. This is convenient and can be overridden in the Handler if desired by overriding a one-line function. * Instead of locking up header parsing inside ConditionalHeaderUtil as was done in micheh/psr7-cache, make a start on a new reusable header parsing framework, with recursive descent parsers for HTTP-date and IfNoneMatch. Change-Id: I260809081cad7701df8620ab03834158670d4230
70 lines
1.5 KiB
PHP
70 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Rest\HeaderParser;
|
|
|
|
use MediaWiki\Rest\HeaderParser\HttpDate;
|
|
|
|
/**
|
|
* @covers \MediaWiki\Rest\HeaderParser\HttpDate
|
|
*/
|
|
class HttpDateTest extends \MediaWikiUnitTestCase {
|
|
public static function provideParse() {
|
|
return [
|
|
'RFC 7231 example 1' => [
|
|
'Sun, 06 Nov 1994 08:49:37 GMT',
|
|
784111777
|
|
],
|
|
'RFC 7231 example 2' => [
|
|
'Sunday, 06-Nov-94 08:49:37 GMT',
|
|
784111777
|
|
],
|
|
'RFC 7231 example 3' => [
|
|
'Sun Nov 6 08:49:37 1994',
|
|
784111777
|
|
],
|
|
'asctime whitespace nitpick' => [
|
|
'Sun Nov 6 08:49:37 1994',
|
|
null
|
|
],
|
|
'PHP "r" format' => [
|
|
'Sun, 06 Nov 1994 08:49:37 +0000',
|
|
null
|
|
],
|
|
'RFC 7231 example 1 with trail' => [
|
|
'Sun, 06 Nov 1994 08:49:37 GMT.',
|
|
null
|
|
],
|
|
'RFC 7231 example 2 with trail' => [
|
|
'Sunday, 06-Nov-94 08:49:37 GMT.',
|
|
null
|
|
],
|
|
'RFC 7231 example 3 with trail' => [
|
|
'Sun Nov 6 08:49:37 1994.',
|
|
null
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideParse
|
|
* @param string $input
|
|
* @param string|null $expected
|
|
*/
|
|
public function testParse( $input, $expected ) {
|
|
$result = HttpDate::parse( $input );
|
|
$this->assertSame( $expected, $result );
|
|
}
|
|
|
|
public static function provideFormatRoundtrip() {
|
|
for ( $ts = 1000000000; $ts < 2000000000; $ts += 100000000 ) {
|
|
yield [ $ts ];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideFormatRoundtrip
|
|
*/
|
|
public function testFormatRoundtrip( $ts ) {
|
|
$this->assertSame( $ts, HttpDate::parse( HttpDate::format( $ts ) ) );
|
|
}
|
|
}
|