Search in sources :

Example 1 with MockHttpOutputMessage

use of org.springframework.mock.http.MockHttpOutputMessage in project modesti by jlsalmon.

the class SecurityControllerTest method json.

protected String json(Object o) throws IOException {
    MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
    this.mappingJackson2HttpMessageConverter.write(o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
    return mockHttpOutputMessage.getBodyAsString();
}
Also used : MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage)

Example 2 with MockHttpOutputMessage

use of org.springframework.mock.http.MockHttpOutputMessage in project thingsboard by thingsboard.

the class AbstractControllerTest method json.

@SuppressWarnings("unchecked")
protected String json(Object o) throws IOException {
    MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
    HttpMessageConverter converter = o instanceof String ? stringHttpMessageConverter : mappingJackson2HttpMessageConverter;
    converter.write(o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
    return mockHttpOutputMessage.getBodyAsString();
}
Also used : MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)

Example 3 with MockHttpOutputMessage

use of org.springframework.mock.http.MockHttpOutputMessage in project spring-security by spring-projects.

the class OAuth2AccessTokenResponseHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.

@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
    Converter tokenResponseParametersConverter = mock(Converter.class);
    given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class);
    this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter);
    // @formatter:off
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()).build();
    // @formatter:on
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Access Token Response");
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Converter(org.springframework.core.convert.converter.Converter) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpOutputMessage

use of org.springframework.mock.http.MockHttpOutputMessage in project spring-security by spring-projects.

the class OAuth2AccessTokenResponseHttpMessageConverterTests method writeInternalWhenOAuth2AccessTokenResponseThenWriteTokenResponse.

@Test
public void writeInternalWhenOAuth2AccessTokenResponseThenWriteTokenResponse() throws Exception {
    Instant expiresAt = Instant.now().plusSeconds(3600);
    Set<String> scopes = new LinkedHashSet<>(Arrays.asList("read", "write"));
    Map<String, Object> additionalParameters = new HashMap<>();
    additionalParameters.put("custom_parameter_1", "custom-value-1");
    additionalParameters.put("custom_parameter_2", "custom-value-2");
    // @formatter:off
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes).refreshToken("refresh-token-1234").additionalParameters(additionalParameters).build();
    // @formatter:on
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.messageConverter.writeInternal(accessTokenResponse, outputMessage);
    String tokenResponse = outputMessage.getBodyAsString();
    assertThat(tokenResponse).contains("\"access_token\":\"access-token-1234\"");
    assertThat(tokenResponse).contains("\"token_type\":\"Bearer\"");
    assertThat(tokenResponse).contains("\"expires_in\"");
    assertThat(tokenResponse).contains("\"scope\":\"read write\"");
    assertThat(tokenResponse).contains("\"refresh_token\":\"refresh-token-1234\"");
    assertThat(tokenResponse).contains("\"custom_parameter_1\":\"custom-value-1\"");
    assertThat(tokenResponse).contains("\"custom_parameter_2\":\"custom-value-2\"");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) HashMap(java.util.HashMap) Instant(java.time.Instant) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Test(org.junit.jupiter.api.Test)

Example 5 with MockHttpOutputMessage

use of org.springframework.mock.http.MockHttpOutputMessage in project spring-security by spring-projects.

the class OAuth2ErrorHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.

@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
    Converter errorParametersConverter = mock(Converter.class);
    given(errorParametersConverter.convert(any())).willThrow(RuntimeException.class);
    this.messageConverter.setErrorParametersConverter(errorParametersConverter);
    OAuth2Error oauth2Error = new OAuth2Error("unauthorized_client", "The client is not authorized", "https://tools.ietf.org/html/rfc6749#section-5.2");
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(oauth2Error, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Error");
}
Also used : MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Converter(org.springframework.core.convert.converter.Converter) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpOutputMessage (org.springframework.mock.http.MockHttpOutputMessage)10 Test (org.junit.jupiter.api.Test)5 Converter (org.springframework.core.convert.converter.Converter)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)2 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)2 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)2 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 lombok.val (lombok.val)1 CasYamlHttpMessageConverter (org.apereo.cas.web.CasYamlHttpMessageConverter)1