我的Micronaut文件上传控制器(使用Java)具有以下签名:
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
@Post
public Single<IdType> uploadFile(Publisher<CompletedFileUpload> files)
我有以下工作的Spock测试(在Groovy中):
@MicronautTest
class UploadSpecification extends Specification implements CsvFileBuilder {
@Inject
@Client('/')
HttpClient client
@Shared
List<String> allowedMimeTypes = List.of("text/csv", "application/vnd.ms-excel")
@Unroll
void "upload mailings csv with content type #mediaType"() {
given:
MultipartBody multipartBody = MultipartBody
.builder()
.addPart("files", "myfile.csv", new MediaType(mediaType), createCsvAsBytes(buildCsv()))
.build()
when:
HttpResponse response = client.toBlocking()
.exchange(POST("/v1/mailings", multipartBody).contentType(MediaType.MULTIPART_FORM_DATA_TYPE))
then:
response.status == HttpStatus.OK
where:
mediaType << allowedMimeTypes
}
}
我想对测试进行的更改是:我不想使用在测试顶部注入的标准HttpClient,而是要使用以下内容:
@Inject
UploadClient uploadClient
@Client(value = "/v1/mailings")
static interface UploadClient {
@Post
HttpResponse postFile(...)
}
我的问题是,客户端的postFile需要什么签名?我仍然可以使用MultipartBody,但以某种方式将其转换为CompletedFileUpload吗?我真的不确定如何解决这个问题,而我是RxJava的初学者.
任何帮助表示赞赏.
解决方法:
请尝试以下操作:
@Client(value ="/v1/upload")
static interface UploadClient {
@Post(uri = "/mailings", produces = MediaType.MULTIPART_FORM_DATA)
HttpResponse postFile(@Body MultipartBody file)
}
添加产品和身体注释