Spring Cloud Contract 和 multipart/mixed 控制器方法
Spring Cloud Contract and multipart/mixed controller method
我正在努力为 multipart/mixed 控制器方法创建合同,如下所示:
@PostMapping("/posts")
public Json createNew(@RequestPart MultipartFile header,
@RequestPart MultipartFile photo,
@RequestPart Json info) {
// logic
return new Json("ok");
}
问题是,使用 Groovy DSL,我不能(或没有找到方法)指定请求部分的内容类型。
没有它,自动生成的测试将如下所示:
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF")
.multiPart("header", "filename1", "content1".getBytes())
.multiPart("photo", "filename1", "content1".getBytes())
.multiPart("info", "filename1", "{\r\n \"status\" : \"new upload\"\r\n}".getBytes());
失败,因为 "info" 部分不会反序列化为 Json 没有指定内容类型的对象。
我试过下面这段代码,但没有成功:
request {
method 'POST'
url '/posts'
multipart(
[
...
...
info : named(
name: value(consumer(regex(nonEmpty())), producer('filename1')),
content: value(consumer(regex(nonEmpty())), new ServerDslProperty(file("info.json"),
headers {contentType(applicationJson())})))
]
)
headers {
contentType('multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF')
}
}
所以,我的问题是:如何指定多部分请求的部分内容类型?
编辑 正如@Marcin所说,这是一个错误
Issue on github
问题已通过关闭此问题得到解决 https://github.com/spring-cloud/spring-cloud-contract/issues/599。只需使用最新的 2.0.0 版本(通过 Finchley 发行版)。
我正在努力为 multipart/mixed 控制器方法创建合同,如下所示:
@PostMapping("/posts")
public Json createNew(@RequestPart MultipartFile header,
@RequestPart MultipartFile photo,
@RequestPart Json info) {
// logic
return new Json("ok");
}
问题是,使用 Groovy DSL,我不能(或没有找到方法)指定请求部分的内容类型。 没有它,自动生成的测试将如下所示:
// given:
MockMvcRequestSpecification request = given()
.header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF")
.multiPart("header", "filename1", "content1".getBytes())
.multiPart("photo", "filename1", "content1".getBytes())
.multiPart("info", "filename1", "{\r\n \"status\" : \"new upload\"\r\n}".getBytes());
失败,因为 "info" 部分不会反序列化为 Json 没有指定内容类型的对象。
我试过下面这段代码,但没有成功:
request {
method 'POST'
url '/posts'
multipart(
[
...
...
info : named(
name: value(consumer(regex(nonEmpty())), producer('filename1')),
content: value(consumer(regex(nonEmpty())), new ServerDslProperty(file("info.json"),
headers {contentType(applicationJson())})))
]
)
headers {
contentType('multipart/form-data; boundary=----WebKitFormBoundaryBTX23kTw0Z5a5bsF')
}
}
所以,我的问题是:如何指定多部分请求的部分内容类型?
编辑 正如@Marcin所说,这是一个错误 Issue on github
问题已通过关闭此问题得到解决 https://github.com/spring-cloud/spring-cloud-contract/issues/599。只需使用最新的 2.0.0 版本(通过 Finchley 发行版)。