Java 当http响应内容类型不存在时,测试org.apache.http.entity.ContentType';不包含空格
我正在为授权服务器编写一个测试,测试oauth响应的内容类型是否为JSON。授权服务器使用的是Java 当http响应内容类型不存在时,测试org.apache.http.entity.ContentType';不包含空格,java,spring,junit,rest-assured,spring-oauth2,Java,Spring,Junit,Rest Assured,Spring Oauth2,我正在为授权服务器编写一个测试,测试oauth响应的内容类型是否为JSON。授权服务器使用的是spring-security-oauth2.0.1.4.RELEASE,我的JUnit测试使用的是rest-assured 2.9.0 @Test public void testTokenEndpoint() throws Exception { // Client Credentials Grant ResponseBody clientCredentialsGrantRespon
spring-security-oauth2.0.1.4.RELEASE
,我的JUnit测试使用的是rest-assured 2.9.0
@Test
public void testTokenEndpoint() throws Exception {
// Client Credentials Grant
ResponseBody clientCredentialsGrantResponseBody =
given(this.spec)
.authentication().basic(VALID_CLIENT_ID, VALID_CLIENT_SECRET)
.queryParameter("grant_type", CLIENT_CREDENTIALS_GRANT)
.queryParameter("username", VALID_USERNAME)
.queryParameter("password", VALID_PASSWORD)
.queryParameter("scope", VALID_SCOPES)
.when()
.post(OAUTH_TOKEN_ENDPOINT)
.then()
.assertThat().contentType(is(ContentType.APPLICATION_JSON.toString()))
.extract().response().body();
}
当我运行这个测试时,我遇到了这个失败
java.lang.AssertionError: 1 expectation failed.
Expected content-type is "application/json; charset=UTF-8" doesn't match actual content-type "application/json;charset=UTF-8".
因此,org.apache.http.entity.ContentType
的值包含类型和字符集之间的空格,但授权服务器响应内容类型不包含空格
现在我可以通过这样做来解决这个问题
.assertThat().contentType(is(ContentType.APPLICATION_JSON.toString().replace(" ", "")))
但我觉得一定有更好的办法
我是否可以使用没有空格的内容类型枚举?授权服务器是否可以配置为在内容类型中包含空格?您可以尝试使用Spring中的以下类:
org.springframework.http.MediaType
看起来没有空间。我认为没有这样的枚举,但是我觉得您的测试不应该使用
toString
来比较contentType。但这只是我的感觉。而且我也不知道如何正确地去做@JorgeCampos我同意toString
,但如果我不使用它,预期值会有尖括号而不是引号,错误看起来像预期内容类型与实际内容类型“application/json;charset=UTF-8”不匹配.
我不确定这是否重要。是的,我想用一种方法来测试它,就像测试HTTP协议的属性一样,所以它必须有某种方法来检查属性而不是值。