org.springframework.http.MockHttpOutputMessage Java Examples

The following examples show how to use org.springframework.http.MockHttpOutputMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MappingJackson2HttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeWithBaseType() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	converter.write(body, MyBase.class, null, outputMessage);
	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":\"AQI=\""));
	assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8),
			outputMessage.getHeaders().getContentType());
}
 
Example #2
Source File: StandardMultipartHttpServletRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void multipartFileResource() throws IOException {
	String name = "file";
	String disposition = "form-data; name=\"" + name + "\"; filename=\"myFile.txt\"";
	StandardMultipartHttpServletRequest request = requestWithPart(name, disposition, "myBody");
	MultipartFile multipartFile = request.getFile(name);

	assertNotNull(multipartFile);

	MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
	map.add(name, multipartFile.getResource());

	MockHttpOutputMessage output = new MockHttpOutputMessage();
	new FormHttpMessageConverter().write(map, null, output);

	assertThat(output.getBodyAsString(StandardCharsets.UTF_8), containsString(
			"Content-Disposition: form-data; name=\"file\"; filename=\"myFile.txt\"\r\n" +
					"Content-Type: text/plain\r\n" +
					"Content-Length: 6\r\n" +
					"\r\n" +
					"myBody\r\n"));
}
 
Example #3
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[]{"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[]{0x1, 0x2});
	converter.write(body, null, outputMessage);
	Charset utf8 = Charset.forName("UTF-8");
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("<string>Foo</string>"));
	assertTrue(result.contains("<number>42</number>"));
	assertTrue(result.contains("<fraction>42.0</fraction>"));
	assertTrue(result.contains("<array><array>Foo</array><array>Bar</array></array>"));
	assertTrue(result.contains("<bool>true</bool>"));
	assertTrue(result.contains("<bytes>AQI=</bytes>"));
	assertEquals("Invalid content-type", new MediaType("application", "xml", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #4
Source File: FormHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeForm() throws IOException {
	MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
	body.set("name 1", "value 1");
	body.add("name 2", "value 2+1");
	body.add("name 2", "value 2+2");
	body.add("name 3", null);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.write(body, MediaType.APPLICATION_FORM_URLENCODED, outputMessage);

	assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3",
			outputMessage.getBodyAsString(StandardCharsets.UTF_8));
	assertEquals("Invalid content-type", "application/x-www-form-urlencoded;charset=UTF-8",
			outputMessage.getHeaders().getContentType().toString());
	assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
			outputMessage.getHeaders().getContentLength());
}
 
Example #5
Source File: JsonbHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeWithBaseType() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	this.converter.write(body, MyBase.class, null, outputMessage);
	Charset utf8 = StandardCharsets.UTF_8;
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #6
Source File: GsonHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
	ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
	};

	String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
			"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
	inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

	List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
	assertEquals(1, results.size());
	MyBean result = results.get(0);
	assertEquals("Foo", result.getString());
	assertEquals(42, result.getNumber());
	assertEquals(42F, result.getFraction(), 0F);
	assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
	assertTrue(result.isBool());
	assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage);
	JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
 
Example #7
Source File: JsonbHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	this.converter.write(body, null, outputMessage);
	Charset utf8 = StandardCharsets.UTF_8;
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #8
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-15041
public void applicationOctetStreamDefaultContentType() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	ClassPathResource body = Mockito.mock(ClassPathResource.class);
	BDDMockito.given(body.getFilename()).willReturn("spring.dat");
	BDDMockito.given(body.contentLength()).willReturn(12L);
	BDDMockito.given(body.getInputStream()).willReturn(new ByteArrayInputStream("Spring Framework".getBytes()));
	HttpRange range = HttpRange.createByteRange(0, 5);
	ResourceRegion resourceRegion = range.toResourceRegion(body);

	converter.write(Collections.singletonList(resourceRegion), null, outputMessage);

	assertThat(outputMessage.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM));
	assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/12"));
	assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring"));
}
 
Example #9
Source File: MappingJackson2HttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[]{"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[]{0x1, 0x2});
	converter.write(body, null, outputMessage);
	Charset utf8 = Charset.forName("UTF-8");
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":\"AQI=\""));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #10
Source File: RssChannelHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeOtherCharset() throws IOException, SAXException {
	Channel channel = new Channel("rss_2.0");
	channel.setTitle("title");
	channel.setLink("https://example.com");
	channel.setDescription("description");

	String encoding = "ISO-8859-1";
	channel.setEncoding(encoding);

	Item item1 = new Item();
	item1.setTitle("title1");

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(channel, null, outputMessage);

	assertEquals("Invalid content-type", new MediaType("application", "rss+xml", Charset.forName(encoding)),
			outputMessage.getHeaders().getContentType());
}
 
