Search code examples
javamultipartform-datarest-assuredselenium-java

RestAssured Multipart - Concatenate multiple multiPart values into a single value


I am having multiple API tests which have different sets of multipart input values which vary dynamically, I want to create 1 common function which can accept these varying multiple multipart into a single multipart line so that I don't have to create multiple response functions to accept these varying values.

public test1(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
|key3 | value3|
}

public test2(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
}

public test3(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
|key3 | value3|
|key4 | value4|
|key5 | value5|
}

Actual:

 public void common() {
    Response response = given().urlEncodingEnabled(true).config(RestAssured.config()
                            .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
                            .multiPart("key1","value1")
                            .multiPart("key2","value2")
                            .multiPart("key3","value3")
                            .multiPart("key4","value4")
                            .multiPart("key5","value5")
                            .config(RestAssured.config()
                            .contentType(ContentType.JSON)
                            .headers(request.getHeaders())
                            .post(path);
}

Expected logic something like which combines all multipart values into a single function:

 public void common() {
    given().urlEncodingEnabled(true).config(RestAssured.config()
    .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
    .multiPart("key1","value1";"key2","value2";"key3","value3";"key4","value4";"key5","value5")
    .config(RestAssured.config()
    .contentType(ContentType.JSON)
    .headers(request.getHeaders())
    .post(path);
}

Any pointers will be appreciated.


Solution

  • Something like below should match your use cases, you can add a helper method like buildMultiParts below which will take var args of type string and return the modified request with added multiparts

    import static io.restassured.config.EncoderConfig.encoderConfig;
    import static io.restassured.RestAssured.given;
    import io.restassured.RestAssured;
    import io.restassured.http.ContentType;
    import io.restassured.internal.RequestSpecificationImpl;
    import io.restassured.specification.RequestSpecification;
    
    public class MultiPartExample {
    
        public static void main(String[] args) {
            RestAssured.baseURI = "http://example.com"; // Set your base URL
    
            RequestSpecification requestSpecification = buildMultiParts("key1", "value1", "key2",
                "value2", "key3", "value3", "key4", "value4", "key5", "value5");
            ((RequestSpecificationImpl) requestSpecification).getMultiPartParams().forEach(
                System.out::println);
            requestSpecification.urlEncodingEnabled(true).config(RestAssured.config().encoderConfig(
                    encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
                .headers("sample", "sample").post("/path");
        }
    
    
        private static RequestSpecification buildMultiParts(String... keyValues) {
            if (keyValues.length % 2 != 0) {
                throw new IllegalArgumentException("Invalid number of arguments");
            }
            int numPairs = keyValues.length / 2;
            RequestSpecification specification = given();
            for (int i = 0; i < numPairs; i++) {
                int index = i * 2;
                String key = keyValues[index];
                String value = keyValues[index + 1];
                specification.multiPart(key, value);
            }
    
            return specification;
        }
    
    }
    

    You can see the output also with params added

    controlName=key1, mimeType=text/plain, charset=<none>, fileName=file, content=value1, headers=[:]
    controlName=key2, mimeType=text/plain, charset=<none>, fileName=file, content=value2, headers=[:]
    controlName=key3, mimeType=text/plain, charset=<none>, fileName=file, content=value3, headers=[:]
    controlName=key4, mimeType=text/plain, charset=<none>, fileName=file, content=value4, headers=[:]
    controlName=key5, mimeType=text/plain, charset=<none>, fileName=file, content=value5, headers=[:]