Hey,guys!I'm using pact framwork(version num:4.1.2...
# pact-jvm
u
Hey,guys!I'm using pact framwork(version num:4.1.28) to verify a multipart/form-data interface. This interface will receive a excel and parse to a Workbook. Here is the interface:
Copy code
@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:
Copy code
@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?❤️
👀 1
u
I would guess that the content type header is not being set correctly
Or the other option is that Apache Tika does not support Workbook format