TYPO3 CMS  TYPO3_7-6
MessageTest.php
Go to the documentation of this file.
1 <?php
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->setExpectedException('InvalidArgumentException', 'Invalid header value');
207  $message = $this->message->withHeader('X-Foo', [$value]);
208  }
209 
214  {
215  return [
216  'null' => [null],
217  'true' => [true],
218  'false' => [false],
219  'int' => [1],
220  'float' => [1.1],
221  'object' => [(object) ['foo' => 'bar']],
222  ];
223  }
224 
229  {
230  $this->setExpectedException('InvalidArgumentException', 'Invalid header value');
231  $message = $this->message->withHeader('X-Foo', $value);
232  }
233 
238  {
239  $this->setExpectedException('InvalidArgumentException', 'must be a string');
240  $message = $this->message->withAddedHeader('X-Foo', $value);
241  }
242 
247  {
248  $this->assertFalse($this->message->hasHeader('X-Foo'));
249  $message = $this->message->withoutHeader('X-Foo');
250  $this->assertNotSame($this->message, $message);
251  $this->assertFalse($message->hasHeader('X-Foo'));
252  }
253 
258  {
259  $this->assertSame([], $this->message->getHeader('X-Foo-Bar'));
260  }
261 
266  {
267  $this->assertSame('', $this->message->getHeaderLine('X-Foo-Bar'));
268  }
269 
274  {
275  return [
276  'name-with-cr' => ["X-Foo\r-Bar", 'value'],
277  'name-with-lf' => ["X-Foo\n-Bar", 'value'],
278  'name-with-crlf' => ["X-Foo\r\n-Bar", 'value'],
279  'name-with-2crlf' => ["X-Foo\r\n\r\n-Bar", 'value'],
280  'value-with-cr' => ['X-Foo-Bar', "value\rinjection"],
281  'value-with-lf' => ['X-Foo-Bar', "value\ninjection"],
282  'value-with-crlf' => ['X-Foo-Bar', "value\r\ninjection"],
283  'value-with-2crlf' => ['X-Foo-Bar', "value\r\n\r\ninjection"],
284  'array-value-with-cr' => ['X-Foo-Bar', ["value\rinjection"]],
285  'array-value-with-lf' => ['X-Foo-Bar', ["value\ninjection"]],
286  'array-value-with-crlf' => ['X-Foo-Bar', ["value\r\ninjection"]],
287  'array-value-with-2crlf' => ['X-Foo-Bar', ["value\r\n\r\ninjection"]],
288  'multi-line-header-space' => ['X-Foo-Bar', "value\r\n injection"],
289  'multi-line-header-tab' => ['X-Foo-Bar', "value\r\n\tinjection"],
290  ];
291  }
292 
297  public function doesNotAllowCRLFInjectionWhenCallingWithHeader($name, $value)
298  {
299  $this->setExpectedException('InvalidArgumentException');
300  $this->message->withHeader($name, $value);
301  }
302 
308  {
309  $this->setExpectedException('InvalidArgumentException');
310  $this->message->withAddedHeader($name, $value);
311  }
312 }
doesNotAllowCRLFInjectionWhenCallingWithAddedHeader($name, $value)
doesNotAllowCRLFInjectionWhenCallingWithHeader($name, $value)