* - "list-contains=$XXX" varies on whether the header value as a
* comma-separated list contains $XXX as one of the list items.
*/
- public function addVaryHeader( $header, $option = null ) {
+ public function addVaryHeader( $header, array $option = null ) {
if ( !array_key_exists( $header, $this->mVaryHeader ) ) {
- $this->mVaryHeader[$header] = (array)$option;
- } elseif ( is_array( $option ) ) {
- if ( is_array( $this->mVaryHeader[$header] ) ) {
- $this->mVaryHeader[$header] = array_merge( $this->mVaryHeader[$header], $option );
- } else {
- $this->mVaryHeader[$header] = $option;
- }
+ $this->mVaryHeader[$header] = array();
+ }
+ if ( !is_array( $option ) ) {
+ $option = array();
}
- $this->mVaryHeader[$header] = array_unique( (array)$this->mVaryHeader[$header] );
+ $this->mVaryHeader[$header] = array_unique( array_merge( $this->mVaryHeader[$header], $option ) );
}
/**
$actualHtml = implode( "\n", $links['html'] );
$this->assertEquals( $expectedHtml, $actualHtml );
}
+
+ /**
+ * @dataProvider provideVaryHeaders
+ * @covers OutputPage::addVaryHeader
+ * @covers OutputPage::getVaryHeader
+ * @covers OutputPage::getXVO
+ */
+ public function testVaryHeaders( $calls, $vary, $xvo ) {
+ // get rid of default Vary fields
+ $outputPage = $this->getMockBuilder( 'OutputPage' )
+ ->setConstructorArgs( array( new RequestContext() ) )
+ ->setMethods( array( 'getCacheVaryCookies' ) )
+ ->getMock();
+ $outputPage->expects( $this->any() )
+ ->method( 'getCacheVaryCookies' )
+ ->will( $this->returnValue( array() ) );
+ TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = array();
+
+ foreach ( $calls as $call ) {
+ call_user_func_array( array( $outputPage, 'addVaryHeader' ), $call );
+ }
+ $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
+ $this->assertEquals( $xvo, $outputPage->getXVO(), 'X-Vary-Options:' );
+ }
+
+ public function provideVaryHeaders() {
+ // note: getXVO() automatically adds Vary: Cookie
+ return array(
+ array( // single header
+ array(
+ array( 'Cookie' ),
+ ),
+ 'Vary: Cookie',
+ 'X-Vary-Options: Cookie',
+ ),
+ array( // non-unique headers
+ array(
+ array( 'Cookie' ),
+ array( 'Accept-Language' ),
+ array( 'Cookie' ),
+ ),
+ 'Vary: Cookie, Accept-Language',
+ 'X-Vary-Options: Cookie,Accept-Language',
+ ),
+ array( // two headers with single options
+ array(
+ array( 'Cookie', array( 'string-contains=phpsessid' ) ),
+ array( 'Accept-Language', array( 'string-contains=en' ) ),
+ ),
+ 'Vary: Cookie, Accept-Language',
+ 'X-Vary-Options: Cookie;string-contains=phpsessid,Accept-Language;string-contains=en',
+ ),
+ array( // one header with multiple options
+ array(
+ array( 'Cookie', array( 'string-contains=phpsessid', 'string-contains=userId' ) ),
+ ),
+ 'Vary: Cookie',
+ 'X-Vary-Options: Cookie;string-contains=phpsessid;string-contains=userId',
+ ),
+ array( // Duplicate option
+ array(
+ array( 'Cookie', array( 'string-contains=phpsessid' ) ),
+ array( 'Cookie', array( 'string-contains=phpsessid' ) ),
+ array( 'Accept-Language', array( 'string-contains=en', 'string-contains=en' ) ),
+
+
+ ),
+ 'Vary: Cookie, Accept-Language',
+ 'X-Vary-Options: Cookie;string-contains=phpsessid,Accept-Language;string-contains=en',
+ ),
+ array( // Same header, different options
+ array(
+ array( 'Cookie', array( 'string-contains=phpsessid' ) ),
+ array( 'Cookie', array( 'string-contains=userId' ) ),
+ ),
+ 'Vary: Cookie',
+ 'X-Vary-Options: Cookie;string-contains=phpsessid;string-contains=userId',
+ ),
+ );
+ }
}
/**