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 testCreateTemporaryRedirect() {
53 $rf = new ResponseFactory
;
54 $response = $rf->createTemporaryRedirect( 'http://www.example.com/' );
55 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
56 $this->assertSame( 307, $response->getStatusCode() );
59 public function testCreateSeeOther() {
60 $rf = new ResponseFactory
;
61 $response = $rf->createSeeOther( 'http://www.example.com/' );
62 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
63 $this->assertSame( 303, $response->getStatusCode() );
66 public function testCreateNotModified() {
67 $rf = new ResponseFactory
;
68 $response = $rf->createNotModified();
69 $this->assertSame( 0, $response->getBody()->getSize() );
70 $this->assertSame( 304, $response->getStatusCode() );
73 /** @expectedException \InvalidArgumentException */
74 public function testCreateHttpErrorInvalid() {
75 $rf = new ResponseFactory
;
76 $rf->createHttpError( 200 );
79 public function testCreateHttpError() {
80 $rf = new ResponseFactory
;
81 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
82 $this->assertSame( 415, $response->getStatusCode() );
83 $body = $response->getBody();
85 $data = json_decode( $body->getContents(), true );
86 $this->assertSame( 415, $data['httpCode'] );
87 $this->assertSame( '...', $data['message'] );
90 public function testCreateFromExceptionUnlogged() {
91 $rf = new ResponseFactory
;
92 $response = $rf->createFromException( new HttpException( 'hello', 415 ) );
93 $this->assertSame( 415, $response->getStatusCode() );
94 $body = $response->getBody();
96 $data = json_decode( $body->getContents(), true );
97 $this->assertSame( 415, $data['httpCode'] );
98 $this->assertSame( 'hello', $data['message'] );
101 public function testCreateFromExceptionLogged() {
102 $rf = new ResponseFactory
;
103 $response = $rf->createFromException( new \
Exception( "hello", 415 ) );
104 $this->assertSame( 500, $response->getStatusCode() );
105 $body = $response->getBody();
107 $data = json_decode( $body->getContents(), true );
108 $this->assertSame( 500, $data['httpCode'] );
109 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
112 public static function provideCreateFromReturnValue() {
114 [ 'hello', '{"value":"hello"}' ],
115 [ true, '{"value":true}' ],
116 [ [ 'x' => 'y' ], '{"x":"y"}' ],
117 [ [ 'x', 'y' ], '["x","y"]' ],
118 [ [ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
119 [ (object)[ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
121 [ (object)[], '{}' ],
125 /** @dataProvider provideCreateFromReturnValue */
126 public function testCreateFromReturnValue( $input, $expected ) {
127 $rf = new ResponseFactory
;
128 $response = $rf->createFromReturnValue( $input );
129 $body = $response->getBody();
131 $this->assertSame( $expected, $body->getContents() );
134 /** @expectedException \InvalidArgumentException */
135 public function testCreateFromReturnValueInvalid() {
136 $rf = new ResponseFactory
;
137 $rf->createFromReturnValue( new ArrayIterator
);