| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Rest\HeaderContainer; |
| 6 |
use MediaWikiUnitTestCase; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class HeaderContainerTest extends MediaWikiUnitTestCase { |
| 12 |
public static function provideSetHeader() { |
| 13 |
return [ |
| 14 |
'simple' => [ |
| 15 |
[ |
| 16 |
[ 'Test', 'foo' ] |
| 17 |
], |
| 18 |
[ 'Test' => [ 'foo' ] ], |
| 19 |
[ 'Test' => 'foo' ] |
| 20 |
], |
| 21 |
'replace' => [ |
| 22 |
[ |
| 23 |
[ 'Test', 'foo' ], |
| 24 |
[ 'Test', 'bar' ], |
| 25 |
], |
| 26 |
[ 'Test' => [ 'bar' ] ], |
| 27 |
[ 'Test' => 'bar' ], |
| 28 |
], |
| 29 |
'array value' => [ |
| 30 |
[ |
| 31 |
[ 'Test', [ '1', '2' ] ], |
| 32 |
[ 'Test', [ '3', '4' ] ], |
| 33 |
], |
| 34 |
[ 'Test' => [ '3', '4' ] ], |
| 35 |
[ 'Test' => '3, 4' ] |
| 36 |
], |
| 37 |
'preserve most recent case' => [ |
| 38 |
[ |
| 39 |
[ 'test', 'foo' ], |
| 40 |
[ 'tesT', 'bar' ], |
| 41 |
], |
| 42 |
[ 'tesT' => [ 'bar' ] ], |
| 43 |
[ 'tesT' => 'bar' ] |
| 44 |
], |
| 45 |
'empty' => [ [], [], [] ], |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public function testSetHeader( $setOps, $headers, $lines ) { |
| 51 |
$hc = new HeaderContainer; |
| 52 |
foreach ( $setOps as [ $name, $value ] ) { |
| 53 |
$hc->setHeader( $name, $value ); |
| 54 |
} |
| 55 |
$this->assertSame( $headers, $hc->getHeaders() ); |
| 56 |
$this->assertSame( $lines, $hc->getHeaderLines() ); |
| 57 |
} |
| 58 |
|
| 59 |
public static function provideAddHeader() { |
| 60 |
return [ |
| 61 |
'simple' => [ |
| 62 |
[ |
| 63 |
[ 'Test', 'foo' ] |
| 64 |
], |
| 65 |
[ 'Test' => [ 'foo' ] ], |
| 66 |
[ 'Test' => 'foo' ] |
| 67 |
], |
| 68 |
'add' => [ |
| 69 |
[ |
| 70 |
[ 'Test', 'foo' ], |
| 71 |
[ 'Test', 'bar' ], |
| 72 |
], |
| 73 |
[ 'Test' => [ 'foo', 'bar' ] ], |
| 74 |
[ 'Test' => 'foo, bar' ], |
| 75 |
], |
| 76 |
'array value' => [ |
| 77 |
[ |
| 78 |
[ 'Test', [ '1', '2' ] ], |
| 79 |
[ 'Test', [ '3', '4' ] ], |
| 80 |
], |
| 81 |
[ 'Test' => [ '1', '2', '3', '4' ] ], |
| 82 |
[ 'Test' => '1, 2, 3, 4' ] |
| 83 |
], |
| 84 |
'preserve original case' => [ |
| 85 |
[ |
| 86 |
[ 'Test', 'foo' ], |
| 87 |
[ 'tesT', 'bar' ], |
| 88 |
], |
| 89 |
[ 'Test' => [ 'foo', 'bar' ] ], |
| 90 |
[ 'Test' => 'foo, bar' ] |
| 91 |
], |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public function testAddHeader( $addOps, $headers, $lines ) { |
| 97 |
$hc = new HeaderContainer; |
| 98 |
foreach ( $addOps as [ $name, $value ] ) { |
| 99 |
$hc->addHeader( $name, $value ); |
| 100 |
} |
| 101 |
$this->assertSame( $headers, $hc->getHeaders() ); |
| 102 |
$this->assertSame( $lines, $hc->getHeaderLines() ); |
| 103 |
} |
| 104 |
|
| 105 |
public static function provideRemoveHeader() { |
| 106 |
return [ |
| 107 |
'simple' => [ |
| 108 |
[ [ 'Test', 'foo' ] ], |
| 109 |
[ 'Test' ], |
| 110 |
[], |
| 111 |
[] |
| 112 |
], |
| 113 |
'case mismatch' => [ |
| 114 |
[ [ 'Test', 'foo' ] ], |
| 115 |
[ 'tesT' ], |
| 116 |
[], |
| 117 |
[] |
| 118 |
], |
| 119 |
'remove nonexistent' => [ |
| 120 |
[ [ 'A', '1' ] ], |
| 121 |
[ 'B' ], |
| 122 |
[ 'A' => [ '1' ] ], |
| 123 |
[ 'A' => '1' ] |
| 124 |
], |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
public function testRemoveHeader( $addOps, $removeOps, $headers, $lines ) { |
| 130 |
$hc = new HeaderContainer; |
| 131 |
foreach ( $addOps as [ $name, $value ] ) { |
| 132 |
$hc->addHeader( $name, $value ); |
| 133 |
} |
| 134 |
foreach ( $removeOps as $name ) { |
| 135 |
$hc->removeHeader( $name ); |
| 136 |
} |
| 137 |
$this->assertSame( $headers, $hc->getHeaders() ); |
| 138 |
$this->assertSame( $lines, $hc->getHeaderLines() ); |
| 139 |
} |
| 140 |
|
| 141 |
public function testHasHeader() { |
| 142 |
$hc = new HeaderContainer; |
| 143 |
$hc->addHeader( 'A', '1' ); |
| 144 |
$hc->addHeader( 'B', '2' ); |
| 145 |
$hc->addHeader( 'C', '3' ); |
| 146 |
$hc->removeHeader( 'B' ); |
| 147 |
$hc->removeHeader( 'c' ); |
| 148 |
$this->assertTrue( $hc->hasHeader( 'A' ) ); |
| 149 |
$this->assertTrue( $hc->hasHeader( 'a' ) ); |
| 150 |
$this->assertFalse( $hc->hasHeader( 'B' ) ); |
| 151 |
$this->assertFalse( $hc->hasHeader( 'c' ) ); |
| 152 |
$this->assertFalse( $hc->hasHeader( 'C' ) ); |
| 153 |
} |
| 154 |
|
| 155 |
public function testGetRawHeaderLines() { |
| 156 |
$hc = new HeaderContainer; |
| 157 |
$hc->addHeader( 'A', '1' ); |
| 158 |
$hc->addHeader( 'a', '2' ); |
| 159 |
$hc->addHeader( 'b', '3' ); |
| 160 |
$hc->addHeader( 'Set-Cookie', 'x' ); |
| 161 |
$hc->addHeader( 'SET-cookie', 'y' ); |
| 162 |
$this->assertSame( |
| 163 |
[ |
| 164 |
'A: 1, 2', |
| 165 |
'b: 3', |
| 166 |
'Set-Cookie: x', |
| 167 |
'Set-Cookie: y', |
| 168 |
], |
| 169 |
$hc->getRawHeaderLines() |
| 170 |
); |
| 171 |
} |
| 172 |
} |