unit-testingpactpact-jvm

how to create PACT for multipart/form-data uploading cdc test


I`m trying to create cdc test for uploading file verifying. I use DIUS library. I do not find any examples how to work with .withFileUpload() in DIUS. My code for pact is next:

@Pact(provider = PROVIDER, consumer = CONSUMER)
public RequestResponsePact createPact(PactDslWithProvider builder) throws Exception {
    DslPart responseBody = new PactDslJsonBody()
            .stringType("resource", DESTINATION_FILENAME)
            .stringType("requestId", null)
            .stringType("code", "201")
            .array("response")
            .closeArray()
            .asBody();

        return builder.given("UploadOperation")
                .uponReceiving("Upload operation")
                .path("/files/upload")
                .matchQuery("overwrite", "true")
                .matchQuery("destination_filename", DESTINATION_FILENAME)
                .withFileUpload("file",
                        ".gitignore",
                        "multipart/form-data", 
                         new byte[]{11,44,66,123,66}) // some bytes
                .willRespondWith()
                .status(201)
                .body(responseBody)
                .toPact();
        }

Code for pact creation and verification:

    @Test
    @PactVerification
    public void doTest() throws IOException {
        String url = String.format("Http://localhost:%d/files/upload?overwrite=true&destination_filename=%s", PORT, DESTINATION_FILENAME);

        // HttpEntity for request
        HttpEntity multipart = MultipartEntityBuilder.create()
                .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                .addBinaryBody("file", new byte[]{11,44,66,123,66}, 
                   ContentType.create("multipart/form-data"), ".gitignore")
                .build();

        // I make the request and get an answer                 
        HttpResponse response = Request.Put(url)
                .addHeader("Content-Type", "multipart/form-data; 
                   boundary=j72BRjsEynnAqDw43KTlsjxoKWsjdF_tl6N5")
                .body(multipart)
                .execute()
                .returnResponse();

        String json = EntityUtils.toString(response.getEntity());
        System.out.println("json=" + json);
        JSONObject jsonObject = new JSONObject(json);
        assertTrue(jsonObject.getString("code").equals("201"));
        assertTrue(response.getStatusLine().getStatusCode() == 201);}

but when I run the test i get: json={"error": Missing start boundary}

java.lang.AssertionError: Pact Test function failed with an exception, possibly due to ExpectedButNotReceived(expectedRequests=[    method: PUT
path: /files/upload
query: [destination_filename:[test], overwrite:[true]]
headers: [Content-Type:multipart/form-data; boundary=iYxVLiQ0ZrP5g0SUP2pWa-rg20UM4JFe90p]
matchers: MatchingRules(rules={query=Category(name=query, matchingRules={overwrite=MatchingRuleGroup(rules=[RegexMatcher(regex=true, example=null)], ruleLogic=AND), destination_filename=MatchingRuleGroup(rules=[RegexMatcher(regex=test, example=null)], ruleLogic=AND)}), header=Category(name=header, matchingRules={Content-Type=MatchingRuleGroup(rules=[RegexMatcher(regex=multipart/form-data;(\s*charset=[^;]*;)?\s*boundary=.*, example=multipart/form-data; boundary=iYxVLiQ0ZrP5g0SUP2pWa-rg20UM4JFe90p)], ruleLogic=AND)}), path=Category(name=path, matchingRules={})})
generators: Generators(categories={})
body: OptionalBody(state=PRESENT, value=--iYxVLiQ0ZrP5g0SUP2pWa-rg20UM4JFe90p
Content-Disposition: form-data; name="file"; filename=".gitignore"
Content-Type: multipart/form-data

,B{B
--iYxVLiQ0ZrP5g0SUP2pWa-rg20UM4JFe90p--
)])

...
Caused by: org.json.JSONException: JSONObject["code"] not found.

Whats wrong in my code? I suppose something wrong with Content type, with 'boundary' part. But I dont know how to specify arbitrary boundary. Maybe anybody knows another library where multipart/form-data uploading requests realized.

Thanks.


Solution

  • I found the solution from test example in DIUS library

    1. contentType in .withFileUpload() and accordingly in .addBinaryBody() methods shouldn`t be "multipart/form-data". It may be "form-data" for example.
    2. .addHeader in request method is not necessary because content type was already defined in body.