Search in sources :

Example 6 with HttpOutputMessage

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);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with HttpOutputMessage

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);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonpHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with HttpOutputMessage

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));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) Test(org.junit.Test)

Example 9 with HttpOutputMessage

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);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 10 with HttpOutputMessage

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");
}
Also used : HttpOutputMessage(org.springframework.http.HttpOutputMessage) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ByteBuffer(java.nio.ByteBuffer) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

HttpOutputMessage (org.springframework.http.HttpOutputMessage)16 HttpHeaders (org.springframework.http.HttpHeaders)14 HttpInputMessage (org.springframework.http.HttpInputMessage)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Test (org.junit.Test)6 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)4 Method (java.lang.reflect.Method)4 MediaType (org.springframework.http.MediaType)4 IOException (java.io.IOException)3 StreamingHttpOutputMessage (org.springframework.http.StreamingHttpOutputMessage)3 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)2 FastJsonpHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4)2 OutputStream (java.io.OutputStream)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2 Expectations (org.jmock.Expectations)2 ResponseEntity (org.springframework.http.ResponseEntity)2 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)2 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)1 MappingFastJsonValue (com.alibaba.fastjson.support.spring.MappingFastJsonValue)1