java RestAssured Multipart -将多个multiPart值连接为单个值

isr3a4wc  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(163)

我有多个API测试,它们有不同的多部分输入值集,这些值动态变化,我想创建一个公共函数,它可以将这些变化的多个多部分接受到一个单一的多部分行中,这样我就不必创建多个响应函数来接受这些变化的值。

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|
}

实际:

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);
}

预期逻辑类似于将所有多部分值组合到单个函数中:

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);
}

任何指针将不胜感激。

o3imoua4

o3imoua41#

类似下面的东西应该与您的用例相匹配,您可以添加一个类似buildMultiParts的帮助器方法,它将接受类型为string的var args并返回添加了multipart的修改后的请求

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;
    }

}

您还可以看到添加了参数的输出

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=[:]
oxf4rvwz

oxf4rvwz2#

这里有两个解决方案(从我的观点来看):
1.写一个自定义函数,参数是一个Map<String,?>,然后为每个Map的EntrySet添加多个multipart
1.第二种方法与第一种方法相同,但这次使用varargs来获取动态参数。但缺点是keyvalue的数据类型必须是String。它不像第一个解决方案那样是动态的。

相关问题