List of usage examples for org.springframework.mock.web MockHttpServletRequest setContent
public void setContent(@Nullable byte[] content)
From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java
protected void putXmlObject(final JAXBContext context, final String url, final int expectedStatus, final Object object, final String expectedUrlSuffix) throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final Marshaller marshaller = context.createMarshaller(); marshaller.marshal(object, out);//from w ww. ja v a 2 s . c om final byte[] content = out.toByteArray(); final MockHttpServletRequest request = createRequest(PUT, url); request.setContentType(MediaType.APPLICATION_XML); request.setContent(content); final MockHttpServletResponse response = createResponse(); dispatch(request, response); assertEquals(expectedStatus, response.getStatus()); final String location = response.getHeader("Location").toString(); assertTrue("location '" + location + "' should end with '" + expectedUrlSuffix + "'", location.endsWith(expectedUrlSuffix)); }
From source file:org.openo.nfvo.jujuvnfmadapter.service.rest.fullstack.TestHelper.java
@SuppressWarnings("deprecation") public static MockHttpServletRequest buildMockRequest(File file) throws FileNotFoundException, IOException { String contnet = IOUtils.toString(new FileInputStream(file)); byte[] content = contnet.getBytes(); MockHttpServletRequest context = new MockHttpServletRequest(); context.setContentType("application/json"); context.setContent(content); return context; }
From source file:org.openo.nfvo.jujuvnfmadapter.service.rest.fullstack.TestHelper.java
public static MockHttpServletRequest buildMockRequest(String content) throws FileNotFoundException, IOException { MockHttpServletRequest context = new MockHttpServletRequest(); context.setContentType("application/json"); context.setContent(content.getBytes()); return context; }
From source file:org.openo.nfvo.jujuvnfmadapter.service.rest.fullstack.TestHelper.java
public static MockHttpServletRequest buildDefaultRequest(String fileName) throws FileNotFoundException, IOException { MockHttpServletRequest context = new MockHttpServletRequest(); context.setContentType("application/json"); if (fileName != null) { String file = FileUtils.getClassPath() + File.separator + fileName; InputStream input = new FileInputStream(file); @SuppressWarnings("deprecation") String contnet = IOUtils.toString(input); byte[] content = contnet.getBytes(); context.setContent(content); }//from w ww . j a va 2 s . co m return context; }
From source file:org.osaf.cosmo.BaseMockServletTestCase.java
/** *///w w w . ja va 2 s . c o m protected void sendXmlRequest(MockHttpServletRequest request, Document doc) throws Exception { OutputFormat format = new OutputFormat("xml", "UTF-8", true); ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.setNamespaces(true); serializer.asDOMSerializer().serialize(doc); request.setContentType("text/xml"); request.setCharacterEncoding("UTF-8"); // log.debug("content: " + new String(out.toByteArray())); request.setContent(out.toByteArray()); ; }
From source file:org.osaf.cosmo.cmp.CmpPutTest.java
/** *//* w w w .ja v a 2 s . c o m*/ public void testPutContentTypePrecondition() throws Exception { MockHttpServletRequest request = createMockRequest("PUT", "/deadbeef"); // add content but with the wrong content type request.setContentType("application/octet-stream"); request.setContent("deadbeef".getBytes()); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("incorrect status", MockHttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus()); }
From source file:org.osaf.cosmo.cmp.CmpPutTest.java
/** *///from w w w . j av a2 s .c o m public void testPutContentHeaderPrecondition() throws Exception { MockHttpServletRequest request = createMockRequest("PUT", "/deadbeef"); // add real content but also Content-Encoding header // that is not allowed request.setContentType("text/xml"); request.setContent("deadbeef".getBytes()); request.addHeader("Content-Encoding", "my-encoding"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("incorrect status", MockHttpServletResponse.SC_NOT_IMPLEMENTED, response.getStatus()); }
From source file:org.osaf.cosmo.cmp.CmpPutTest.java
/** *//*from w w w . j a v a 2 s . c o m*/ public void testPutBadCommand() throws Exception { MockHttpServletRequest request = createMockRequest("PUT", "/deadbeef"); // add some content so that put preconditions are met request.setContentType("text/xml"); request.setContent("deadbeef".getBytes()); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("incorrect status", MockHttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
From source file:org.pentaho.test.platform.web.ui.servlet.MondrianCatalogPublisherTests.java
protected MockHttpServletResponse simulateRequest(Map<String, String> map) throws Exception { // prepare request MockHttpServletRequest request = new MockHttpServletRequest(); request.setContentType("multipart/form-data; boundary=---1234"); //$NON-NLS-1$ request.setCharacterEncoding("UTF-8"); //$NON-NLS-1$ String mondrianSchemaFile = map.get("mondrianSchemaFile"); if (logger.isDebugEnabled()) { logger.debug("uploading mondrian schema file named \"" + mondrianSchemaFile + "\""); }//from ww w . ja va2 s .c om String content = MessageFormat.format(DEFAULT_CONTENT_TEMPLATE, mondrianSchemaFile, DEFAULT_FILE_CONTENT); if (logger.isDebugEnabled()) { logger.debug("content=" + content); //$NON-NLS-1$ } request.setContent(content.getBytes("UTF-8")); //$NON-NLS-1$ request.addParameter("publishPath", map.get("publishPath")); //$NON-NLS-1$ //$NON-NLS-2$ request.addParameter("publishKey", map.get("publishKey")); //$NON-NLS-1$ //$NON-NLS-2$ request.addParameter("overwrite", map.get("overwrite")); //$NON-NLS-1$ //$NON-NLS-2$ request.addParameter("jndiName", map.get("jndiName")); //$NON-NLS-1$ //$NON-NLS-2$ MockHttpSession httpSession = new MockHttpSession(); httpSession.setAttribute("pentaho-session", pentahoSession); //$NON-NLS-1$ request.setSession(httpSession); // prepare response MockHttpServletResponse response = new MockHttpServletResponse(); // prepare mondrian catalog service MondrianCatalogHelper catService = new MondrianCatalogHelper(); catService.setDataSourcesConfig("file:" + destFile.getAbsolutePath()); //$NON-NLS-1$ // catService.afterPropertiesSet(); // prepare mondrian catalog publisher MondrianCatalogPublisher pub = new MondrianCatalogPublisher(); pub.setMondrianCatalogService(catService); pub.setFullyQualifiedServerURL("http://localhost:8080/pentaho"); //$NON-NLS-1$ // pub.afterPropertiesSet(); // process request // TODO We need to figure out how to test this . doGet is a protected method now //pub.doGet(request, response); // assertions response.getWriter().flush(); String responseContent = response.getContentAsString(); if (logger.isDebugEnabled()) { logger.debug("response=" + responseContent); //$NON-NLS-1$ } return response; }
From source file:org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilterTests.java
@Test public void getRequestBody() throws Exception { setupContext();//w ww .ja v a2s . c o m MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); request.setContent("{1}".getBytes()); request.addHeader("singleName", "singleValue"); request.addHeader("multiName", "multiValue1"); request.addHeader("multiName", "multiValue2"); RequestContext.getCurrentContext().setRequest(request); InputStream inputStream = getFilter().getRequestBody(request); assertTrue(Arrays.equals("{1}".getBytes(), copyToByteArray(inputStream))); }