Skip to content

Commit

Permalink
Force Content-Type header contents to lowercase as per
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus authored and clue committed Sep 25, 2017
1 parent 3c94b05 commit a0c8de7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Middleware/RequestBodyParserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class RequestBodyParserMiddleware
{
public function __invoke(ServerRequestInterface $request, $next)
{
$type = $request->getHeaderLine('Content-Type');
$type = strtolower($request->getHeaderLine('Content-Type'));

if ($type === 'application/x-www-form-urlencoded') {
return $next($this->parseFormUrlencoded($request));
Expand Down
60 changes: 55 additions & 5 deletions tests/Middleware/RequestBodyParserMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,71 @@
namespace React\Tests\Http\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Http\ServerRequest;
use React\Tests\Http\TestCase;
use RingCentral\Psr7\BufferStream;
use React\Stream\ThroughStream;
use React\Http\HttpBodyStream;

final class RequestBodyParserMiddlewareTest extends TestCase
{
public function testFormUrlencodedParsing()
{
$middleware = new RequestBodyParserMiddleware();
$request = new ServerRequest(
200,
'POST',
'https://example.com/',
array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
'hello=world'
);

/** @var ServerRequestInterface $parsedRequest */
$parsedRequest = $middleware(
$request,
function (ServerRequestInterface $request) {
return $request;
}
);

$this->assertSame(
array('hello' => 'world'),
$parsedRequest->getParsedBody()
);
$this->assertSame('hello=world', (string)$parsedRequest->getBody());
}

public function testFormUrlencodedParsingIgnoresCaseForHeadersButRespectsContentCase()
{
$middleware = new RequestBodyParserMiddleware();
$request = new ServerRequest(
'POST',
'https://example.com/',
array(
'CONTENT-TYPE' => 'APPLICATION/X-WWW-Form-URLEncoded',
),
'Hello=World'
);

/** @var ServerRequestInterface $parsedRequest */
$parsedRequest = $middleware(
$request,
function (ServerRequestInterface $request) {
return $request;
}
);

$this->assertSame(
array('Hello' => 'World'),
$parsedRequest->getParsedBody()
);
$this->assertSame('Hello=World', (string)$parsedRequest->getBody());
}

public function testFormUrlencodedParsingNestedStructure()
{
$middleware = new RequestBodyParserMiddleware();
$request = new ServerRequest(
'POST',
'https://example.com/',
array(
'Content-Type' => 'application/x-www-form-urlencoded',
Expand Down

0 comments on commit a0c8de7

Please sign in to comment.