Example #11
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeJsonWithJavaFormat() throws IOException {
	this.converter = new ProtobufHttpMessageConverter(
			new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
			this.extensionRegistry);
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
	this.converter.write(this.testMsg, contentType, outputMessage);

	assertEquals(contentType, outputMessage.getHeaders().getContentType());

	final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertFalse("body is empty", body.isEmpty());

	Msg.Builder builder = Msg.newBuilder();
	JsonFormat.parser().merge(body, builder);
	assertEquals(this.testMsg, builder.build());

	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER));
	assertNull(outputMessage.getHeaders().getFirst(
			ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER));
}
 
Example #12
Source File: StandardMultipartHttpServletRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void multipartFileResource() throws IOException {
	String name = "file";
	String disposition = "form-data; name=\"" + name + "\"; filename=\"myFile.txt\"";
	StandardMultipartHttpServletRequest request = requestWithPart(name, disposition, "myBody");
	MultipartFile multipartFile = request.getFile(name);

	assertNotNull(multipartFile);

	MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
	map.add(name, multipartFile.getResource());

	MockHttpOutputMessage output = new MockHttpOutputMessage();
	new FormHttpMessageConverter().write(map, null, output);

	assertThat(output.getBodyAsString(StandardCharsets.UTF_8), containsString(
			"Content-Disposition: form-data; name=\"file\"; filename=\"myFile.txt\"\r\n" +
					"Content-Type: text/plain\r\n" +
					"Content-Length: 6\r\n" +
					"\r\n" +
					"myBody\r\n"));
}
 
Example #13
Source File: GsonHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeWithBaseType() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	this.converter.write(body, MyBase.class, null, outputMessage);
	Charset utf8 = StandardCharsets.UTF_8;
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #14
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void writeDOMSource() throws Exception {
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
	documentBuilderFactory.setNamespaceAware(true);
	Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
	Element rootElement = document.createElement("root");
	document.appendChild(rootElement);
	rootElement.setTextContent("Hello World");
	DOMSource domSource = new DOMSource(document);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(domSource, null, outputMessage);
	assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
			isSimilarTo("<root>Hello World</root>"));
	assertEquals("Invalid content-type", new MediaType("application", "xml"),
			outputMessage.getHeaders().getContentType());
	assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
			outputMessage.getHeaders().getContentLength());
}
 
Example #15
Source File: MappingJackson2HttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jsonpAndJsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	jacksonValue.setJsonpFunction("callback");
	this.converter.writeInternal(jacksonValue, null, outputMessage);

	String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertThat(result, startsWith("/**/callback("));
	assertThat(result, endsWith(");"));
	assertThat(result, containsString("\"withView1\":\"with\""));
	assertThat(result, not(containsString("\"withView2\":\"with\"")));
	assertThat(result, not(containsString("\"withoutView\":\"without\"")));
}
 
Example #16
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	this.converter.write(jacksonValue, null, outputMessage);

	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertThat(result, containsString("<withView1>with</withView1>"));
	assertThat(result, not(containsString("<withView2>with</withView2>")));
	assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
 
Example #17
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void writeDOMSource() throws Exception {
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
	documentBuilderFactory.setNamespaceAware(true);
	Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
	Element rootElement = document.createElement("root");
	document.appendChild(rootElement);
	rootElement.setTextContent("Hello World");
	DOMSource domSource = new DOMSource(document);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(domSource, null, outputMessage);
	assertXMLEqual("Invalid result", "<root>Hello World</root>",
			outputMessage.getBodyAsString(Charset.forName("UTF-8")));
	assertEquals("Invalid content-type", new MediaType("application", "xml"),
			outputMessage.getHeaders().getContentType());
	assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
			outputMessage.getHeaders().getContentLength());
}
 
Example #18
Source File: GsonHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
	ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
	ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {};

	String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
			"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
	inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

	List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
	assertEquals(1, results.size());
	MyBean result = results.get(0);
	assertEquals("Foo", result.getString());
	assertEquals(42, result.getNumber());
	assertEquals(42F, result.getFraction(), 0F);
	assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
	assertTrue(result.isBool());
	assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
	JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
 
Example #19
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void writeDOMSource() throws Exception {
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
	documentBuilderFactory.setNamespaceAware(true);
	Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
	Element rootElement = document.createElement("root");
	document.appendChild(rootElement);
	rootElement.setTextContent("Hello World");
	DOMSource domSource = new DOMSource(document);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(domSource, null, outputMessage);
	assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
			isSimilarTo("<root>Hello World</root>"));
	assertEquals("Invalid content-type", new MediaType("application", "xml"),
			outputMessage.getHeaders().getContentType());
	assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
			outputMessage.getHeaders().getContentLength());
}
 
Example #20
Source File: GsonHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
	ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
	ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {};

	String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
			"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
	inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

	List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
	assertEquals(1, results.size());
	MyBean result = results.get(0);
	assertEquals("Foo", result.getString());
	assertEquals(42, result.getNumber());
	assertEquals(42F, result.getFraction(), 0F);
	assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
	assertTrue(result.isBool());
	assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
	JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
 
