使用queryParams而不是Json body进行Restassured API测试。

huangapple 未分类评论46阅读模式
英文:

Using queryParams instead of Json body in Restassured API testing

问题

我正在使用RestAssured库来自动化处理Java语言中的API响应,以下是我使用的API主体:

  1. {
  2. "meta": {
  3. "language": "en",
  4. "marketCountry": "IN",
  5. "sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
  6. },
  7. "contactInformation": {
  8. "company": "test",
  9. "firstName": "test",
  10. "lastName": "test",
  11. "address": "test",
  12. "zip": "test",
  13. "city": "test",
  14. "country": "IN",
  15. "countryDisplay": "India",
  16. "email": "test@test.com",
  17. "phoneNumber": "2324243243",
  18. "comments": "test"
  19. },
  20. "shipmentScale": {
  21. "domestic": false,
  22. "regional": false,
  23. "global": true
  24. },
  25. "shipmentProduct": {
  26. "FREIGHT": {
  27. "numberOfShipments": "50",
  28. "frequency": "WEEKLY",
  29. "checked": true
  30. }
  31. }

而不是使用整个API主体,我想要使用查询参数。有办法做到这一点吗?

这是我迄今为止一直在使用的内容,并且一直收到422状态代码错误:

  1. String result = given().header("Content-Type", "application/json")
  2. .header("Accept", "application/json").log().all().queryParam("marketCountry", "IN")
  3. .queryParam("shipmentScale.domestic", "false")
  4. .queryParam("shipmentScale.regional", "false")
  5. .queryParam("shipmentScale.global", "true")
  6. .queryParam("shipmentProduct.FREIGHT.checked", "true")
  7. .queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
  8. .queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
  9. .queryParam("contactInformation.company", "test")
  10. .queryParam("contactInformation.firstName", "test")
  11. .queryParam("contactInformation.lastName", "test")
  12. .queryParam("contactInformation.address", "test")
  13. .queryParam("contactInformation.zip", "test")
  14. .queryParam("contactInformation.city", "test")
  15. .queryParam("contactInformation.email", "test@test.com")
  16. .queryParam("contactInformation.phoneNumber", "213456")
  17. .queryParam("contactInformation.comments", "test")
  18. .queryParam("contactInformation.country", "IN")
  19. .queryParam("contactInformation.comments", "test")
  20. .when().post().then().assertThat().statusCode(200).extract().response().asString();
英文:

I am using RestAssured library to automate API responses in Java language and here is the API body I use:

  1. {
  2. "meta": {
  3. "language": "en",
  4. "marketCountry": "IN",
  5. "sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
  6. },
  7. "contactInformation": {
  8. "company": "test",
  9. "firstName": "test",
  10. "lastName": "test",
  11. "address": "test",
  12. "zip": "test",
  13. "city": "test",
  14. "country": "IN",
  15. "countryDisplay": "India",
  16. "email": "test@test.com",
  17. "phoneNumber": "2324243243",
  18. "comments": "test"
  19. },
  20. "shipmentScale": {
  21. "domestic": false,
  22. "regional": false,
  23. "global": true
  24. },
  25. "shipmentProduct": {
  26. "FREIGHT": {
  27. "numberOfShipments": "50",
  28. "frequency": "WEEKLY",
  29. "checked": true
  30. }
  31. }

instead of using the whole api body I wanna use queryParameters.
Is there a way doing that?

This is what I have been using so far and keep getting 422 status code error:

  1. String result = given().header("Content-Type","application/json" )
  2. .header("Accept","application/json").log().all().queryParam("marketCountry", "IN").queryParam("shipmentScale.domestic", "false")
  3. .queryParam("shipmentScale.regional", "false").queryParam("shipmentScale.global", "true")
  4. .queryParam("shipmentProduct.FREIGHT.checked", "true")
  5. .queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
  6. .queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
  7. .queryParam("contactInformation.company", "test")
  8. .queryParam("contactInformation.firstName", "test")
  9. .queryParam("contactInformation.lastName", "test")
  10. .queryParam("contactInformation.address", "test")
  11. .queryParam("contactInformation.zip", "test")
  12. .queryParam("contactInformation.city", "test")
  13. .queryParam("contactInformation.email", "test@test.com")
  14. .queryParam("contactInformation.phoneNumber", "213456")
  15. .queryParam("contactInformation.comments", "test")
  16. .queryParam("contactInformation.country", "IN")
  17. .queryParam("contactInformation.comments", "test")
  18. .when().post().then().assertThat().statusCode(200).extract().response().asString();

答案1

得分: 0

一个简单的方法是使用 RestAssured 库来自动化处理 Java 语言中的 API 响应。请参阅下面的 Java 类:

  1. public class Basics {
  2. public static void main(String[] args) {
  3. RestAssured.baseURI = "https://12.23.454.55";
  4. given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
  5. .body("{" +
  6. "\n" +
  7. "\n" +
  8. "\n" +
  9. "\n" +
  10. "\"meta\": {" +
  11. " \"language\": \"en\"," +
  12. " \"marketCountry\": \"IN\"," +
  13. " \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"" +
  14. "}," +
  15. "\"contactInformation\": {" +
  16. " \"company\": \"test\"," +
  17. " \"firstName\": \"test\"," +
  18. " \"lastName\": \"test\"," +
  19. " \"address\": \"test\"," +
  20. " \"zip\": \"test\"," +
  21. " \"city\": \"test\"," +
  22. " \"country\": \"IN\"," +
  23. " \"countryDisplay\": \"India\"," +
  24. " \"email\": \"test@test.com\"," +
  25. " \"phoneNumber\": \"2324243243\"," +
  26. " \"comments\": \"test\"" +
  27. "}," +
  28. "\"shipmentScale\": {" +
  29. " \"domestic\": false," +
  30. " \"regional\": false," +
  31. " \"global\": true" +
  32. "}," +
  33. "\"shipmentProduct\": {" +
  34. " \"FREIGHT\": {" +
  35. " \"numberOfShipments\": \"50\"," +
  36. " \"frequency\": \"WEEKLY\"," +
  37. " \"checked\": true" +
  38. " }" +
  39. "}" +
  40. "}").when().post("api/add/json")
  41. .then().log().all().assertThat().statusCode(200);
  42. }
  43. }

如果你愿意,你可以对响应进行更多的验证,或者你可以使用 JsonPath 类来解析你的 JSON 响应,以便进行进一步的验证。

然而,另一种方法是将 JSON 请求保持在一个单独的类方法中,并在请求体中返回该对象。或者更好的做法是探索使用 POJO Java 类的可能性。

英文:

A simple approach is to using RestAssured library to automate API responses in Java language. See the java class below:

  1. public class Basics {
  2. public static void main(String[] args) {
  3. RestAssured.baseURI = "https://12.23.454.55";
  4. given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
  5. .body("{\r\n" +
  6. "\r\n" +
  7. "\r\n" +
  8. "\r\n" +
  9. "\"meta\": {\r\n" +
  10. " \"language\": \"en\",\r\n" +
  11. " \"marketCountry\": \"IN\",\r\n" +
  12. " \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"\r\n" +
  13. "},\r\n" +
  14. "\"contactInformation\": {\r\n" +
  15. " \"company\": \"test\",\r\n" +
  16. " \"firstName\": \"test\",\r\n" +
  17. " \"lastName\": \"test\",\r\n" +
  18. " \"address\": \"test\",\r\n" +
  19. " \"zip\": \"test\",\r\n" +
  20. " \"city\": \"test\",\r\n" +
  21. " \"country\": \"IN\",\r\n" +
  22. " \"countryDisplay\": \"India\",\r\n" +
  23. " \"email\": \"test@test.com\",\r\n" +
  24. " \"phoneNumber\": \"2324243243\",\r\n" +
  25. " \"comments\": \"test\"\r\n" +
  26. "},\r\n" +
  27. "\"shipmentScale\": {\r\n" +
  28. " \"domestic\": false,\r\n" +
  29. " \"regional\": false,\r\n" +
  30. " \"global\": true\r\n" +
  31. "},\r\n" +
  32. "\"shipmentProduct\": {\r\n" +
  33. " \"FREIGHT\": {\r\n" +
  34. " \"numberOfShipments\": \"50\",\r\n" +
  35. " \"frequency\": \"WEEKLY\",\r\n" +
  36. " \"checked\": true\r\n" +
  37. " }\r\n" +
  38. "\r\n" +
  39. "\r\n" +
  40. "\r\n" +
  41. "}").when().post("api/add/json")
  42. .then().log().all().assertThat().statusCode(200);
  43. }
  44. }

If you like you can do more validation of the response or you can use the JsonPath class to parse your json responses in a bid to do further validation.

However, another approach is to keep your json request in a separate class' method and returning the object in your request body. Or better still you can explore the possibilities of a POJO java class.

huangapple
  • 本文由 发表于 2020年5月29日 17:23:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/62082642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定