Example usage for org.springframework.mock.web MockHttpServletResponse getContentType

List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentType

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getContentType.

Prototype

@Override
    @Nullable
    public String getContentType() 

Source Link

Usage

From source file:com.jayway.restassured.module.mockmvc.ExtractTest.java

@Test
public void can_extract_spring_mvcs_response() {
    MockHttpServletResponse response =

            given().standaloneSetup(new GreetingController()).param("name", "Johan").when().get("/greeting")
                    .then().statusCode(200).body("id", equalTo(1)).extract().response()
                    .mockHttpServletResponse();

    assertThat(response.getContentType()).contains("application/json");
}

From source file:io.restassured.module.mockmvc.ExtractTest.java

@Test
public void can_extract_spring_mvcs_response() {
    MockHttpServletResponse response =

            RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).param("name", "Johan").when()
                    .get("/greeting").then().statusCode(200).body("id", equalTo(1)).extract().response()
                    .mockHttpServletResponse();

    assertThat(response.getContentType()).contains("application/json");
}

From source file:it.geosolutions.httpproxy.service.BaseProxyServiceTest.java

/**
 * Test IProxyService execute as HTTP GET
 *///  w w w. j  av  a 2  s  .c o  m
public void executeGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", testUrl);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("text/xml"));

        LOGGER.info("Success proxy GET in '" + testUrl + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy");
    }
}

From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java

@Test
public void testSvnHandleGetImageAsInline() throws Exception {
    final BaseCommand command = new BaseCommand();
    command.setName(repositoryName);//from www.  j  a v  a2s  . c  o  m
    command.setPath("/testimage.gif");

    final GetFileController ctrl = new GetFileController();
    ctrl.setApplication(application);
    ctrl.setRepositoryService(mock(RepositoryService.class));

    final ConfigurableMimeFileTypeMap mftm = new ConfigurableMimeFileTypeMap();
    mftm.afterPropertiesSet();
    ctrl.setMimeFileTypeMap(mftm);
    final ModelAndView modelAndView;

    request.addParameter(GetFileController.DISPLAY_REQUEST_PARAMETER,
            GetFileController.CONTENT_DISPOSITION_INLINE);

    final MockHttpServletResponse res = new MockHttpServletResponse();
    modelAndView = ctrl.svnHandle(null, command, 100, null, request, res, null);

    assertNull(modelAndView);
    assertEquals("image/gif", res.getContentType());
    assertTrue(((String) res.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("inline"));
}

From source file:it.geosolutions.httpproxy.service.ProxyServiceDefaultTest.java

/**
 * Test IProxyService execute as HTTP GET
 *///from   www .j ava2  s . c om
@Test
public void testExecuteGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", TEST_URL);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("application/vnd.ogc.wms_xml"));

        LOGGER.info("Success proxy GET in '" + TEST_URL + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy-->\t" + e.getLocalizedMessage());
    }
}

From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java

