-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace addContentLengthHeader with ContentLength Middleware
This feature isn't needed in core and is better as optional middleware.
- Loading branch information
Showing
5 changed files
with
78 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
namespace Slim\Middleware; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Slim\Http\Body; | ||
|
||
class ContentLength | ||
{ | ||
/** | ||
* Invoke | ||
* | ||
* @param ServerRequestInterface $request PSR7 server request | ||
* @param ResponseInterface $response PSR7 response | ||
* @param callable $next Middleware callable | ||
* @return ResponseInterface PSR7 response | ||
*/ | ||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | ||
{ | ||
$response = $next($request, $response); | ||
|
||
// Add Content-Length header if not already added | ||
$size = $response->getBody()->getSize(); | ||
if ($size !== null && !$response->hasHeader('Content-Length')) { | ||
$response = $response->withHeader('Content-Length', (string) $size); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @link https://github.com/slimphp/Slim | ||
* @copyright Copyright (c) 2011-2017 Josh Lockhart | ||
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) | ||
*/ | ||
namespace Slim\Tests\Middleware; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Http\Body; | ||
use Slim\Http\Headers; | ||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
use Slim\Http\Uri; | ||
use Slim\Middleware\ContentLength; | ||
|
||
class ContentLengthTest extends TestCase | ||
{ | ||
public function testAddsContentLenght() | ||
{ | ||
$mw = new ContentLength('append'); | ||
|
||
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123'); | ||
$headers = new Headers(); | ||
$cookies = []; | ||
$serverParams = []; | ||
$body = new Body(fopen('php://temp', 'r+')); | ||
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); | ||
$response = new Response(); | ||
|
||
$next = function (ServerRequestInterface $req, ResponseInterface $res) { | ||
$res->write('Body'); | ||
return $res; | ||
}; | ||
|
||
$newResponse = $mw($request, $response, $next); | ||
|
||
$this->assertEquals(4, $newResponse->getHeaderLine('Content-Length')); | ||
} | ||
} |