带有内容类型的 java REST Assured 响应主体:带有注入的 html 标签的 text/html

编程入门 行业动态 更新时间:2024-10-17 00:26:20
本文介绍了带有内容类型的 java REST Assured 响应主体:带有注入的 html 标签的 text/html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设有人开发了以json格式返回数据的rest api,但他没有将响应Content-Type设置为application/json而是text/html.

Let's say someone developed rest api that returns data in json format but he didn't set response Content-Type to application/json but text/html.

现在我已经用 REST Assured 编写了测试:

Now I have test written in REST Assured:

given() .param("username", username) .param("password", password) .when() .get("/authenticate") .then() .statusCode(200) .body("user.id" , hasItem(20));

但它不起作用.我注销了响应正文,这就是我得到的:

but it doesn't work. I logged out response body and and that's what I get:

<html> <body>{"key":"752E7A74E8F3999BE9EFE3EA0E0DF320","user":{"id":20,"firstName":"K1","lastName":"K1","role":"ROLE_CUSTOMER","phoneNumber":"888888888"},"expirationDate":"2016-08-10T13:52:50+02:00"}</body> </html>

和错误:

FAILED: test_login_as_valid_customer("888888888", "3432") java.lang.AssertionError: 1 expectation failed. JSON path key doesn't match. Expected: 20 Actual:

在 body 标签之间有我预期的 json,但是 html 标签从何而来?当我在 Postman 或 Paw 甚至网络浏览器中测试 api 方法时,我看不到它们的响应(因为它是使用 url 参数的简单 GET).

Between body tags there is my expected json but where did html tags come from? I cant't see them in response when I test api method in Postman or Paw or even in web browser (since it is simple GET with url params).

我怀疑我收到错误JSON 路径键不匹配".因为那些标签.

I suspect I get error "JSON path key doesn't match." because of those tags.

推荐答案

从最初的问题过去了 3 年,我遇到了同样的问题,这就是我所得到的.

3 years passed from the original question, I'm having the same issue and here's what I've got.

将响应体包装成 html 标签是 io.restassured.internal.support.Prettifier 类的工作.您可以通过以下方式禁用它.

Wrapping the response body into html tags is a work of io.restassured.internal.support.Prettifier class. You can disable it the following way.

RestAssured.requestSpecification.expect() .log().body(false) // `shouldPrettyPrint` parameter set to false

但它只影响日志记录,而不影响实际的响应正文.

But it affects only logging, not actual response body.

关于响应身体的断言是内容类型.如果某些 API 提供了无效的标头并且无法在服务器端修复它,您可以在客户端解决它.在 RestAssured 的情况下,可以使用过滤器来完成:

Assertions on response body mind it's content-type. If some API gives invalid headers and there's no way to fix it on the server side, you can work it around on the client side. In case of RestAssured, it can be done with a filter:

static final Filter FORCE_JSON_RESPONSE_BODY = (reqSpec, respSpec, ctx) -> { Response response = ctx.next(reqSpec, respSpec); ((RestAssuredResponseOptionsImpl) response).setContentType("application/json"); return response; }; // globally RestAssured.filters(FORCE_JSON_RESPONSE_BODY); // or per request Response response = RestAssured .given() .filters(FORCE_JSON_RESPONSE_BODY) .get("something")

更多推荐

带有内容类型的 java REST Assured 响应主体:带有注入的 html 标签的 text/html

本文发布于:2023-08-03 12:48:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主体   类型   标签   内容   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!