use of org.springframework.http.HttpInputMessage in project fastjson by alibaba.
the class FastJsonHttpMessageConverter4Test method test_1.
public void test_1() throws Exception {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
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 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
method1.setAccessible(true);
method1.invoke(converter, int.class);
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"));
Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
use of org.springframework.http.HttpInputMessage in project fastjson by alibaba.
the class FastJsonHttpMessageConverterTest method test_read.
@SuppressWarnings("deprecation")
public void test_read() throws Exception {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setCharset(Charset.forName("UTF-8"));
Assert.assertEquals(Charset.forName("UTF-8"), converter.getCharset());
converter.setFeatures(SerializerFeature.BrowserCompatible);
Assert.assertEquals(1, converter.getFeatures().length);
Assert.assertEquals(SerializerFeature.BrowserCompatible, converter.getFeatures()[0]);
Assert.assertNull(converter.getDateFormat());
converter.setDateFormat("yyyyMMdd");
converter.setFilters(serializeFilter);
Assert.assertEquals(1, converter.getFilters().length);
Assert.assertEquals(serializeFilter, converter.getFilters()[0]);
converter.addSerializeFilter(serializeFilter);
Assert.assertEquals(2, converter.getFilters().length);
converter.addSerializeFilter(null);
converter.setSupportedMediaTypes(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON_UTF8 }));
Assert.assertEquals(1, converter.getSupportedMediaTypes().size());
Method method = FastJsonHttpMessageConverter.class.getDeclaredMethod("supports", Class.class);
method.setAccessible(true);
method.invoke(converter, int.class);
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, 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, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":\"123\"}", new String(bytes, "UTF-8"));
}
use of org.springframework.http.HttpInputMessage in project fastjson by alibaba.
the class FastJsonpHttpMessageConverter4Test method test_2.
public void test_2() 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;
}
};
MappingFastJsonValue mappingFastJsonValue = new MappingFastJsonValue(vo);
mappingFastJsonValue.setJsonpFunction("callback");
converter.write(mappingFastJsonValue, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("/**/callback({\"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.HttpInputMessage in project spring-boot by spring-projects.
the class HttpTunnelPayloadTests method getWithData.
@Test
public void getWithData() throws Exception {
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.setContent("hello".getBytes());
servletRequest.addHeader("x-seq", 123);
HttpInputMessage request = new ServletServerHttpRequest(servletRequest);
HttpTunnelPayload payload = HttpTunnelPayload.get(request);
assertThat(payload.getSequence()).isEqualTo(123L);
assertThat(getData(payload)).isEqualTo("hello".getBytes());
}
use of org.springframework.http.HttpInputMessage in project spring-boot by spring-projects.
the class HttpTunnelPayloadTests method getNoData.
@Test
public void getNoData() throws Exception {
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
HttpInputMessage request = new ServletServerHttpRequest(servletRequest);
HttpTunnelPayload payload = HttpTunnelPayload.get(request);
assertThat(payload).isNull();
}
Aggregations