首页 > 解决方案 > RestAssured: '没有在响应中指定支持的 Content-Type。对于支持的内容类型,Content-Type 为 'text/html;charset=ISO-8859-1''

问题描述

我期待服务器上出现此请求的错误,然后将其作为字符串与预期响应进行比较。

return given()
                .queryParams(incorrectParams)
                .when()
                .put("https://example.com/endpoint")
                .then()
                .statusCode(500)
                .extract().body().as(String.class)

问题是 RestAssured 在执行此操作时给了我一个错误:java.lang.IllegalStateException: Cannot parse object because no supported Content-Type is specified in response。内容类型是'text/html;charset=ISO-8859-1'。

我已在设置中添加了解码器,但它无济于事。

RestAssured.config().decoderConfig(new DecoderConfig(ContentType.HTML.withCharset(Charset.forName("ISO-8859-1"))));

错误情况下服务器返回的类型为:Content-Type: text/html;charset=ISO-8859-1

我还在调试器中看到默认解码器应该支持这样的响应。

还要提一件事 - 如果在 queryParams 中使用正确的参数发送请求,则返回 Content-Type: application/json 并且对于正面测试用例来说一切正常。

如何正确处理(提取)这样的响应?被保险人应该设置什么?

放心版本是4.4.0

标签: restcontent-typerest-assured

解决方案


只是为了将响应保存为字符串,您可以这样做。

.then()
.statusCode(500)
.extract().asString()

推荐阅读