@@ -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