Skip to content

Commit

Permalink
Merge pull request #8161 from kenjis/fix-Request-body-0
Browse files Browse the repository at this point in the history
fix: when request body is `0`, $body will be null
  • Loading branch information
kenjis authored Nov 7, 2023
2 parents 17dab6a + 42be01d commit 0708fd7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 6,
'count' => 5,
'path' => __DIR__ . '/system/HTTP/IncomingRequest.php',
];
$ignoreErrors[] = [
Expand Down
7 changes: 6 additions & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,14 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
$body = file_get_contents('php://input');
}

// If file_get_contents() returns false or empty string, set null.
if ($body === false || $body === '') {
$body = null;
}

$this->config = $config;
$this->uri = $uri;
$this->body = ! empty($body) ? $body : null;
$this->body = $body;
$this->userAgent = $userAgent;
$this->validLocales = $config->supportedLocales;

Expand Down
11 changes: 9 additions & 2 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public function testGetJsonVarReturnsNullFromNullBody(): void
$this->assertNull($request->getJsonVar('myKey'));
}

public function testgetJSONReturnsNullFromNullBody(): void
public function testGetJSONReturnsNullFromNullBody(): void
{
$config = new App();
$config->baseURL = 'http://example.com/';
Expand Down Expand Up @@ -847,7 +847,7 @@ public function testGetPostSecondStreams(): void
$this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost());
}

public function testWithFalseBody(): void
public function testGetBodyWithFalseBody(): void
{
// Use `false` here to simulate file_get_contents returning a false value
$request = $this->createRequest(null, false);
Expand All @@ -856,6 +856,13 @@ public function testWithFalseBody(): void
$this->assertNull($request->getBody());
}

public function testGetBodyWithZero(): void
{
$request = $this->createRequest(null, '0');

$this->assertSame('0', $request->getBody());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3020
*/
Expand Down

0 comments on commit 0708fd7

Please sign in to comment.