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();
}
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();
}
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");
}
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\"");
}
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");
}
Aggregations