3 namespace MediaWiki\Tests\Rest
;
6 use MediaWiki\Rest\HttpException
;
7 use MediaWiki\Rest\ResponseFactory
;
10 /** @covers \MediaWiki\Rest\ResponseFactory */
11 class ResponseFactoryTest
extends MediaWikiTestCase
{
12 public static function provideEncodeJson() {
21 /** @dataProvider provideEncodeJson */
22 public function testEncodeJson( $input, $expected ) {
23 $rf = new ResponseFactory
;
24 $this->assertSame( $expected, $rf->encodeJson( $input ) );
27 public function testCreateJson() {
28 $rf = new ResponseFactory
;
29 $response = $rf->createJson( [] );
30 $response->getBody()->rewind();
31 $this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
32 $this->assertSame( '[]', $response->getBody()->getContents() );
33 // Make sure getSize() is functional, since testCreateNoContent() depends on it
34 $this->assertSame( 2, $response->getBody()->getSize() );
37 public function testCreateNoContent() {
38 $rf = new ResponseFactory
;
39 $response = $rf->createNoContent();
40 $this->assertSame( [], $response->getHeader( 'Content-Type' ) );
41 $this->assertSame( 0, $response->getBody()->getSize() );
42 $this->assertSame( 204, $response->getStatusCode() );
45 public function testCreatePermanentRedirect() {
46 $rf = new ResponseFactory
;
47 $response = $rf->createPermanentRedirect( 'http://www.example.com/' );
48 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
49 $this->assertSame( 301, $response->getStatusCode() );
52 public function testCreateLegacyTemporaryRedirect() {
53 $rf = new ResponseFactory
;
54 $response = $rf->createLegacyTemporaryRedirect( 'http://www.example.com/' );
55 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
56 $this->assertSame( 302, $response->getStatusCode() );
59 public function testCreateTemporaryRedirect() {
60 $rf = new ResponseFactory
;
61 $response = $rf->createTemporaryRedirect( 'http://www.example.com/' );
62 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
63 $this->assertSame( 307, $response->getStatusCode() );
66 public function testCreateSeeOther() {
67 $rf = new ResponseFactory
;
68 $response = $rf->createSeeOther( 'http://www.example.com/' );
69 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
70 $this->assertSame( 303, $response->getStatusCode() );
73 public function testCreateNotModified() {
74 $rf = new ResponseFactory
;
75 $response = $rf->createNotModified();
76 $this->assertSame( 0, $response->getBody()->getSize() );
77 $this->assertSame( 304, $response->getStatusCode() );
80 /** @expectedException \InvalidArgumentException */
81 public function testCreateHttpErrorInvalid() {
82 $rf = new ResponseFactory
;
83 $rf->createHttpError( 200 );
86 public function testCreateHttpError() {
87 $rf = new ResponseFactory
;
88 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
89 $this->assertSame( 415, $response->getStatusCode() );
90 $body = $response->getBody();
92 $data = json_decode( $body->getContents(), true );
93 $this->assertSame( 415, $data['httpCode'] );
94 $this->assertSame( '...', $data['message'] );
97 public function testCreateFromExceptionUnlogged() {
98 $rf = new ResponseFactory
;
99 $response = $rf->createFromException( new HttpException( 'hello', 415 ) );
100 $this->assertSame( 415, $response->getStatusCode() );
101 $body = $response->getBody();
103 $data = json_decode( $body->getContents(), true );
104 $this->assertSame( 415, $data['httpCode'] );
105 $this->assertSame( 'hello', $data['message'] );
108 public function testCreateFromExceptionLogged() {
109 $rf = new ResponseFactory
;
110 $response = $rf->createFromException( new \
Exception( "hello", 415 ) );
111 $this->assertSame( 500, $response->getStatusCode() );
112 $body = $response->getBody();
114 $data = json_decode( $body->getContents(), true );
115 $this->assertSame( 500, $data['httpCode'] );
116 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
119 public static function provideCreateFromReturnValue() {
121 [ 'hello', '{"value":"hello"}' ],
122 [ true, '{"value":true}' ],
123 [ [ 'x' => 'y' ], '{"x":"y"}' ],
124 [ [ 'x', 'y' ], '["x","y"]' ],
125 [ [ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
126 [ (object)[ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
128 [ (object)[], '{}' ],
132 /** @dataProvider provideCreateFromReturnValue */
133 public function testCreateFromReturnValue( $input, $expected ) {
134 $rf = new ResponseFactory
;
135 $response = $rf->createFromReturnValue( $input );
136 $body = $response->getBody();
138 $this->assertSame( $expected, $body->getContents() );
141 /** @expectedException \InvalidArgumentException */
142 public function testCreateFromReturnValueInvalid() {
143 $rf = new ResponseFactory
;
144 $rf->createFromReturnValue( new ArrayIterator
);