0

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.

Automation Engr
  • 444
  • 2
  • 4
  • 26

2 Answers2

2

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=[:]
Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13
0

There are 2 solutions here (from my view):

  1. write a custom function with parameter is a Map<String,?>, then foreach the EntrySet of the Map to add multiple multipart.

  2. Second solution is the same with the first one, but this time, using varargs to get dynamic parameter. But the downside is that the data type must be String to both key and value. It's not dynamic as first solution.

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • I think I was not able to explain my Expected result properly, what I meant was I need some logic which will allow me to merge multiple multipart values into a single value. The multipart function is a default RestAssured method which does not support this and just passing the key-values separated by ; does not work. Hope this makes sense. – Automation Engr May 24 '23 at 10:19
  • Java doesn’t allow ; as a separator, that’s why I suggested 2 other solutions. – lucas-nguyen-17 May 24 '23 at 11:55
  • @AutomationEngr check Abhay's answer, that's what I was talking about, solution 2. – lucas-nguyen-17 May 24 '23 at 15:28