Skip to content

Commit c7ec2c9

Browse files
committed
fix: incorrect logic in setRequestBody()
- When it is a JSON/XML request, REQUEST global should be empty. - When calling withBody() and withBodyFormat(), Content-Type is not set.
1 parent faa014c commit c7ec2c9

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

system/Test/FeatureTestTrait.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,36 +367,33 @@ protected function populateGlobals(string $method, Request $request, ?array $par
367367
* This allows the body to be formatted in a way that the controller is going to
368368
* expect as in the case of testing a JSON or XML API.
369369
*
370-
* @param array|null $params The parameters to be formatted and put in the body. If this is empty, it will get the
371-
* what has been loaded into the request global of the request class.
370+
* @param array|null $params The parameters to be formatted and put in the body.
372371
*/
373372
protected function setRequestBody(Request $request, ?array $params = null): Request
374373
{
375-
if (isset($this->requestBody) && $this->requestBody !== '') {
376-
$request->setBody($this->requestBody);
377-
378-
return $request;
379-
}
380-
381374
if (isset($this->bodyFormat) && $this->bodyFormat !== '') {
382-
if (empty($params)) {
383-
$params = $request->fetchGlobal('request');
384-
}
385-
386375
$formatMime = '';
387376
if ($this->bodyFormat === 'json') {
388377
$formatMime = 'application/json';
389378
} elseif ($this->bodyFormat === 'xml') {
390379
$formatMime = 'application/xml';
391380
}
392381

393-
if (! empty($formatMime) && ! empty($params)) {
382+
if ($formatMime !== '') {
383+
$request->setHeader('Content-Type', $formatMime);
384+
}
385+
386+
if ($params !== null && $formatMime !== '') {
394387
$formatted = Services::format()->getFormatter($formatMime)->format($params);
395388
$request->setBody($formatted);
396-
$request->setHeader('Content-Type', $formatMime);
397389
}
398390
}
399391

392+
// withBody() has higher priority than $params of withBodyFormat().
393+
if (isset($this->requestBody) && $this->requestBody !== '') {
394+
$request->setBody($this->requestBody);
395+
}
396+
400397
return $request;
401398
}
402399
}

0 commit comments

Comments
 (0)