Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / 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 MediaWikiTestCase;
9
10 /** @covers \MediaWiki\Rest\ResponseFactory */
11 class ResponseFactoryTest extends MediaWikiTestCase {
12 public static function provideEncodeJson() {
13 return [
14 [ (object)[], '{}' ],
15 [ '/', '"/"' ],
16 [ '£', '"£"' ],
17 [ [], '[]' ],
18 ];
19 }
20
21 /** @dataProvider provideEncodeJson */
22 public function testEncodeJson( $input, $expected ) {
23 $rf = new ResponseFactory;
24 $this->assertSame( $expected, $rf->encodeJson( $input ) );
25 }
26
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() );
35 }
36
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() );
43 }
44
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() );
50 }
51
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() );
57 }
58
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() );
64 }
65
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() );
71 }
72
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() );
78 }
79
80 /** @expectedException \InvalidArgumentException */
81 public function testCreateHttpErrorInvalid() {
82 $rf = new ResponseFactory;
83 $rf->createHttpError( 200 );
84 }
85
86 public function testCreateHttpError() {
87 $rf = new ResponseFactory;
88 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
89 $this->assertSame( 415, $response->getStatusCode() );
90 $body = $response->getBody();
91 $body->rewind();
92 $data = json_decode( $body->getContents(), true );
93 $this->assertSame( 415, $data['httpCode'] );
94 $this->assertSame( '...', $data['message'] );
95 }
96
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();
102 $body->rewind();
103 $data = json_decode( $body->getContents(), true );
104 $this->assertSame( 415, $data['httpCode'] );
105 $this->assertSame( 'hello', $data['message'] );
106 }
107
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();
113 $body->rewind();
114 $data = json_decode( $body->getContents(), true );
115 $this->assertSame( 500, $data['httpCode'] );
116 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
117 }
118
119 public static function provideCreateFromReturnValue() {
120 return [
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"}' ],
127 [ [], '[]' ],
128 [ (object)[], '{}' ],
129 ];
130 }
131
132 /** @dataProvider provideCreateFromReturnValue */
133 public function testCreateFromReturnValue( $input, $expected ) {
134 $rf = new ResponseFactory;
135 $response = $rf->createFromReturnValue( $input );
136 $body = $response->getBody();
137 $body->rewind();
138 $this->assertSame( $expected, $body->getContents() );
139 }
140
141 /** @expectedException \InvalidArgumentException */
142 public function testCreateFromReturnValueInvalid() {
143 $rf = new ResponseFactory;
144 $rf->createFromReturnValue( new ArrayIterator );
145 }
146 }