use of org.springframework.http.HttpOutputMessage in project fastjson by alibaba.
the class FastJsonHttpMessageConverterTest method test_1.
public void test_1() throws Exception {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
HttpInputMessage input = new HttpInputMessage() {
public HttpHeaders getHeaders() {
// TODO Auto-generated method stub
return null;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
}
};
VO vo = (VO) converter.read(VO.class, VO.class, input);
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converter.write(vo, VO.class, null, out);
converter.write(vo, VO.class, MediaType.ALL, out);
HttpOutputMessage out2 = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders() {
private static final long serialVersionUID = 1L;
@Override
public MediaType getContentType() {
return MediaType.APPLICATION_JSON;
}
@Override
public long getContentLength() {
return 1;
}
};
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.ALL, out2);
}
use of org.springframework.http.HttpOutputMessage in project fastjson by alibaba.
the class FastJsonpHttpMessageConverter4Test method test_1.
public void test_1() throws Exception {
FastJsonpHttpMessageConverter4 converter = new FastJsonpHttpMessageConverter4();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
Method method1 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
method1.setAccessible(true);
method1.invoke(converter, int.class);
HttpInputMessage input = new HttpInputMessage() {
public HttpHeaders getHeaders() {
return null;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
}
};
VO vo = (VO) converter.read(VO.class, VO.class, input);
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
Method method2 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
use of org.springframework.http.HttpOutputMessage in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorMockTests method shouldHandleResponseHeaderAndBody.
@Test
public void shouldHandleResponseHeaderAndBody() throws Exception {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("header", "headerValue");
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.ACCEPTED);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
verify(stringHttpMessageConverter).write(eq("body"), eq(MediaType.TEXT_PLAIN), outputMessage.capture());
assertTrue(mavContainer.isRequestHandled());
assertEquals("headerValue", outputMessage.getValue().getHeaders().get("header").get(0));
}
use of org.springframework.http.HttpOutputMessage in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleMessageConversionExceptions.
@Test
public void testHandleMessageConversionExceptions() throws Exception {
HttpMessageConverter<?> extractor = new HttpMessageConverter() {
@Override
public boolean canRead(Class clazz, MediaType mediaType) {
return true;
}
@Override
public boolean canWrite(Class clazz, MediaType mediaType) {
return false;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
return null;
}
@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
throw new HttpMessageConversionException("error");
}
@Override
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
};
ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(extractor);
handler.setMessageConverters(messageConverters);
HttpHeaders headers = new HttpHeaders();
final String appSpecificBodyContent = "This user is not authorized";
InputStream appSpecificErrorBody = new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
ClientHttpResponse response = new TestClientHttpResponse(headers, 401, appSpecificErrorBody);
expected.expect(HttpClientErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.HttpOutputMessage in project spring-boot by spring-projects.
the class HttpTunnelPayloadTests method assignTo.
@Test
public void assignTo() throws Exception {
ByteBuffer data = ByteBuffer.wrap("hello".getBytes());
HttpTunnelPayload payload = new HttpTunnelPayload(2, data);
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
HttpOutputMessage response = new ServletServerHttpResponse(servletResponse);
payload.assignTo(response);
assertThat(servletResponse.getHeader("x-seq")).isEqualTo("2");
assertThat(servletResponse.getContentAsString()).isEqualTo("hello");
}
Aggregations