Skip to content

Commit ea6fa7e

Browse files
committed
fix: keep mock uploads multipart after JSON requests (#7987)
1 parent 15486cc commit ea6fa7e

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎system/Test/FeatureTestTrait.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ protected function setRequestBody(Request $request, ?array $params = null): Requ
460460
$request->setBody($this->requestBody);
461461
}
462462

463-
if ($this->bodyFormat !== '') {
463+
if ($this->bodyFormat !== '' && $request->getFiles() === []) {
464464
$formatMime = '';
465465
if ($this->bodyFormat === 'json') {
466466
$formatMime = 'application/json';

‎system/Test/Mock/MockUploadedFile.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
?int $error = UPLOAD_ERR_OK,
2929
?string $clientPath = null,
3030
) {
31-
parent::__construct($path, $originalName, $mimeType, $size, $error, $clientPath);
31+
parent::__construct($path, $originalName, $mimeType ?? '', $size, $error, $clientPath);
3232
}
3333

3434
public function isValid(): bool

‎tests/system/Test/FeatureTestTraitTest.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,60 @@ public function testUploadedFilesAreClearedAfterRequest(): void
220220
}
221221
}
222222

223+
public function testUploadAfterJsonRequestUsesMultipartOnlyForUpload(): void
224+
{
225+
$source = tempnam(sys_get_temp_dir(), 'ci4-upload-');
226+
file_put_contents($source, 'file contents');
227+
228+
try {
229+
$this->withRoutes([
230+
[
231+
'POST',
232+
'upload',
233+
static function (): string {
234+
$request = service('request');
235+
236+
return $request->getHeaderLine('Content-Type') . ':'
237+
. ($request->getFile('document') === null ? 'absent' : 'present') . ':'
238+
. $request->getPost('title');
239+
},
240+
],
241+
]);
242+
243+
$this->assertSame(
244+
'application/json:absent:First',
245+
$this->withBodyFormat('json')->post('upload', ['title' => 'First'])->response()->getBody(),
246+
);
247+
$this->assertSame(
248+
'multipart/form-data:present:Second',
249+
$this->withFiles(['document' => new MockUploadedFile($source, 'document.txt')])
250+
->post('upload', ['title' => 'Second'])->response()->getBody(),
251+
);
252+
$this->assertSame(
253+
'application/json:absent:Third',
254+
$this->post('upload', ['title' => 'Third'])->response()->getBody(),
255+
);
256+
} finally {
257+
@unlink($source);
258+
}
259+
}
260+
261+
public function testMockUploadedFileWithoutMimeTypeHasStringClientMimeType(): void
262+
{
263+
$source = tempnam(sys_get_temp_dir(), 'ci4-upload-');
264+
file_put_contents($source, 'file contents');
265+
266+
try {
267+
$file = new MockUploadedFile($source, 'document.txt');
268+
269+
$this->assertTrue($file->isValid());
270+
$this->assertSame('', $file->getClientMimeType());
271+
$this->assertSame('text/plain', (new MockUploadedFile($source, 'document.txt', 'text/plain'))->getClientMimeType());
272+
} finally {
273+
@unlink($source);
274+
}
275+
}
276+
223277
public function testCallValidationTwice(): void
224278
{
225279
$this->withRoutes([

0 commit comments

Comments
 (0)