java - 如何使用java在放心框架中传递大而复杂的xml正文
问题描述
我处理了少于 20 行的小型 xml 正文请求,并在 java 中为它创建了键值对。但我必须使用 acord xml 作为有效负载请求才能获得超过 250 行的响应。我尝试使用 form-data 提供 .xml 文件,但它不起作用。contentType 是 xml 格式,并且以 xml 格式接收响应。
如果在框架中编码,有人可以指导我正确的方向吗?
@Test
public void xmlPostRequest_Test() {
RestAssured.baseURI = "http://localhost:8006";
String requestBody = "<client>\r\n" +
" <clientNo>100</clientNo>\r\n" +
" <name>Tom Cruise</name>\r\n" +
" <ssn>124-542-5555</ssn>\r\n" +
"</client>";
Response response = null;
response = given().
contentType(ContentType.XML)
.accept(ContentType.XML)
.body(requestBody)
.when()
.post("/addClient");
System.out.println("Post Response :" + response.asString());
System.out.println("Status Code :" + response.getStatusCode());
System.out.println("Does Reponse contains '100 Tom Cruise 124-542-5555'? :" + response.asString().contains("100 Tom Cruise 124-542-5555"));
}
解决方案
您应该使用文件来传递 xml 有效负载。
请查看以下代码并提供反馈。它已经过测试和工作。
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.filter.session.SessionFilter;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;
public class XmlExample {
//@Test
public void postComplexXML() throws IOException {
String FilePath="path\\to\\xml.xml";
String XMLBodyToPost=generateStringFromResource(FilePath);
RestAssured.baseURI="http://services.groupkt.com/state/get/IND/UP";
Response res= given().queryParam("key", "value").body(XMLBodyToPost).when().post().then().statusCode(201).and().
contentType(ContentType.XML).extract().response();
//Pass the RrstAssured Response to convert to XML
XmlPath x=rawToXML(res);
//Get country value from response
String country=x.get("RestResponse.result.country");
int size=x.get("result()");
}
推荐阅读
- windows - QWebEngineView 在干净的 Windows 安装中关闭应用程序而不会出错,但在开发 Windows 中工作正常(QT 创建者所在的位置)
- jestjs - 开玩笑 mockImplementation 总是返回 undefined
- android - android 11 媒体播放器通知问题
- woocommerce - woocommerce 单一和归档功能在索引页面上运行良好,但在其他页面上不起作用
- magento-1.9 - IDS Connect 的 Magento 集成
- python - 多个几何图形的 Open3D 可视化
- python - 保存已清理的数据框 python
- flutter - 我如何解决颤振中的昼夜主题问题?
- rxjs - 在 rxjs 中设置时间延迟时,“热”可观察对象不起作用
- python - 如何在python中的多个线程上分担责任