4 * @covers PageDataRequestHandler
10 class PageDataRequestHandlerTest
extends \MediaWikiTestCase
{
15 private $interfaceTitle;
22 protected function setUp() {
25 $this->interfaceTitle
= Title
::newFromText( "Special:PageDataRequestHandlerTest" );
27 $this->obLevel
= ob_get_level();
30 protected function tearDown() {
31 $obLevel = ob_get_level();
33 while ( ob_get_level() > $this->obLevel
) {
37 if ( $obLevel !== $this->obLevel
) {
38 $this->fail( "Test changed output buffer level: was {$this->obLevel}" .
39 "before test, but $obLevel after test."
47 * @return PageDataRequestHandler
49 protected function newHandler() {
50 return new PageDataRequestHandler( 'json' );
54 * @param array $params
55 * @param string[] $headers
59 protected function makeOutputPage( array $params, array $headers ) {
61 $request = new FauxRequest( $params );
62 $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
64 foreach ( $headers as $name => $value ) {
65 $request->setHeader( strtoupper( $name ), $value );
68 // construct Context and OutputPage
69 $context = new DerivativeContext( RequestContext
::getMain() );
70 $context->setRequest( $request );
72 $output = new OutputPage( $context );
73 $output->setTitle( $this->interfaceTitle
);
74 $context->setOutput( $output );
79 public function handleRequestProvider() {
82 $cases[] = [ '', [], [], '!!', 400 ];
84 $cases[] = [ '', [ 'target' => 'Helsinki' ], [], '!!', 303, [ 'Location' => '!.+!' ] ];
87 foreach ( $cases as $c ) {
91 if ( isset( $case[1]['target'] ) ) {
92 $case[0] .= $case[1]['target'];
93 unset( $case[1]['target'] );
96 $subpageCases[] = $case;
99 $cases = array_merge( $cases, $subpageCases );
103 [ 'target' => 'Helsinki' ],
104 [ 'Accept' => 'text/HTML' ],
107 [ 'Location' => '!Helsinki$!' ]
113 'target' => 'Helsinki',
114 'revision' => '4242',
116 [ 'Accept' => 'text/HTML' ],
119 [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
128 [ 'Location' => '!Helsinki&action=raw!' ]
131 // #31: /Q5 with "Accept: text/foobar" triggers a 406
135 [ 'Accept' => 'text/foobar' ],
144 [ 'Accept' => 'text/HTML' ],
147 [ 'Location' => '!Helsinki$!' ]
153 [ 'Accept' => 'text/HTML' ],
156 [ 'Location' => '!Helsinki$!' ]
162 [ 'Accept' => 'text/HTML' ],
165 [ 'Location' => '!AC/DC$!' ]
172 * @dataProvider handleRequestProvider
174 * @param string $subpage The subpage to request (or '')
175 * @param array $params Request parameters
176 * @param array $headers Request headers
177 * @param string $expectedOutput Regex to match the output against.
178 * @param int $expectedStatusCode Expected HTTP status code.
179 * @param string[] $expectedHeaders Expected HTTP response headers.
181 public function testHandleRequest(
186 $expectedStatusCode = 200,
187 array $expectedHeaders = []
189 $output = $this->makeOutputPage( $params, $headers );
190 $request = $output->getRequest();
192 /* @var FauxResponse $response */
193 $response = $request->response();
196 $handler = $this->newHandler();
200 $handler->handleRequest( $subpage, $request, $output );
202 if ( $output->getRedirect() !== '' ) {
203 // hack to apply redirect to web response
207 $text = ob_get_contents();
210 $this->assertEquals( $expectedStatusCode, $response->getStatusCode(), 'status code' );
211 $this->assertRegExp( $expectedOutput, $text, 'output' );
213 foreach ( $expectedHeaders as $name => $exp ) {
214 $value = $response->getHeader( $name );
215 $this->assertNotNull( $value, "header: $name" );
216 $this->assertInternalType( 'string', $value, "header: $name" );
217 $this->assertRegExp( $exp, $value, "header: $name" );
219 } catch ( HttpError
$e ) {
221 $this->assertEquals( $expectedStatusCode, $e->getStatusCode(), 'status code' );
222 $this->assertRegExp( $expectedOutput, $e->getHTML(), 'error output' );
225 // We always set "Access-Control-Allow-Origin: *"
226 $this->assertSame( '*', $response->getHeader( 'Access-Control-Allow-Origin' ) );
229 public function provideHttpContentNegotiation() {
230 $helsinki = Title
::newFromText( 'Helsinki' );
232 'Accept Header of HTML' => [
234 [ 'ACCEPT' => 'text/html' ], // headers
237 'Accept Header without weights' => [
239 [ 'ACCEPT' => '*/*, text/html, text/x-wiki' ],
240 'Helsinki&action=raw'
242 'Accept Header with weights' => [
244 [ 'ACCEPT' => 'text/*; q=0.5, text/json; q=0.7, application/rdf+xml; q=0.8' ],
245 'Helsinki&action=raw'
247 'Accept Header accepting evertyhing and HTML' => [
249 [ 'ACCEPT' => 'text/html, */*' ],
250 'Helsinki&action=raw'
252 'No Accept Header' => [
255 'Helsinki&action=raw'
261 * @dataProvider provideHttpContentNegotiation
263 * @param Title $title
264 * @param array $headers Request headers
265 * @param string $expectedRedirectSuffix Expected suffix of the HTTP Location header.
269 public function testHttpContentNegotiation(
272 $expectedRedirectSuffix
274 /* @var FauxResponse $response */
275 $output = $this->makeOutputPage( [], $headers );
276 $request = $output->getRequest();
278 $handler = $this->newHandler();
279 $handler->httpContentNegotiation( $request, $output, $title );
281 $this->assertStringEndsWith(
282 $expectedRedirectSuffix,
283 $output->getRedirect(),