@Test
public void testSvnHandleGetFile() throws Exception {
    final BaseCommand command = new BaseCommand();
    command.setName(repositoryName);/*  w ww  . j  a v  a2s.  c  o  m*/
    command.setPath("/testfile.txt");

    final GetFileController ctrl = new GetFileController();
    ctrl.setApplication(application);
    ctrl.setRepositoryService(mock(RepositoryService.class));

    final ConfigurableMimeFileTypeMap mftm = new ConfigurableMimeFileTypeMap();
    mftm.afterPropertiesSet();
    ctrl.setMimeFileTypeMap(mftm);
    final ModelAndView modelAndView;

    final MockHttpServletResponse res = new MockHttpServletResponse();
    ctrl.setServletContext(mockServletContext);
    modelAndView = ctrl.svnHandle(null, command, 100, null, request, res, null);

    assertNull(modelAndView);
    assertEquals("text/plain", res.getContentType());
    assertTrue(((String) res.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("attachment"));
}

From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java

@Test
public void testSvnHandleGetFileAsAttachment() throws Exception {
    final BaseCommand command = new BaseCommand();
    command.setName(repositoryName);/*from www. ja  v a 2s .c  om*/
    command.setPath("/testimage.gif");

    final GetFileController ctrl = new GetFileController();
    ctrl.setApplication(application);
    ctrl.setRepositoryService(mock(RepositoryService.class));

    request.addParameter(GetFileController.DISPLAY_REQUEST_PARAMETER, (String) null);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final ModelAndView modelAndView = ctrl.svnHandle(null, command, 100, null, request, mockResponse, null);

    assertNull(modelAndView);

    assertEquals(WebUtils.APPLICATION_OCTET_STREAM, mockResponse.getContentType());
    assertTrue(((String) mockResponse.getHeader(WebUtils.CONTENT_DISPOSITION_HEADER)).startsWith("attachment"));
}

From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilderTest.java

@Test
public void testBuilderUploadResponse() throws IOException {

    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    request.setParameter("extAction", "action");
    request.setParameter("extMethod", "method");
    request.setParameter("extType", "type");
    request.setParameter("extTID", "1");

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    ExtDirectResponseBuilder.create(request, servletResponse)
            .addResultProperty("additionalProperty", Boolean.FALSE)
            .addResultProperty("text", "a lot of "text"").buildAndWrite();

    assertThat(servletResponse.getContentType()).isEqualTo("text/html;charset=UTF-8");
    String content = servletResponse.getContentAsString();
    assertThat(servletResponse.getContentLength()).isEqualTo(content.getBytes("UTF-8").length);

    assertThat(content).startsWith("<html><body><textarea>");
    assertThat(content).endsWith("</textarea></body></html>");

    String json = content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1);
    assertThat(json).contains("\\&quot;");
    json = json.replace("\\&quot;", "\'");
    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> header = mapper.readValue(json, Map.class);

    assertThat(header.get("action")).isEqualTo("action");
    assertThat(header.get("method")).isEqualTo("method");
    assertThat(header.get("type")).isEqualTo("type");
    assertThat(header.get("tid")).isEqualTo(1);

    @SuppressWarnings("unchecked")
    Map<String, Object> result = (Map<String, Object>) header.get("result");
    assertThat(result).hasSize(3);//from   w  w  w . j  a v  a 2  s.  c  om
    assertThat((Boolean) result.get("success")).isTrue();
    assertThat(result.get("text")).isEqualTo("a lot of 'text'");
    assertThat(result.get("additionalProperty")).isEqualTo(Boolean.FALSE);
}

From source file:org.geogig.geoserver.rest.GeoGigWebAPIIntegrationTest.java

/** Test for resource {@code /rest/<repository>/repo/manifest} */
@Test//w  w w.j a va  2s .c om
public void testGetManifest() throws Exception {
    final String url = BASE_URL + "/repo/manifest";
    MockHttpServletResponse sr = getAsServletResponse(url);
    assertEquals(200, sr.getStatus());

    String contentType = sr.getContentType();
    assertTrue(contentType, sr.getContentType().startsWith("text/plain"));

    String responseBody = sr.getContentAsString();
    assertNotNull(responseBody);
    assertTrue(responseBody, responseBody.startsWith("HEAD refs/heads/master"));
}

From source file:cherry.foundation.download.TableDownloadTemplateTest.java

@Test
public void testDownloadXlsNoHeader() throws InvalidFormatException, IOException {
    // /*from  w w  w  .ja  v a  2s. com*/
    MockHttpServletResponse response = new MockHttpServletResponse();
    // 
    tableDownloadOperation.downloadXls(response, "test_{0}.xls", new LocalDateTime(2015, 1, 23, 12, 34, 56),
            null, createCommonClause(), createOrderByClause(), constant("TEST00"));
    // 
    assertEquals("application/vnd.ms-excel", response.getContentType());
    assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type"));
    assertEquals("attachment; filename=\"test_20150123123456.xls\"", response.getHeader("Content-Disposition"));
    try (InputStream in = new ByteArrayInputStream(response.getContentAsByteArray());
            Workbook workbook = WorkbookFactory.create(in);
            ExcelReader reader = new ExcelReader(workbook)) {
        String[] record;
        record = reader.read();
        assertEquals(1, record.length);
        assertEquals("TEST00", record[0]);
        assertNull(reader.read());
    }
}