POST请求失败(确认测试)

时间:2015-08-13 15:29:13

标签: java json http-post rest-assured

我在确定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

我的想法用尽了。

我想要做的就是避免为所有测试编写完整的jsons,如果我可以跳过所有测试,它会更快#34;&#34;和{}。 我的方法是否正确?

2 个答案:

答案 0 :(得分:13)

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

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。您的服务器响应正确,因为服务器需要并了解JSON。它理解主体应该是JSON,因为您将JSON作为内容类型传递。

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

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

此处您的服务返回415,因为您错过了JSON内容类型。将paramparameterPOST一起使用时会发生的情况是您创建表单参数。表单参数也在请求体中发送但是表单参数不是JSON!像你一样指定“key”和“val”作为表单参数将与此相同:

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

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

  1. 您没有发送JSON
  2. 您的内容类型错误
  3. 因为(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),然后按照documentation中的描述创建一个地图:

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

    您可以在object-mapping documentation中阅读更多内容。

答案 1 :(得分:1)

我一直在寻找答案,我也想出来了。

将文件添加到src / test / resouces文件夹中,并将此代码添加到测试中。应该都很好

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