Example usage for org.springframework.mock.web MockHttpServletRequest setContentType

List of usage examples for org.springframework.mock.web MockHttpServletRequest setContentType

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setContentType.

Prototype

public void setContentType(@Nullable String contentType) 

Source Link

Usage

From source file:org.osaf.cosmo.cmp.CmpPutTest.java

/**
 *///from w ww . 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.osaf.cosmo.dav.impl.StandardDavRequestTest.java

public void testEmptyPropfindBody() throws Exception {
    // empty propfind body => allprop request

    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.setContentType("text/xml");
    StandardDavRequest request = new StandardDavRequest(httpRequest, testHelper.getResourceLocatorFactory(),
            testHelper.getEntityFactory());

    assertEquals("propfind type not allprop", PROPFIND_ALL_PROP, request.getPropFindType());
    assertTrue("propnames not empty", request.getPropFindProperties().isEmpty());
}

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 w ww.j a  va  2 s  .  c  o  m*/
    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.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void requestReplyViewExpressionString() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//  w w w . j a v a  2s.  co m
        protected Message<String> handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();
    controller.start();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    request.setContentType("text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("baz", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("foo", reply);
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void requestReplyViewExpressionView() throws Exception {
    final View view = mock(View.class);
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//from  www .  j av a 2 s. c  om
        protected Message<String> handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("foo").setHeader("bar", view).build();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();
    controller.start();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    request.setContentType("text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertSame(view, modelAndView.getView());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("foo", reply);
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void testSendWithError() throws Exception {
    QueueChannel requestChannel = new QueueChannel() {
        @Override//w  w w .  j a v a  2  s .  c  om
        protected boolean doSend(Message<?> message, long timeout) {
            throw new RuntimeException("Planned");
        }
    };
    HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.afterPropertiesSet();
    controller.start();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    request.setContentType("text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals(1, modelAndView.getModel().size());
    Errors errors = (Errors) modelAndView.getModel().get("errors");
    assertEquals(1, errors.getErrorCount());
    ObjectError error = errors.getAllErrors().get(0);
    assertEquals(3, error.getArguments().length);
    assertTrue("Wrong message: " + error,
            ((String) error.getArguments()[1]).startsWith("failed to send Message"));
}

From source file:org.unitedinternet.cosmo.dav.impl.StandardDavRequestTest.java

/**
 * Tests empty prop find body.//from  w w w .  j  av a  2  s  . c  o m
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testEmptyPropfindBody() throws Exception {
    // empty propfind body => allprop request

    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.setContentType("text/xml");
    StandardDavRequest request = new StandardDavRequest(httpRequest, testHelper.getResourceLocatorFactory(),
            testHelper.getEntityFactory());

    Assert.assertEquals("propfind type not allprop", PROPFIND_ALL_PROP, request.getPropFindType());
    Assert.assertTrue("propnames not empty", request.getPropFindProperties().isEmpty());
}

From source file:ren.hankai.cordwood.web.support.MultiReadHttpServletRequestTest.java

@Test
public void testGetInputStream() throws Exception {
    final String requestBody = "{\"name\": \"\"}";

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    request.setContent(requestBody.getBytes("UTF-8"));

    final MultiReadHttpServletRequest wr = new MultiReadHttpServletRequest(request);
    final InputStream inputStream = wr.getInputStream();
    Assert.assertNotNull(inputStream);// w ww  . java2 s  .c  o  m
    ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, byteOutStream);
    Assert.assertEquals(requestBody, byteOutStream.toString("UTF-8"));
    Assert.assertTrue(inputStream instanceof CachedServletInputStream);
    final CachedServletInputStream cis = (CachedServletInputStream) inputStream;
    Assert.assertTrue(cis.isFinished());
    Assert.assertTrue(cis.isReady());
    cis.setReadListener(null);// ??

    /* ? */
    // ?
    final BufferedReader reader = wr.getReader();
    final StringWriter writer = new StringWriter();
    IOUtils.copy(reader, writer);
    Assert.assertEquals(requestBody, byteOutStream.toString("UTF-8"));

    // ?
    byteOutStream = new ByteArrayOutputStream();
    IOUtils.copy(wr.getInputStream(), byteOutStream);
    Assert.assertEquals(requestBody, byteOutStream.toString("UTF-8"));
}