郭佳佳
03/27/2023, 9:40 AM@PostMapping("/pact-test-form-data")
public SimpleResultVo<Object> pactTest(SecurityUploadHttpServletRequest request) {
List<String> result = new ArrayList<>();
CommonsMultipartFile[] mfs = request.getMultipartFiles();
try(InputStream is = mfs[0].getInputStream(); Workbook workbook = new XSSFWorkbook(is)) {
workbook.getSheetAt(0);
} catch (IOException e) {
e.printStackTrace();
}
result.add(request.getParameter("userName"));
result.add(request.getParameter("password"));
return ResultVoUtil.success(result);
}
Here is the pact I created:
@Pact(consumer = "FBB")
public RequestResponsePact formDataReqTest(PactDslWithProvider builder) throws IOException {
File file = new File("C:\\Users\\g00574640\\Desktop\\importProjectList.xlsx");
MultipartEntityBuilder entityBuilder =
MultipartEntityBuilder.create()
.addTextBody("userName", "guojiahui")
.addTextBody("password", "123456")
.addBinaryBody("file", file,
ContentType.create("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
"importProjectList.xlsx");
String expectStr =
TestingFileUtils.loadFileAsString("business/service/ResourceServiceTest/expect/formData.json");
Map<String, String> map = new HashMap<>();
map.put("Content-Type", "application/json");
return builder.uponReceiving("query a server info success.")
.path("/cloud-platform-service/v1/resource/pact-test-form-data")
.method("POST")
.body(entityBuilder)
.willRespondWith()
.status(200)
.body(expectStr)
.toPact();
}
@Test
@PactTestFor(providerName = "FormDataAPI", port = "8500", pactMethod = "formDataReqTest")
public void runTestformDataReq() throws IOException {
File file = new File("C:\\Users\\g00574640\\Desktop\\importProjectList.xlsx");
MultipartEntityBuilder entityBuilder =
MultipartEntityBuilder.create()
.addTextBody("userName", "guojiahui").addTextBody("password", "123456")
.addBinaryBody("file", file,
ContentType.create("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
"importProjectList.xlsx");
HttpEntity httpEntity = entityBuilder.build();
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestBuilder request =
<http://RequestBuilder.post|RequestBuilder.post>("<http://localhost:8500/cloud-platform-service/v1/resource/pact-test-form-data>")
.setEntity(httpEntity);
HttpUriRequest httpUriRequest = request.build();
httpclient.execute(httpUriRequest);
}
But when i verified this interface and executed Workbook workbook = new XSSFWorkbook(is)
,
I received an exception like this:`java.util.zip.DataFormatException: invalid block type` .
Did I write the wrong pact? I loaded this file and found that this file is broken.
What shoud I do?❤️uglyog
uglyog