TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
core/Tests/Unit/Http/MessageTest.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 $stream;
31 
35  protected $message;
36 
37  protected function setUp()
38  {
39  $this->stream = new Stream('php://memory', 'wb+');
40  $this->message = (new Message())->withBody($this->stream);
41  }
42 
46  public function protocolHasAcceptableDefault()
47  {
48  $this->assertEquals('1.1', $this->message->getProtocolVersion());
49  }
50 
55  {
56  $message = $this->message->withProtocolVersion('1.0');
57  $this->assertNotSame($this->message, $message);
58  $this->assertEquals('1.0', $message->getProtocolVersion());
59  }
60 
65  {
66  $this->assertSame($this->stream, $this->message->getBody());
67  }
68 
73  {
74  $stream = new Stream('php://memory', 'wb+');
75  $message = $this->message->withBody($stream);
76  $this->assertNotSame($this->message, $message);
77  $this->assertSame($stream, $message->getBody());
78  }
79 
84  {
85  $message = $this->message->withHeader('X-Foo', ['Foo', 'Bar']);
86  $this->assertNotSame($this->message, $message);
87  $this->assertEquals(['Foo', 'Bar'], $message->getHeader('X-Foo'));
88  }
89 
94  {
95  $message = $this->message->withHeader('X-Foo', ['Foo', 'Bar']);
96  $this->assertNotSame($this->message, $message);
97  $this->assertEquals('Foo,Bar', $message->getHeaderLine('X-Foo'));
98  }
99 
104  {
105  $message = $this->message->withHeader('X-Foo', ['Foo', 'Bar']);
106  $this->assertNotSame($this->message, $message);
107  $this->assertEquals(['X-Foo' => ['Foo', 'Bar']], $message->getHeaders());
108  }
109 
114  {
115  $message = $this->message
116  ->withHeader('X-Foo', 'Foo')
117  ->withAddedHeader('x-foo', 'Bar');
118  $this->assertNotSame($this->message, $message);
119  $this->assertEquals(['X-Foo' => ['Foo', 'Bar']], $message->getHeaders());
120  }
121 
126  {
127  $this->assertFalse($this->message->hasHeader('X-Foo'));
128  }
129 
134  {
135  $message = $this->message->withHeader('X-Foo', 'Foo');
136  $this->assertNotSame($this->message, $message);
137  $this->assertTrue($message->hasHeader('X-Foo'));
138  }
139 
144  {
145  $message = $this->message->withHeader('X-Foo', 'Foo');
146  $this->assertNotSame($this->message, $message);
147  $message2 = $message->withAddedHeader('X-Foo', 'Bar');
148  $this->assertNotSame($message, $message2);
149  $this->assertEquals('Foo,Bar', $message2->getHeaderLine('X-Foo'));
150  }
151 
155  public function canRemoveHeaders()
156  {
157  $message = $this->message->withHeader('X-Foo', 'Foo');
158  $this->assertNotSame($this->message, $message);
159  $this->assertTrue($message->hasHeader('x-foo'));
160  $message2 = $message->withoutHeader('x-foo');
161  $this->assertNotSame($this->message, $message2);
162  $this->assertNotSame($message, $message2);
163  $this->assertFalse($message2->hasHeader('X-Foo'));
164  }
165 
170  {
171  $message = $this->message
172  ->withHeader('X-Foo', 'Foo')
173  ->withAddedHeader('x-foo', 'Bar')
174  ->withAddedHeader('X-FOO', 'Baz');
175  $this->assertNotSame($this->message, $message);
176  $this->assertTrue($message->hasHeader('x-foo'));
177  $message2 = $message->withoutHeader('x-foo');
178  $this->assertNotSame($this->message, $message2);
179  $this->assertNotSame($message, $message2);
180  $this->assertFalse($message2->hasHeader('X-Foo'));
181  $headers = $message2->getHeaders();
182  $this->assertEquals(0, count($headers));
183  }
184 
189  {
190  return [
191  'null' => [null],
192  'true' => [true],
193  'false' => [false],
194  'int' => [1],
195  'float' => [1.1],
196  'array' => [['foo' => ['bar']]],
197  'object' => [(object) ['foo' => 'bar']],
198  ];
199  }
200 
205  {
206  $this->expectException(\InvalidArgumentException::class);
207  $this->expectExceptionCode(1436717266);
208  $this->message->withHeader('X-Foo', [$value]);
209  }
210 
215  {
216  return [
217  'null' => [null],
218  'true' => [true],
219  'false' => [false],
220  'int' => [1],
221  'float' => [1.1],
222  'object' => [(object) ['foo' => 'bar']],
223  ];
224  }
225 
230  {
231  $this->expectException(\InvalidArgumentException::class);
232  $this->expectExceptionCode(1436717266);
233  $this->message->withHeader('X-Foo', $value);
234  }
235 
240  {
241  $this->expectException(\InvalidArgumentException::class);
242  $this->expectExceptionCode(1436717267);
243  $this->message->withAddedHeader('X-Foo', $value);
244  }
245 
250  {
251  $this->assertFalse($this->message->hasHeader('X-Foo'));
252  $message = $this->message->withoutHeader('X-Foo');
253  $this->assertNotSame($this->message, $message);
254  $this->assertFalse($message->hasHeader('X-Foo'));
255  }
256 
261  {
262  $this->assertSame([], $this->message->getHeader('X-Foo-Bar'));
263  }
264 
269  {
270  $this->assertSame('', $this->message->getHeaderLine('X-Foo-Bar'));
271  }
272 
277  {
278  return [
279  'name-with-cr' => ["X-Foo\r-Bar", 'value'],
280  'name-with-lf' => ["X-Foo\n-Bar", 'value'],
281  'name-with-crlf' => ["X-Foo\r\n-Bar", 'value'],
282  'name-with-2crlf' => ["X-Foo\r\n\r\n-Bar", 'value'],
283  'value-with-cr' => ['X-Foo-Bar', "value\rinjection"],
284  'value-with-lf' => ['X-Foo-Bar', "value\ninjection"],
285  'value-with-crlf' => ['X-Foo-Bar', "value\r\ninjection"],
286  'value-with-2crlf' => ['X-Foo-Bar', "value\r\n\r\ninjection"],
287  'array-value-with-cr' => ['X-Foo-Bar', ["value\rinjection"]],
288  'array-value-with-lf' => ['X-Foo-Bar', ["value\ninjection"]],
289  'array-value-with-crlf' => ['X-Foo-Bar', ["value\r\ninjection"]],
290  'array-value-with-2crlf' => ['X-Foo-Bar', ["value\r\n\r\ninjection"]],
291  'multi-line-header-space' => ['X-Foo-Bar', "value\r\n injection"],
292  'multi-line-header-tab' => ['X-Foo-Bar', "value\r\n\tinjection"],
293  ];
294  }
295 
300  public function doesNotAllowCRLFInjectionWhenCallingWithHeader($name, $value)
301  {
302  $this->expectException(\InvalidArgumentException::class);
303  $this->message->withHeader($name, $value);
304  }
305 
311  {
312  $this->expectException(\InvalidArgumentException::class);
313  $this->message->withAddedHeader($name, $value);
314  }
315 }