如何使用java在rest-assured框架中传递大型复杂的xml体
我处理过不到20行的小型xml主体请求,并用java为其创建了键值对。 但我必须使用acordxml作为有效负载请求,以获得超过250行的响应。我尝试使用表单数据提供一个不起作用的.xml文件。 contentType是xml格式,响应以xml格式接收 有人能给我指引正确的方向吗?如果在一个框架中编码,如何实现这一点如何使用java在rest-assured框架中传递大型复杂的xml体,java,xml-parsing,rest-assured,Java,Xml Parsing,Rest Assured,我处理过不到20行的小型xml主体请求,并用java为其创建了键值对。 但我必须使用acordxml作为有效负载请求,以获得超过250行的响应。我尝试使用表单数据提供一个不起作用的.xml文件。 contentType是xml格式,响应以xml格式接收 有人能给我指引正确的方向吗?如果在一个框架中编码,如何实现这一点 @Test public void xmlPostRequest_Test() { RestAssured.baseURI = "http://localhost:800
@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"));
}
@测试
公共无效xmlPostRequest_测试(){
RestAssured.baseURI=”http://localhost:8006";
字符串requestBody=“\r\n”+
“100\r\n”+
“汤姆·克鲁斯\r\n”+
“124-542-5555\r\n”+
"";
响应=空;
响应=给定()。
contentType(contentType.XML)
.accept(ContentType.XML)
.body(requestBody)
.when()
.post(“/addClient”);
System.out.println(“Post-Response:+Response.asString());
System.out.println(“状态代码:+response.getStatusCode());
System.out.println(“响应是否包含'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()");
}
你能出示密码吗?你能告诉我你在用什么样的框架吗?这将是用java中重启的JAR进行测试。上面是示例代码,但是如果xml正文超过250行怎么办。有更好的方法吗?我的第一个建议是从文件中读取xml正文,您可以轻松地将其分配给requestBody。非常感谢!!!!成功了。我能够将文件路径作为有效负载传递到body中
public static Response validateXmlResponse() throws IOException {
// Navigate to xml file path attached in project
String FilePath = "c\downloads\filepath;
String XMLBodyToPost = new String(Files.readAllBytes(Paths.get(FilePath)));
// Call the baseUrl to test the request
RestAssured.baseURI = TestURL;
// Getting a reponse for submitted POST request
Response res = given().auth().basic(userName, password).body(XMLBodyToPost).
when().post()
.then()
.statusCode(200).and().contentType(ContentType.HTML).extract().response();
String response = res.asString();
// System.out.println("Returning response as string format:" + " " + response);
return res;
}