Expand ResponseFactory
[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 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() );
57 }
58
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() );
64 }
65
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() );
71 }
72
73 /** @expectedException \InvalidArgumentException */
74 public function testCreateHttpErrorInvalid() {
75 $rf = new ResponseFactory;
76 $rf->createHttpError( 200 );
77 }
78
79 public function testCreateHttpError() {
80 $rf = new ResponseFactory;
81 $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
82 $this->assertSame( 415, $response->getStatusCode() );
83 $body = $response->getBody();
84 $body->rewind();
85 $data = json_decode( $body->getContents(), true );
86 $this->assertSame( 415, $data['httpCode'] );
87 $this->assertSame( '...', $data['message'] );
88 }
89
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();
95 $body->rewind();
96 $data = json_decode( $body->getContents(), true );
97 $this->assertSame( 415, $data['httpCode'] );
98 $this->assertSame( 'hello', $data['message'] );
99 }
100
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();
106 $body->rewind();
107 $data = json_decode( $body->getContents(), true );
108 $this->assertSame( 500, $data['httpCode'] );
109 $this->assertSame( 'Error: exception of type Exception', $data['message'] );
110 }
111
112 public static function provideCreateFromReturnValue() {
113 return [
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"}' ],
120 [ [], '[]' ],
121 [ (object)[], '{}' ],
122 ];
123 }
124
125 /** @dataProvider provideCreateFromReturnValue */
126 public function testCreateFromReturnValue( $input, $expected ) {
127 $rf = new ResponseFactory;
128 $response = $rf->createFromReturnValue( $input );
129 $body = $response->getBody();
130 $body->rewind();
131 $this->assertSame( $expected, $body->getContents() );
132 }
133
134 /** @expectedException \InvalidArgumentException */
135 public function testCreateFromReturnValueInvalid() {
136 $rf = new ResponseFactory;
137 $rf->createFromReturnValue( new ArrayIterator );
138 }
139 }