Search code examples
javaspringjunitrest-assuredspring-oauth2

Testing org.apache.http.entity.ContentType when the HTTP response content type doesn't contain a space


I am writing a test for an authorization server that tests that the content type of an oauth response is JSON. The authorization server is using spring-security-oauth2 2.0.1.4.RELEASE and my JUnit test is using 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();
}

When I run this test I am greeted with this failure

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".

So the value of the org.apache.http.entity.ContentType contains a space between the type and charset, but the the authorization server response content type does not.

Now I could get around this by doing

.assertThat().contentType(is(ContentType.APPLICATION_JSON.toString().replace(" ", "")))

But I feel that there must be a better way.

Is there a content type enum I can use that doesn't have a space? Can the authorization server be configured to include a space in the content type?


Solution

  • You could try using the following class from Spring: org.springframework.http.MediaType

    It looks like there is no space in the implementation.