TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
core/Tests/Unit/Http/ResponseTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Http;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
26 {
30  protected $response;
31 
32  protected function setUp()
33  {
34  $this->response = new Response();
35  }
36 
40  public function testStatusCodeIs200ByDefault()
41  {
42  $this->assertEquals(200, $this->response->getStatusCode());
43  }
44 
49  {
50  $response = $this->response->withStatus(400);
51  $this->assertNotSame($this->response, $response);
52  $this->assertEquals(400, $response->getStatusCode());
53  }
54 
59  {
60  return [
61  'too-low' => [99],
62  'too-high' => [600],
63  'null' => [null],
64  'bool' => [true],
65  'string' => ['foo'],
66  'array' => [[200]],
67  'object' => [(object) [200]],
68  ];
69  }
70 
75  public function testCannotSetInvalidStatusCode($code)
76  {
77  $this->expectException(\InvalidArgumentException::class);
78  $this->response->withStatus($code);
79  }
80 
85  {
86  $response = $this->response->withStatus(422);
87  $this->assertEquals('Unprocessable Entity', $response->getReasonPhrase());
88  }
89 
93  public function testCanSetCustomReasonPhrase()
94  {
95  $response = $this->response->withStatus(422, 'Foo Bar!');
96  $this->assertEquals('Foo Bar!', $response->getReasonPhrase());
97  }
98 
103  {
104  $this->expectException(\InvalidArgumentException::class);
105  new Response(['TOTALLY INVALID']);
106  }
107 
112  {
113  $body = new Stream('php://memory');
114  $status = 302;
115  $headers = [
116  'location' => ['http://example.com/'],
117  ];
118 
119  $response = new Response($body, $status, $headers);
120  $this->assertSame($body, $response->getBody());
121  $this->assertEquals(302, $response->getStatusCode());
122  $this->assertEquals($headers, $response->getHeaders());
123  }
124 
128  public function invalidStatusDataProvider()
129  {
130  return [
131  'true' => [true],
132  'false' => [false],
133  'float' => [100.1],
134  'bad-string' => ['Two hundred'],
135  'array' => [[200]],
136  'object' => [(object) ['statusCode' => 200]],
137  'too-small' => [1],
138  'too-big' => [600],
139  ];
140  }
141 
147  {
148  $this->expectException(\InvalidArgumentException::class);
149  $this->expectExceptionCode(1436717278);
150  new Response('php://memory', $code);
151  }
152 
157  {
158  return [
159  'true' => [true],
160  'false' => [false],
161  'int' => [1],
162  'float' => [1.1],
163  'array' => [['BODY']],
164  'stdClass' => [(object) ['body' => 'BODY']],
165  ];
166  }
167 
173  {
174  $this->expectException(\InvalidArgumentException::class);
175  $this->expectExceptionCode(1436717277);
176  new Response($body);
177  }
178 
183  {
184  $headers = [
185  ['INVALID'],
186  'x-invalid-null' => null,
187  'x-invalid-true' => true,
188  'x-invalid-false' => false,
189  'x-invalid-int' => 1,
190  'x-invalid-object' => (object) ['INVALID'],
191  'x-valid-string' => 'VALID',
192  'x-valid-array' => ['VALID'],
193  ];
194  $expected = [
195  'x-valid-string' => ['VALID'],
196  'x-valid-array' => ['VALID'],
197  ];
198  $response = new Response('php://memory', 200, $headers);
199  $this->assertEquals($expected, $response->getHeaders());
200  }
201 
206  {
207  return [
208  'name-with-cr' => ["X-Foo\r-Bar", 'value'],
209  'name-with-lf' => ["X-Foo\n-Bar", 'value'],
210  'name-with-crlf' => ["X-Foo\r\n-Bar", 'value'],
211  'name-with-2crlf' => ["X-Foo\r\n\r\n-Bar", 'value'],
212  'value-with-cr' => ['X-Foo-Bar', "value\rinjection"],
213  'value-with-lf' => ['X-Foo-Bar', "value\ninjection"],
214  'value-with-crlf' => ['X-Foo-Bar', "value\r\ninjection"],
215  'value-with-2crlf' => ['X-Foo-Bar', "value\r\n\r\ninjection"],
216  'array-value-with-cr' => ['X-Foo-Bar', ["value\rinjection"]],
217  'array-value-with-lf' => ['X-Foo-Bar', ["value\ninjection"]],
218  'array-value-with-crlf' => ['X-Foo-Bar', ["value\r\ninjection"]],
219  'array-value-with-2crlf' => ['X-Foo-Bar', ["value\r\n\r\ninjection"]],
220  ];
221  }
222 
228  {
229  $this->expectException(\InvalidArgumentException::class);
230  new Response('php://memory', 200, [$name => $value]);
231  }
232 }