Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / Rest / ResponseFactoryTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use ArrayIterator;
6 use MediaWiki\Rest\HttpException;
7 use MediaWiki\Rest\ResponseFactory;
8 use MediaWikiUnitTestCase;
9 use Wikimedia\Message\ITextFormatter;
10 use Wikimedia\Message\MessageValue;
11
12 /** @covers \MediaWiki\Rest\ResponseFactory */
13 class ResponseFactoryTest extends MediaWikiUnitTestCase {
14 public static function provideEncodeJson() {
15 return [
16 [ (object)[], '{}' ],
17 [ '/', '"/"' ],
18 [ '£', '"£"' ],
19 [ [], '[]' ],
20 ];
21 }
22
23 private function createResponseFactory() {
24 $fakeTextFormatter = new class implements ITextFormatter {
25 function getLangCode() {
26 return 'qqx';
27 }
28
29 function format( MessageValue $message ) {
30 return $message->getKey();
31 }
32 };
33 return new ResponseFactory( [ $fakeTextFormatter ] );
34 }
35
36 /** @dataProvider provideEncodeJson */
37 public function testEncodeJson( $input, $expected ) {
38 $rf = $this->createResponseFactory();
39 $this->assertSame( $expected, $rf->encodeJson( $input ) );
40 }
41
42 public function testCreateJson() {
43 $rf = $this->createResponseFactory();
44 $response = $rf->createJson( [] );
45 $response->getBody()->rewind();
46 $this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
47 $this->assertSame( '[]', $response->getBody()->getContents() );
48 // Make sure getSize() is functional, since testCreateNoContent() depends on it
49 $this->assertSame( 2, $response->getBody()->getSize() );
50 }
51
52 public function testCreateNoContent() {
53 $rf = $this->createResponseFactory();
54 $response = $rf->createNoContent();
55 $this->assertSame( [], $response->getHeader( 'Content-Type' ) );
56 $this->assertSame( 0, $response->getBody()->getSize() );
57 $this->assertSame( 204, $response->getStatusCode() );
58 }
59
60 public function testCreatePermanentRedirect() {
61 $rf = $this->createResponseFactory();
62 $response = $rf->createPermanentRedirect( 'http://www.example.com/' );
63 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
64 $this->assertSame( 301, $response->getStatusCode() );
65 }
66
67 public function testCreateLegacyTemporaryRedirect() {
68 $rf = $this->createResponseFactory();
69 $response = $rf->createLegacyTemporaryRedirect( 'http://www.example.com/' );
70 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
71 $this->assertSame( 302, $response->getStatusCode() );
72 }
73
74 public function testCreateTemporaryRedirect() {
75 $rf = $this->createResponseFactory();
76 $response = $rf->createTemporaryRedirect( 'http://www.example.com/' );
77 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
78 $this->assertSame( 307, $response->getStatusCode() );
79 }
80
81 public function testCreateSeeOther() {
82 $rf = $this->createResponseFactory();
83 $response = $rf->createSeeOther( 'http://www.example.com/' );
84 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
85 $this->assertSame( 303, $response->getStatusCode() );
86 }
87
88 public function testCreateNotModified() {
89 $rf = $this->createResponseFactory();
90 $response = $rf->createNotModified();
91 $this->assertSame( 0, $response->getBody()->getSize() );
92 $this->assertSame( 304, $response->getStatusCode() );
93 }
94
95 /** @expectedException \InvalidArgumentException */
96 public function testCreateHttpErrorInvalid() {
97 $rf = $this->createResponseFactory();
98 $rf->createHttpError( 200 );
99 }
100
101 public function testCreateHttpError() {
102 $rf = $this->createResponseFactory();
103 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
104 $this->assertSame( 415, $response->getStatusCode() );
105 $body = $response->getBody();
106 $body->rewind();
107 $data = json_decode( $body->getContents(), true );
108 $this->assertSame( 415, $data['httpCode'] );
109 $this->assertSame( '...', $data['message'] );
110 }
111
112 public function testCreateFromExceptionUnlogged() {
113 $rf = $this->createResponseFactory();
114 $response = $rf->createFromException( new HttpException( 'hello', 415 ) );
115 $this->assertSame( 415, $response->getStatusCode() );
116 $body = $response->getBody();
117 $body->rewind();
118 $data = json_decode( $body->getContents(), true );
119 $this->assertSame( 415, $data['httpCode'] );
120 $this->assertSame( 'hello', $data['message'] );
121 }
122
123 public function testCreateFromExceptionLogged() {
124 $rf = $this->createResponseFactory();
125 $response = $rf->createFromException( new \Exception( "hello", 415 ) );
126 $this->assertSame( 500, $response->getStatusCode() );
127 $body = $response->getBody();
128 $body->rewind();
129 $data = json_decode( $body->getContents(), true );
130 $this->assertSame( 500, $data['httpCode'] );
131 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
132 }
133
134 public static function provideCreateFromReturnValue() {
135 return [
136 [ 'hello', '{"value":"hello"}' ],
137 [ true, '{"value":true}' ],
138 [ [ 'x' => 'y' ], '{"x":"y"}' ],
139 [ [ 'x', 'y' ], '["x","y"]' ],
140 [ [ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
141 [ (object)[ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
142 [ [], '[]' ],
143 [ (object)[], '{}' ],
144 ];
145 }
146
147 /** @dataProvider provideCreateFromReturnValue */
148 public function testCreateFromReturnValue( $input, $expected ) {
149 $rf = $this->createResponseFactory();
150 $response = $rf->createFromReturnValue( $input );
151 $body = $response->getBody();
152 $body->rewind();
153 $this->assertSame( $expected, $body->getContents() );
154 }
155
156 /** @expectedException \InvalidArgumentException */
157 public function testCreateFromReturnValueInvalid() {
158 $rf = $this->createResponseFactory();
159 $rf->createFromReturnValue( new ArrayIterator );
160 }
161
162 public function testCreateLocalizedHttpError() {
163 $rf = $this->createResponseFactory();
164 $response = $rf->createLocalizedHttpError( 404, new MessageValue( 'rftest' ) );
165 $body = $response->getBody();
166 $body->rewind();
167 $this->assertSame(
168 '{"messageTranslations":{"qqx":"rftest"},"httpCode":404,"httpReason":"Not Found"}',
169 $body->getContents() );
170 }
171 }