Example #21
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[]{"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[]{0x1, 0x2});
	converter.write(body, null, outputMessage);
	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertTrue(result.contains("<string>Foo</string>"));
	assertTrue(result.contains("<number>42</number>"));
	assertTrue(result.contains("<fraction>42.0</fraction>"));
	assertTrue(result.contains("<array><array>Foo</array><array>Bar</array></array>"));
	assertTrue(result.contains("<bool>true</bool>"));
	assertTrue(result.contains("<bytes>AQI=</bytes>"));
	assertEquals("Invalid content-type", new MediaType("application", "xml", StandardCharsets.UTF_8),
			outputMessage.getHeaders().getContentType());
}
 
Example #22
Source File: GsonHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void readAndWriteGenerics() throws Exception {
	Field beansList = ListHolder.class.getField("listField");

	String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
			"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
	inputMessage.getHeaders().setContentType(new MediaType("application", "json"));

	Type genericType = beansList.getGenericType();
	List<MyBean> results = (List<MyBean>) converter.read(genericType, MyBeanListHolder.class, inputMessage);
	assertEquals(1, results.size());
	MyBean result = results.get(0);
	assertEquals("Foo", result.getString());
	assertEquals(42, result.getNumber());
	assertEquals(42F, result.getFraction(), 0F);
	assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
	assertTrue(result.isBool());
	assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(results, genericType, new MediaType("application", "json"), outputMessage);
	JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
 
Example #23
Source File: MappingJackson2HttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-13318
public void writeSubTypeList() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	List<MyBean> beans = new ArrayList<MyBean>();
	MyBean foo = new MyBean();
	foo.setString("Foo");
	foo.setNumber(42);
	beans.add(foo);
	MyBean bar = new MyBean();
	bar.setString("Bar");
	bar.setNumber(123);
	beans.add(bar);
	ParameterizedTypeReference<List<MyInterface>> typeReference =
			new ParameterizedTypeReference<List<MyInterface>>() {};

	this.converter.writeInternal(beans, typeReference.getType(), outputMessage);

	String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("\"string\":\"Bar\""));
	assertTrue(result.contains("\"number\":123"));
}
 
Example #24
Source File: MappingJackson2HttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void classLevelJsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView3.class);
	this.converter.writeInternal(jacksonValue, null, outputMessage);

	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertThat(result, not(containsString("\"withView1\":\"with\"")));
	assertThat(result, not(containsString("\"withView2\":\"with\"")));
	assertThat(result, containsString("\"withoutView\":\"without\""));
}
 
Example #25
Source File: RssChannelHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void writeOtherCharset() throws IOException, SAXException {
	Channel channel = new Channel("rss_2.0");
	channel.setTitle("title");
	channel.setLink("http://example.com");
	channel.setDescription("description");

	String encoding = "ISO-8859-1";
	channel.setEncoding(encoding);

	Item item1 = new Item();
	item1.setTitle("title1");

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(channel, null, outputMessage);

	assertEquals("Invalid content-type", new MediaType("application", "rss+xml", Charset.forName(encoding)),
			outputMessage.getHeaders().getContentType());
}
 
Example #26
Source File: GsonHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[]{"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[]{0x1, 0x2});
	this.converter.write(body, null, outputMessage);
	Charset utf8 = Charset.forName("UTF-8");
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #27
Source File: GsonHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	this.converter.write(body, null, outputMessage);
	Charset utf8 = StandardCharsets.UTF_8;
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #28
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(contentType, outputMessage.getHeaders().getContentType());
	assertTrue(outputMessage.getBodyAsBytes().length > 0);
	Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
	assertEquals(this.testMsg, result);

	String messageHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
	assertEquals("Msg", messageHeader);
	String schemaHeader =
			outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
	assertEquals("sample.proto", schemaHeader);
}
 
Example #29
Source File: GsonHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void write() throws IOException {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MyBean body = new MyBean();
	body.setString("Foo");
	body.setNumber(42);
	body.setFraction(42F);
	body.setArray(new String[] {"Foo", "Bar"});
	body.setBool(true);
	body.setBytes(new byte[] {0x1, 0x2});
	this.converter.write(body, null, outputMessage);
	Charset utf8 = StandardCharsets.UTF_8;
	String result = outputMessage.getBodyAsString(utf8);
	assertTrue(result.contains("\"string\":\"Foo\""));
	assertTrue(result.contains("\"number\":42"));
	assertTrue(result.contains("fraction\":42.0"));
	assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
	assertTrue(result.contains("\"bool\":true"));
	assertTrue(result.contains("\"bytes\":[1,2]"));
	assertEquals("Invalid content-type", new MediaType("application", "json", utf8),
			outputMessage.getHeaders().getContentType());
}
 
Example #30
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getContentLength() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
	this.converter.write(this.testMsg, contentType, outputMessage);
	assertEquals(-1, outputMessage.getHeaders().getContentLength());
}