POST请求失败(放心测试)

问题描述 投票:0回答:3

我在放心地发出 POST 请求时遇到问题。

此代码有效:

given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").    
        when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal)); 

但我试图使用

param()
parameter()
这样的方法:

这个给出:

given().parameter("key", "val").                                      
        when().post(url + resource).then().assertThat().statusCode(200);  

Expected status code <200> doesn't match actual status code <415>.

这个:

    given().parameter("key", "val").                                                         
            when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));  

java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response.
Try registering a default parser using:
   RestAssured.defaultParser(<parser type>);

还有这个:

RestAssured.defaultParser = Parser.JSON;                                                   
given().parameter("key", "val").                                                       
        when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty

我已经想不出出了什么问题。

我想做的是避免为所有测试编写完整的json,如果我可以跳过所有“”和{},速度会更快。 我的做法正确吗?

java json http-post rest-assured
3个回答
16
投票

让我们看看你的第一个例子:

given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").    
        when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal));

这里发生的情况是,您将

{ "key" : "val" }
(作为文本)放入请求正文中。该文本恰好是 JSON。从 REST Assured 的角度来看,您也可以输入
{ "key" : "val"
,这不是有效的 JSON。您的服务器响应正确,因为 server 需要并理解 JSON。它知道正文应该是 JSON,因为您将 JSON 作为内容类型传递。

让我们看看你的第二个例子:

given().parameter("key", "val").                                      
        when().post(url + resource).then().assertThat().statusCode(200);

此处您的服务返回 415,因为您缺少 JSON 内容类型。当您使用

param
parameter
POST
时,会发生什么:创建表单参数。表单参数也在请求正文中发送,但表单参数不是 JSON!像您一样指定“key”和“val”作为表单参数将与此相同:

given().contentType("x-www-form-urlencoded").body("key=val").when().url + resource).then().assertThat().statusCode(200);

所以在你的第二个例子中实际上有两个问题:

  1. 您没有发送 JSON
  2. 您的内容类型有误

并且由于 (2) 你从服务器得到 415

继续第三个示例:

given().parameter("key", "val").                                                         
            when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

这里(可能)发生的是您的服务器不包含响应正文,因为它期望请求包含“application/json”作为内容类型。所以没有主体可以断言(请求是错误的)!响应仅包含 415 状态(行)作为标头。

这让我们看到你的最后一个例子:

RestAssured.defaultParser = Parser.JSON;                                                   
given().parameter("key", "val").                                                       
        when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

在这里,您指示 REST Assured 将缺失的内容类型视为 JSON,但问题(再次)是您的服务器根本不返回任何响应正文,因此这无济于事。

解决方案:

您应该将受支持的 JSON 对象映射框架(Jackson、Faster Jackson、Simple JSON 或 Gson)放入类路径中(例如

jackson-databind
),然后按照 文档中所述创建一个映射:

Map<String, Object>  jsonAsMap = new HashMap<>();
map.put("key", "val");

given().
        contentType(ContentType.JSON).
        body(jsonAsMap).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

由于在 Java 中创建地图非常冗长,如果我有嵌套地图,我通常会这样做:

given().
        contentType(ContentType.JSON).
        body(new HashMap<String,Object>() {{
             put("key1, "val1");
             put("key2, "val2");
             put("key3", asList("val3", "val4"));
             put("nested", new HashMap<String,String>() {{
                 put("key4", "val4");
                 put("key5", "val5");
             }});
        }}).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

或者您创建数据的 DTO 表示形式,然后将一个对象传递给 REST Assured:

MyDTO myDTO = new MyDTO(...);
given().
        contentType(ContentType.JSON).
        body(myDTO).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

您可以在对象映射文档中阅读更多内容。


1
投票

我正在寻找答案,我也找到了..

将文件添加到 src/test/resouces 文件夹并将此代码添加到您的 test 中。应该一切都好

URL file = Resources.getResource("ModyNewFieldsReq.json");
String myRequest = Resources.toString(file,Charsets.UTF_8);

Response fieldResponse =  given ()
       .header("Authorization", AuthrztionValue)
       .header("X-App-Client-Id", XappClintIDvalue)
       .contentType("application/vnd.api+json")
       .body(myRequest).with()

     .when()
       .post(dataPostUrl)    

    .then()
       .assertThat()
       .log().ifError()
       .statusCode(200)
       .extract().response();

Assert.assertFalse(fieldResponse.asString().contains("isError"));

0
投票

正如问题所述“无法序列化对象,因为在类路径中找不到 json 序列化器” 添加对 Jackson Databind 的依赖 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind

© www.soinside.com 2019 - 2024. All rights reserved.