我正在使用Spring Boot + Webflux构建微服务,并且我有一个接受多部分文件上传的端点。当我使用curl和Postman测试时,哪个工作正常?
@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")
}
}
}
我现在正在尝试使用WebTestClient进行测试:
@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()
}
该端点返回500,因为我尚未创建将文件写入其中的临时目录位置。但是,即使我正在检查is2xxSuccessful
的断言,即使我正在检查is2xxSuccessful
,该测试仍通过,但由于500,我可以看到它失败了,但是我仍在进行绿色测试
不确定在这里我在做什么错。我映射到的VideoUploadException
只是扩展了ResponseStatusException
class VideoUploadException(reason: String) : ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, reason)