|
5 | 5 | use bdk\HttpMessage\Message; |
6 | 6 | use bdk\HttpMessage\Request; |
7 | 7 | use bdk\HttpMessage\ServerRequest; |
| 8 | +use bdk\HttpMessage\Utility\ContentType; |
8 | 9 | use bdk\HttpMessage\Utility\ParseStr; |
9 | 10 | use ReflectionObject; |
10 | 11 |
|
@@ -159,6 +160,48 @@ public function testGetMethods() |
159 | 160 | ); |
160 | 161 | } |
161 | 162 |
|
| 163 | + public function testWithBodyClearsParsedBody() |
| 164 | + { |
| 165 | + $serverRequest = $this->createServerRequest('POST', '', array( |
| 166 | + 'CONTENT_TYPE' => ContentType::JSON, |
| 167 | + ))->withBody( |
| 168 | + $this->createStream(\json_encode(array('foo' => 'bar'))) |
| 169 | + ); |
| 170 | + |
| 171 | + $this->assertSame(array('foo' => 'bar'), $serverRequest->getParsedBody()); |
| 172 | + |
| 173 | + $serverRequest = $serverRequest->withBody( |
| 174 | + $this->createStream(\json_encode(array('ding' => 'dong'))) |
| 175 | + ); |
| 176 | + |
| 177 | + $this->assertSame(array('ding' => 'dong'), $serverRequest->getParsedBody()); |
| 178 | + } |
| 179 | + |
| 180 | + public function testWithBodyDoesNotClearParsedBody() |
| 181 | + { |
| 182 | + $serverRequest = $this->createServerRequest('POST', '', array( |
| 183 | + 'CONTENT_TYPE' => ContentType::JSON, |
| 184 | + ))->withBody( |
| 185 | + $this->createStream(\json_encode(array('foo' => 'bar'))) |
| 186 | + )->withParsedBody(array( |
| 187 | + 'snap' => 'crackle', |
| 188 | + )); |
| 189 | + |
| 190 | + // we explicitly set the parsed body - ignore the body |
| 191 | + $this->assertSame(array('snap' => 'crackle'), $serverRequest->getParsedBody()); |
| 192 | + |
| 193 | + // updating the body does nothing.... use the explicit parsed body |
| 194 | + $serverRequest = $serverRequest->withBody( |
| 195 | + $this->createStream(\json_encode(array('ding' => 'dong'))) |
| 196 | + ); |
| 197 | + $this->assertSame(array('snap' => 'crackle'), $serverRequest->getParsedBody()); |
| 198 | + |
| 199 | + // withParsedBody(null) -> we will attempt to parsed the body |
| 200 | + $serverRequest = $serverRequest->withParsedBody(null); |
| 201 | + $this->assertSame(array('ding' => 'dong'), $serverRequest->getParsedBody()); |
| 202 | + } |
| 203 | + |
| 204 | + |
162 | 205 | /** |
163 | 206 | * @param string $contentType Request content type |
164 | 207 | * @param string $body Request body |
|
0 commit comments