/** * Return the body content interpreted as a UTF-8 string. */ public String getBodyAsString() { return getBodyAsString(DEFAULT_CHARSET); }
/** * Return the body content as a string. * @param charset the charset to use to turn the body content to a String */ public String getBodyAsString(Charset charset) { byte[] bytes = getBodyAsBytes(); return new String(bytes, charset); }
@Test public void writeInternalWhenOAuth2ErrorThenWriteErrorResponse() throws Exception { OAuth2Error oauth2Error = new OAuth2Error("unauthorized_client", "The client is not authorized", "https://tools.ietf.org/html/rfc6749#section-5.2"); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.messageConverter.writeInternal(oauth2Error, outputMessage); String errorResponse = outputMessage.getBodyAsString(); assertThat(errorResponse).contains("\"error\":\"unauthorized_client\""); assertThat(errorResponse).contains("\"error_description\":\"The client is not authorized\""); assertThat(errorResponse).contains("\"error_uri\":\"https://tools.ietf.org/html/rfc6749#section-5.2\""); }
@Before public void setUp() throws Exception { exceptionReportHttpMessageConverter = new ExceptionReportHttpMessageConverter(); httpMessageConverter = mock(HttpMessageConverter.class); httpOutputMessage = new MockHttpOutputMessage(); exceptionReportHttpMessageConverter.setMessageConverters( new HttpMessageConverter<?>[]{httpMessageConverter}); when(httpMessageConverter.canWrite(any(Class.class), any(MediaType.class))).thenReturn(true); when(httpMessageConverter.getSupportedMediaTypes()).thenReturn(Arrays.asList(APPLICATION_JSON)); }
@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"); OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse .withToken("access-token-1234") .tokenType(OAuth2AccessToken.TokenType.BEARER) .expiresIn(expiresAt.toEpochMilli()) .scopes(scopes) .refreshToken("refresh-token-1234") .additionalParameters(additionalParameters) .build(); 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\""); }
@Test public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() { Converter errorParametersConverter = mock(Converter.class); when(errorParametersConverter.convert(any())).thenThrow(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(); assertThatThrownBy(() -> this.messageConverter.writeInternal(oauth2Error, outputMessage)) .isInstanceOf(HttpMessageNotWritableException.class) .hasMessageContaining("An error occurred writing the OAuth 2.0 Error"); } }
MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage(); this.mappingJackson2HttpMessageConverter.write(o, MediaType.APPLICATION_JSON, mockHttpOutputMessage); return mockHttpOutputMessage.getBodyAsString();
/** * Return the body content interpreted as a UTF-8 string. */ public String getBodyAsString() { return getBodyAsString(DEFAULT_CHARSET); }
/** * Return the body content as a string. * @param charset the charset to use to turn the body content to a String */ public String getBodyAsString(Charset charset) { byte[] bytes = getBodyAsBytes(); return new String(bytes, charset); }
@Test public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() { Converter tokenResponseParametersConverter = mock(Converter.class); when(tokenResponseParametersConverter.convert(any())).thenThrow(RuntimeException.class); this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter); OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse .withToken("access-token-1234") .tokenType(OAuth2AccessToken.TokenType.BEARER) .expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()) .build(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); assertThatThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)) .isInstanceOf(HttpMessageNotWritableException.class) .hasMessageContaining("An error occurred writing the OAuth 2.0 Access Token Response"); } }
.addOffer(new Offer().withAvailability(ItemAvailability.IN_STOCK)); final MockHttpOutputMessage msg = new MockHttpOutputMessage(); halConverter.write(product, MediaTypes.HAL_JSON, msg); String json = msg.getBodyAsString(); log.info("{}", json);
/** * Return the body content interpreted as a UTF-8 string. */ public String getBodyAsString() { return getBodyAsString(DEFAULT_CHARSET); }
/** * Return the body content as a string. * @param charset the charset to use to turn the body content to a String */ public String getBodyAsString(Charset charset) { byte[] bytes = getBodyAsBytes(); try { return new String(bytes, charset.name()); } catch (UnsupportedEncodingException ex) { // should not occur throw new IllegalStateException(ex); } }
/** * Return the body content interpreted as a UTF-8 string. */ public String getBodyAsString() { return getBodyAsString(DEFAULT_CHARSET); }
/** * Return the body content as a string. * @param charset the charset to use to turn the body content to a String */ public String getBodyAsString(Charset charset) { byte[] bytes = getBodyAsBytes(); try { // Use return new String(bytes, charset.name()); } catch (UnsupportedEncodingException ex) { // should not occur throw new InternalError(ex.getMessage()); } }