Spring boot 带有多部分文件上载的WebTestClient
我正在使用SpringBoot+Webflux构建一个微服务,我有一个端点可以接受多部分文件上传。当我用curl和Postman测试时,它工作得很好Spring boot 带有多部分文件上载的WebTestClient,spring-boot,kotlin,spring-webflux,Spring Boot,Kotlin,Spring Webflux,我正在使用SpringBoot+Webflux构建一个微服务,我有一个端点可以接受多部分文件上传。当我用curl和Postman测试时,它工作得很好 @PostMapping("/upload", consumes = [MULTIPART_FORM_DATA_VALUE]) fun uploadVideo(@RequestPart("video") filePart: Mono<FilePart>): Mono<UploadResult> { log.info
@PostMapping("/upload", consumes = [MULTIPART_FORM_DATA_VALUE])
fun uploadVideo(@RequestPart("video") filePart: Mono<FilePart>): Mono<UploadResult> {
log.info("Video upload request received")
return videoFilePart.flatMap { video ->
val fileName = video.filename()
log.info("Saving video to tmp directory: $fileName")
val file = temporaryFilePath(fileName).toFile()
video.transferTo(file)
.thenReturn(UploadResult(true))
.doOnError { error ->
log.error("Failed to save video to temporary directory", error)
}
.onErrorMap {
VideoUploadException("Failed to save video to temporary directory")
}
}
}
@Test
fun shouldSuccessfullyUploadVideo() {
client.post()
.uri("/video/upload")
.contentType(MULTIPART_FORM_DATA)
.syncBody(generateBody())
.exchange()
.expectStatus()
.is2xxSuccessful
}
private fun generateBody(): MultiValueMap<String, HttpEntity<*>> {
val builder = MultipartBodyBuilder()
builder.part("video", ClassPathResource("/videos/sunset.mp4"))
return builder.build()
}
class VideoUploadException(reason: String) : ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, reason)