|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Http\RequestFactory body parsing. |
| 5 | + */ |
| 6 | + |
| 7 | +use Nette\Http\InvalidRequestBodyException; |
| 8 | +use Nette\Http\RequestFactory; |
| 9 | +use Nette\Utils\JsonException; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | + |
| 13 | +require __DIR__ . '/../bootstrap.php'; |
| 14 | + |
| 15 | + |
| 16 | +$factory = new RequestFactory; |
| 17 | + |
| 18 | +$setRawBody = function ($rawBody) { |
| 19 | + $this->rawBodyCallback = function () use ($rawBody) { |
| 20 | + return $rawBody; |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | + |
| 25 | +test(function () use ($factory, $setRawBody) { |
| 26 | + $_SERVER = [ |
| 27 | + 'CONTENT_TYPE' => 'application/json', |
| 28 | + ]; |
| 29 | + |
| 30 | + $request = $factory->createHttpRequest(); |
| 31 | + $setRawBody->bindTo($request, $request)->__invoke('[1, 2.0, "3", true, false, null, {}]'); |
| 32 | + Assert::same('[1, 2.0, "3", true, false, null, {}]', $request->getRawBody()); |
| 33 | + Assert::equal([1, 2.0, '3', TRUE, FALSE, NULL, new stdClass], $request->body); |
| 34 | +}); |
| 35 | + |
| 36 | + |
| 37 | +test(function () use ($factory, $setRawBody) { |
| 38 | + $_SERVER = [ |
| 39 | + 'CONTENT_TYPE' => 'application/json', |
| 40 | + ]; |
| 41 | + |
| 42 | + $request = $factory->createHttpRequest(); |
| 43 | + $setRawBody->bindTo($request, $request)->__invoke('['); |
| 44 | + Assert::same('[', $request->getRawBody()); |
| 45 | + $e = Assert::exception([$request, 'getBody'], InvalidRequestBodyException::class); |
| 46 | + Assert::type(JsonException::class, $e->getPrevious()); |
| 47 | +}); |
| 48 | + |
| 49 | + |
| 50 | +test(function () use ($factory, $setRawBody) { |
| 51 | + $_SERVER = [ |
| 52 | + 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', |
| 53 | + ]; |
| 54 | + |
| 55 | + $_POST = [ |
| 56 | + 'a' => 'b', |
| 57 | + ]; |
| 58 | + |
| 59 | + $request = $factory->createHttpRequest(); |
| 60 | + $setRawBody->bindTo($request, $request)->__invoke('a=c'); |
| 61 | + Assert::same('a=c', $request->getRawBody()); |
| 62 | + Assert::equal(['a' => 'b'], $request->body); |
| 63 | +}); |
0 commit comments