List of usage examples for org.springframework.http MediaType toString
@Override
public String toString()
From source file:io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.java
private void setContentTypeToApplicationFormUrlEncoded(MockHttpServletRequestBuilder request) { String contentType = APPLICATION_FORM_URLENCODED_VALUE; EncoderConfig encoderConfig = config.getEncoderConfig(); if (encoderConfig.shouldAppendDefaultContentCharsetToContentTypeIfUndefined()) { contentType += "; charset="; if (encoderConfig.hasDefaultCharsetForContentType(contentType)) { contentType += encoderConfig.defaultCharsetForContentType(contentType); } else {/*w w w . j ava2 s .c om*/ contentType += encoderConfig.defaultContentCharset(); } } MediaType mediaType = MediaType.parseMediaType(contentType); request.contentType(mediaType); List<Header> newHeaders = new ArrayList<Header>(headers.asList()); newHeaders.add(new Header(CONTENT_TYPE, mediaType.toString())); headers = new Headers(newHeaders); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModel() throws Exception { // TODO: Make this test more robust // Setup//w ww.j av a 2s.c om String encoding = "ISO-8859-1"; // Default encoding JSOG expected = new JSOG("foobar"); MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expected); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelCustomEncodingString() throws Exception { // TODO: Make this test more robust // Setup//from w ww. j a v a 2s. c o m String encoding = "UTF-8"; JSOG expected = new JSOG("foobar"); MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expected); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.setEncoding(encoding); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelCustomEncodingCharset() throws Exception { // TODO: Make this test more robust // Setup/* ww w.j av a2s . c o m*/ String encoding = "UTF-8"; JSOG expected = new JSOG("foobar"); MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expected); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.setEncoding(Charset.forName(encoding)); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelBean() throws Exception { // Setup// w w w. jav a2 s .c om String encoding = "ISO-8859-1"; // Default encoding JSOG expected = JSOG.object("bean", JSOG.object().put("foo", "foovalue").put("bar", "barvalue")); MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("bean", new TestBean()); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelJSONP() throws Exception { // TODO: Make this test more robust // Setup//from w w w.j a v a 2s .c o m String encoding = "ISO-8859-1"; // Default encoding String callback = "foo"; JSOG expectedJson = new JSOG("foobar"); String expected = callback + "(" + expectedJson + ")"; MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expectedJson); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(callback); // Execution replay(request, response, sos); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value String actual = new String(out.getValue(), encoding); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelJSONPCustomCallback() throws Exception { // TODO: Make this test more robust // Setup// w w w . j av a2 s . c o m String encoding = "ISO-8859-1"; // Default encoding String callback = "foo"; String callbackParamName = "bar"; JSOG expectedJson = new JSOG("foobar"); String expected = callback + "(" + expectedJson + ")"; MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expectedJson); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter(callbackParamName)).andReturn(callback); // Execution replay(request, response, sos); instance.setJsonpCallbackParam(callbackParamName); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value String actual = new String(out.getValue(), encoding); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelCustomContentType() throws Exception { // Setup//from w ww . j a va2 s. c o m String encoding = "ISO-8859-1"; // Default encoding JSOG expected = new JSOG("foobar"); MediaType contentType = MediaType.TEXT_PLAIN; instance.setOutputContentType(contentType); // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("JSOG", expected); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
/** * This tests that complex models can be rendered properly. * A complex model is one that doesn't have "JSOG" as it's only key (excepting BindingResult values). * @throws Exception// ww w.j a va2s.c om */ @Test public void testRenderMergedOutputModelComplex() throws Exception { // TODO: Make this test more robust // Setup String encoding = "ISO-8859-1"; // Default encoding JSOG expected = JSOG.object("foo", "foovalue").put("bar", "barvalue").put("obj", JSOG.object()); MediaType contentType = MediaType.APPLICATION_JSON; // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("foo", "foovalue"); model.put("bar", "barvalue"); model.put("obj", JSOG.object()); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }
From source file:net.sf.jsog.spring.JsogViewTest.java
@Test public void testRenderMergedOutputModelBeanCustomBeanJsogFactory() throws Exception { // Setup//from www . j av a 2s . c om String encoding = "ISO-8859-1"; // Default encoding JSOG beanJsog = JSOG.object("foo", "foovalue").put("bar", "barvalue"); JSOG expected = JSOG.object("bean", beanJsog); MediaType contentType = MediaType.APPLICATION_JSON; BeanJsogFactory bjf = createMock(BeanJsogFactory.class); instance.setBeanJsogFactory(bjf); expect(bjf.create(isA(TestBean.class))).andReturn(beanJsog); // Setup the model Map<String, Object> model = new HashMap<String, Object>(); model.put("bean", new TestBean()); // Setup the output stream ServletOutputStream sos = createMock(ServletOutputStream.class); expect(response.getOutputStream()).andReturn(sos); Capture<byte[]> out = new Capture<byte[]>(); sos.write(capture(out)); expectLastCall(); sos.flush(); expectLastCall(); sos.close(); expectLastCall(); response.setContentType(contentType.toString()); expectLastCall(); response.setCharacterEncoding(encoding); expectLastCall(); response.setContentLength(expected.toString().getBytes(encoding).length); expectLastCall(); expect(request.getParameter("callback")).andReturn(null); // Execution replay(request, response, sos, bjf); instance.renderMergedOutputModel(model, request, response); // Verification verify(request, response, sos, bjf); assertTrue(out.hasCaptured()); // Parse the resulting value JSOG actual = JSOG.parse(new String(out.getValue(), encoding)); assertEquals(actual, expected); }