-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
namespace Nette\Http; | ||
|
||
use Nette; | ||
|
||
|
||
/** | ||
* Exception is thrown when a request body can not be parsed. | ||
*/ | ||
class InvalidRequestBodyException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Http\RequestFactory body parsing. | ||
*/ | ||
|
||
use Nette\Http\InvalidRequestBodyException; | ||
use Nette\Http\RequestFactory; | ||
use Nette\Utils\JsonException; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$factory = new RequestFactory; | ||
|
||
$setRawBody = function ($rawBody) { | ||
$this->rawBodyCallback = function () use ($rawBody) { | ||
return $rawBody; | ||
}; | ||
}; | ||
|
||
|
||
test(function () use ($factory, $setRawBody) { | ||
$_SERVER = [ | ||
'CONTENT_TYPE' => 'application/json', | ||
]; | ||
|
||
$request = $factory->createHttpRequest(); | ||
$setRawBody->bindTo($request, $request)->__invoke('[1, 2.0, "3", true, false, null, {}]'); | ||
Assert::same('[1, 2.0, "3", true, false, null, {}]', $request->getRawBody()); | ||
Assert::equal([1, 2.0, '3', TRUE, FALSE, NULL, new stdClass], $request->body); | ||
}); | ||
|
||
|
||
test(function () use ($factory, $setRawBody) { | ||
$_SERVER = [ | ||
'CONTENT_TYPE' => 'application/json', | ||
]; | ||
|
||
$request = $factory->createHttpRequest(); | ||
$setRawBody->bindTo($request, $request)->__invoke('['); | ||
Assert::same('[', $request->getRawBody()); | ||
$e = Assert::exception([$request, 'getBody'], InvalidRequestBodyException::class); | ||
Assert::type(JsonException::class, $e->getPrevious()); | ||
}); | ||
|
||
|
||
test(function () use ($factory, $setRawBody) { | ||
$_SERVER = [ | ||
'CONTENT_TYPE' => 'application/x-www-form-urlencoded', | ||
]; | ||
|
||
$_POST = [ | ||
'a' => 'b', | ||
]; | ||
|
||
$request = $factory->createHttpRequest(); | ||
$setRawBody->bindTo($request, $request)->__invoke('a=c'); | ||
Assert::same('a=c', $request->getRawBody()); | ||
Assert::equal(['a' => 'b'], $request->body); | ||
}); |