Pur*_*ple 14 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));
Run Code Online (Sandbox Code Playgroud)
但我试图使用param()或parameter()类似的方法:
这个给出:
given().parameter("key", "val").
when().post(url + resource).then().assertThat().statusCode(200);
Run Code Online (Sandbox Code Playgroud)
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));
Run Code Online (Sandbox Code Playgroud)
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));
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty
我的想法用尽了.
我要做的是避免为所有测试编写完整的jsons,如果我可以跳过所有""和{},它会更快.我的方法是否正确?
Joh*_*han 13
让我们看看你的第一个例子:
Run Code Online (Sandbox Code Playgroud)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作为内容类型传递.
那么让我们看看你的第二个例子:
Run Code Online (Sandbox Code Playgroud)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);
Run Code Online (Sandbox Code Playgroud)
所以在你的第二个例子中,实际上存在两个问题:
因为(2)你从服务器得到415
继续你的第三个例子:
Run Code Online (Sandbox Code Playgroud)given().parameter("key", "val"). when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));
这里发生的(可能)是您的服务器不包含响应主体,因为它希望请求包含"application/json"作为内容类型.所以没有主体断言(请求是错误的)!响应仅包含415状态(行)作为标题.
这引导我们进入你的最后一个例子:
Run Code Online (Sandbox Code Playgroud)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));
Run Code Online (Sandbox Code Playgroud)
由于在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));
Run Code Online (Sandbox Code Playgroud)
或者您创建数据的DTO表示,并将对象传递给REST Assured:
MyDTO myDTO = new MyDTO(...);
given().
contentType(ContentType.JSON).
body(myDTO).
when().
post(url + resource).
then().
statusCode(200).
body("otherVal", equalTo(otherVal));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19594 次 |
| 最近记录: |