Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/YurunHttp/Handler/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,14 +735,14 @@ public function coBatch($requests, $timeout = null)
$result[$k] = null;
$curlHandler = curl_init();
$recvHeaders[$k] = $saveFileFps[$k] = null;
$this->buildCurlHandlerBase($request, $curlHandler, $recvHeaders[$k], $saveFileFps[$k]);
$files = $request->getUploadedFiles();
$body = (string) $request->getBody();
if (!empty($files))
{
$body = FormDataBuilder::build($body, $files, $boundary);
$request = $request->withHeader('Content-Type', MediaType::MULTIPART_FORM_DATA . '; boundary=' . $boundary);
}
$this->buildCurlHandlerBase($request, $curlHandler, $recvHeaders[$k], $saveFileFps[$k]);
$this->buildCurlHandlerEx($request, $curlHandler, null, null, $body);
curl_multi_add_handle($mh, $curlHandler);
$curlHandlers[$k] = $curlHandler;
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/HttpRequestTest/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,32 @@ public function testUploadMulti(): void
});
}

/**
* 验证 Curl::send 单请求上传文件时,请求主头 Content-Type 被正确设置为 multipart/form-data。
*
* @return void
*/
public function testSendUploadContentType(): void
{
$this->call(function () {
$http = new HttpRequest();
$file = new UploadedFile(basename(__FILE__), MediaType::TEXT_HTML, __FILE__);
$http->content([
'file' => $file,
]);
$response = $http->post($this->host . '?a=info');
$this->assertResponse($response);
$data = $response->json(true);

// 上传文件被正确解析(间接证明主头 Content-Type 正确)
$this->assertTrue(isset($data['files']['file']));
// 直接验证请求主头 Content-Type 已被正确设置为 multipart/form-data 且带 boundary
$headers = array_change_key_case($data['header'], CASE_LOWER);
$this->assertArrayHasKey('content-type', $headers);
$this->assertStringStartsWith('multipart/form-data; boundary=', $headers['content-type']);
});
}

/**
* body.
*
Expand Down Expand Up @@ -639,6 +665,57 @@ public function testCoBatch(): void
});
}

/**
* 并发批量上传文件时,验证每个请求的主请求头 Content-Type 均被正确设置为 multipart/form-data。
*
* @return void
*/
public function testCoBatchUploadMulti(): void
{
$this->call(function () {
$file1 = new UploadedFile(basename(__FILE__), MediaType::TEXT_HTML, __FILE__);
$file2Path = __DIR__ . '/1.txt';
$file2 = new UploadedFile('1.txt', MediaType::TEXT_PLAIN, $file2Path);
$http1 = new HttpRequest();
$http1->url($this->host . '?a=info');
$http1->method('POST');
$http1->content([
'file' => $file1,
]);
$http2 = new HttpRequest();
$http2->url($this->host . '?a=info');
$http2->method('POST');
$http2->content([
'file' => $file2,
]);
$result = Batch::run([
'http1' => $http1,
'http2' => $http2,
]);
foreach ($result as $k => $response) {
$this->assertResponse($response);
$data = $response->json(true);
$this->assertTrue(isset($data['files']['file']));
// 验证请求主头 Content-Type 已被正确设置为 multipart/form-data(含 boundary)
$headers = array_change_key_case($data['header'], CASE_LOWER);
$this->assertArrayHasKey('content-type', $headers);
$this->assertStringStartsWith('multipart/form-data; boundary=', $headers['content-type']);
$file = $data['files']['file'];
if ($k === 'http1') {
$content = file_get_contents(__FILE__);
$this->assertEquals(MediaType::TEXT_HTML, $file['type']);
} elseif ($k === 'http2') {
$content = file_get_contents($file2Path);
$this->assertEquals(MediaType::TEXT_PLAIN, $file['type']);
} else {
$content = '';
}
$this->assertEquals(\strlen($content), $file['size']);
$this->assertEquals(md5($content), $file['hash']);
}
});
}

/**
* batch.
*
Expand Down