Debian Codegraph @

tests/phpunit/unit/includes/Rest/ResponseFactoryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use ArrayIterator;
6 use Exception;
7 use InvalidArgumentException;
8 use MediaWiki\Rest\HttpException;
9 use MediaWiki\Rest\LocalizedHttpException;
10 use MediaWiki\Rest\RedirectException;
11 use MediaWiki\Rest\ResponseException;
12 use MediaWiki\Rest\ResponseFactory;
13 use MediaWiki\Tests\Unit\DummyServicesTrait;
14 use MediaWikiUnitTestCase;
15 use Wikimedia\Message\MessageValue;
16
17 /** @covers \MediaWiki\Rest\ResponseFactory */
18 class ResponseFactoryTest extends MediaWikiUnitTestCase {
19 use DummyServicesTrait;
20
21 public static function provideEncodeJson() {
22 return [
23 [ (object)[], '{}' ],
24 [ '/', '"/"' ],
25 [ '£', '"£"' ],
26 [ [], '[]' ],
27 [ "\xc0", "\"\u{FFFD}\"" ] // T289597
28 ];
29 }
30
31 private function createResponseFactory() {
32 return new ResponseFactory( [ $this->getDummyTextFormatter() ] );
33 }
34
35 /** @dataProvider provideEncodeJson */
36 public function testEncodeJson( $input, $expected ) {
37 $rf = $this->createResponseFactory();
38 $this->assertSame( $expected, $rf->encodeJson( $input ) );
39 }
40
41 public function testCreateJson() {
42 $rf = $this->createResponseFactory();
43 $response = $rf->createJson( [] );
44 $response->getBody()->rewind();
45 $this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
46 $this->assertSame( '[]', $response->getBody()->getContents() );
47 // Make sure getSize() is functional, since testCreateNoContent() depends on it
48 $this->assertSame( 2, $response->getBody()->getSize() );
49 }
50
51 public static function provideUseException() {
52 return [ [ false ], [ true ] ];
53 }
54
55 /** @dataProvider provideUseException */
56 public function testCreateNoContent( $useException ) {
57 $rf = $this->createResponseFactory();
58 $response = $useException ?
59 $rf->createFromException( new HttpException( '', 204 ) ) :
60 $rf->createNoContent();
61 $this->assertSame( [], $response->getHeader( 'Content-Type' ) );
62 $this->assertSame( 0, $response->getBody()->getSize() );
63 $this->assertSame( 204, $response->getStatusCode() );
64 }
65
66 /** @dataProvider provideUseException */
67 public function testCreatePermanentRedirect( $useException ) {
68 $rf = $this->createResponseFactory();
69 $response = $useException ?
70 $rf->createFromException( new RedirectException(
71 301, 'http://www.example.com/'
72 ) ) :
73 $rf->createPermanentRedirect( 'http://www.example.com/' );
74 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
75 $this->assertSame( 301, $response->getStatusCode() );
76 }
77
78 /** @dataProvider provideUseException */
79 public function testCreateLegacyTemporaryRedirect( $useException ) {
80 $rf = $this->createResponseFactory();
81 $response = $useException ?
82 $rf->createFromException( new RedirectException(
83 302, 'http://www.example.com/'
84 ) ) :
85 $rf->createLegacyTemporaryRedirect( 'http://www.example.com/' );
86 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
87 $this->assertSame( 302, $response->getStatusCode() );
88 }
89
90 /** @dataProvider provideUseException */
91 public function testCreateRedirect( $useException ) {
92 $rf = $this->createResponseFactory();
93 $response = $useException ?
94 $rf->createFromException( new RedirectException(
95 333, 'http://www.example.com/'
96 ) ) :
97 $rf->createRedirect( 'http://www.example.com/', 333 );
98 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
99 $this->assertSame( 333, $response->getStatusCode() );
100 }
101
102 /** @dataProvider provideUseException */
103 public function testCreateTemporaryRedirect( $useException ) {
104 $rf = $this->createResponseFactory();
105 $response = $useException ?
106 $rf->createFromException( new RedirectException(
107 307, 'http://www.example.com/'
108 ) ) :
109 $rf->createTemporaryRedirect( 'http://www.example.com/' );
110 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
111 $this->assertSame( 307, $response->getStatusCode() );
112 }
113
114 /** @dataProvider provideUseException */
115 public function testCreateSeeOther( $useException ) {
116 $rf = $this->createResponseFactory();
117 $response = $useException ?
118 $rf->createFromException( new RedirectException(
119 303, 'http://www.example.com/'
120 ) ) :
121 $rf->createSeeOther( 'http://www.example.com/' );
122 $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
123 $this->assertSame( 303, $response->getStatusCode() );
124 }
125
126 /** @dataProvider provideUseException */
127 public function testCreateNotModified( $useException ) {
128 $rf = $this->createResponseFactory();
129 $response = $useException ?
130 $rf->createFromException( new HttpException( '', 304 ) ) :
131 $rf->createNotModified();
132 $this->assertSame( 0, $response->getBody()->getSize() );
133 $this->assertSame( 304, $response->getStatusCode() );
134 }
135
136 public function testCreateHttpErrorInvalid() {
137 $rf = $this->createResponseFactory();
138 $this->expectException( InvalidArgumentException::class );
139 $rf->createHttpError( 200 );
140 }
141
142 public function testCreateHttpError() {
143 $rf = $this->createResponseFactory();
144 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
145 $this->assertSame( 415, $response->getStatusCode() );
146 $body = $response->getBody();
147 $body->rewind();
148 $data = json_decode( $body->getContents(), true );
149 $this->assertSame( 415, $data['httpCode'] );
150 $this->assertSame( '...', $data['message'] );
151 }
152
153 public function testCreateFromExceptionUnlogged() {
154 $rf = $this->createResponseFactory();
155 $response = $rf->createFromException( new HttpException( 'hello', 415 ) );
156 $this->assertSame( 415, $response->getStatusCode() );
157 $body = $response->getBody();
158 $body->rewind();
159 $data = json_decode( $body->getContents(), true );
160 $this->assertSame( 415, $data['httpCode'] );
161 $this->assertSame( 'hello', $data['message'] );
162 }
163
164 public function testCreateFromExceptionLogged() {
165 $rf = $this->createResponseFactory();
166 $response = $rf->createFromException( new Exception( "hello", 415 ) );
167 $this->assertSame( 500, $response->getStatusCode() );
168 $body = $response->getBody();
169 $body->rewind();
170 $data = json_decode( $body->getContents(), true );
171 $this->assertSame( 500, $data['httpCode'] );
172 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
173 }
174
175 public function testCreateFromExceptionWrapped() {
176 $rf = $this->createResponseFactory();
177 $wrapped = $rf->create();
178 $response = $rf->createFromException( new ResponseException( $wrapped ) );
179 $this->assertSame( $wrapped, $response );
180 }
181
182 public function testCreateFromExceptionWithExtraData() {
183 $rf = $this->createResponseFactory();
184 $e = new LocalizedHttpException( new MessageValue( 'rftest' ), 404 );
185 $response = $rf->createFromException( $e, [ 'foo' => 'bar' ] );
186 $body = $response->getBody();
187 $body->rewind();
188 $data = json_decode( $body->getContents(), true );
189 $this->assertArrayHasKey( 'foo', $data );
190 $this->assertSame( 'bar', $data['foo'] );
191 }
192
193 public static function provideCreateFromReturnValue() {
194 return [
195 [ 'hello', '{"value":"hello"}' ],
196 [ true, '{"value":true}' ],
197 [ [ 'x' => 'y' ], '{"x":"y"}' ],
198 [ [ 'x', 'y' ], '["x","y"]' ],
199 [ [ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
200 [ (object)[ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
201 [ [], '[]' ],
202 [ (object)[], '{}' ],
203 ];
204 }
205
206 /** @dataProvider provideCreateFromReturnValue */
207 public function testCreateFromReturnValue( $input, $expected ) {
208 $rf = $this->createResponseFactory();
209 $response = $rf->createFromReturnValue( $input );
210 $body = $response->getBody();
211 $body->rewind();
212 $this->assertSame( $expected, $body->getContents() );
213 }
214
215 public function testCreateFromReturnValueInvalid() {
216 $rf = $this->createResponseFactory();
217 $this->expectException( InvalidArgumentException::class );
218 $rf->createFromReturnValue( new ArrayIterator );
219 }
220
221 public function testCreateLocalizedHttpError() {
222 $rf = $this->createResponseFactory();
223 $response = $rf->createLocalizedHttpError( 404, new MessageValue( 'rftest' ) );
224 $body = $response->getBody();
225 $body->rewind();
226 $this->assertSame(
227 '{"messageTranslations":{"qqx":"rftest"},"httpCode":404,"httpReason":"Not Found"}',
228 $body->getContents() );
229 }
230
231 public function testFormatMessage() {
232 $rf = $this->createResponseFactory();
233 $mv = new MessageValue( 'rftest' );
234 $ret = $rf->formatMessage( $mv );
235 $this->assertIsArray( $ret );
236 $this->assertArrayHasKey( 'messageTranslations', $ret );
237 $this->assertIsArray( $ret['messageTranslations'] );
238 $this->assertArrayHasKey( 'qqx', $ret['messageTranslations'] );
239 $this->assertSame( 'rftest', $ret['messageTranslations']['qqx'] );
240 }
241
242 public function testGetFormattedMessage() {
243 $rf = $this->createResponseFactory();
244 $mv = new MessageValue( 'rftest' );
245
246 $ret = $rf->getFormattedMessage( $mv );
247 $this->assertIsString( $ret );
248 $this->assertSame( 'rftest', $ret );
249
250 $ret = $rf->getFormattedMessage( $mv, 'doesnotexist' );
251 $this->assertIsString( $ret );
252 $this->assertSame( 'rftest', $ret );
253 }
254 }