使用PathParam中的PathSegment测试POST请求时java断言失败
我有一个REST API POST请求,需要多个条目。这些条目是使用PathSegment提取的。API正在工作,但当我使用Rest Assured编写测试用例时,断言失败了。我将JAX-RS和Jersey用于API
我已经通过SO和其他一些论坛寻求答案,但没有令人满意的答案
我的REST API代码是:
@Produces(MediaType.APPLICATION_JSON)
@Path("/order/{id}/{var1: items}/{var2: qty}")
public final String orderMultipleItems(@PathParam("var1") final PathSegment itemPs, @PathParam("var2") final PathSegment qtyPs,
@PathParam("id") final int id) {
HashMap<Integer, Integer> items = new HashMap<Integer, Integer>();
//rest of the code
}
这是我放心的代码:
@Test
public final void testOrderMultipleItems() throws URISyntaxException, AssertionError {
String msg= given().contentType("application/json").when()
.post(TestUtil.getURI("/api/customer/order/1002/items;item=3006;item=3005/qty;q=1;q=1"))
.getBody().asString();
assertNotEquals("Order(s) Received", msg);
}
测试时我得到了404,但通过curl运行POST请求时得到了200。我的post请求在测试用例中出错了吗
如有任何建议,将不胜感激
# 1 楼答案
当您使用curl向服务器发送请求URI时,它会按原样提交:
但是,当您通过RestAssured(使用post)发送URI时,RestAssured编码特殊字符“'到'%3B'和'='到'%3D'请求URI如下:
这对服务器来说是不可理解的。这就是问题所在
因此,在发送请求之前,可以使用以下代码来避免这种情况
希望这能解决你的问题