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:org.jasig.cas.support.oauth.web.OAuth20WrapperControllerTests.java

@Test
public void verifyNoGrantTypeForTokenCtrls() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.handleRequest(mockRequest, mockResponse);

    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("application/json", mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + new InvalidParameterException(OAuthConstants.GRANT_TYPE).getMessage() + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

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

@Test
public void testDownloadCsvNoHeader() throws IOException {
    // /*from  w  ww  . ja  v a  2s  .c om*/
    MockHttpServletResponse response = new MockHttpServletResponse();
    // 
    tableDownloadOperation.downloadCsv(response, StandardCharsets.UTF_8, "test_{0}.csv",
            new LocalDateTime(2015, 1, 23, 12, 34, 56), null, createCommonClause(), createOrderByClause(),
            constant("TEST00"));
    // 
    assertEquals("text/csv", response.getContentType());
    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("text/csv;charset=UTF-8", response.getHeader("Content-Type"));
    assertEquals("attachment; filename=\"test_20150123123456.csv\"", response.getHeader("Content-Disposition"));
    assertEquals("\"TEST00\"\r\n", response.getContentAsString());
}

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

@Test
public void testDownloadCsvWithHeader() throws IOException {
    // // ww  w. j av  a2 s .c  o m
    MockHttpServletResponse response = new MockHttpServletResponse();
    // 
    tableDownloadOperation.downloadCsv(response, StandardCharsets.UTF_8, "test_{0}.csv",
            new LocalDateTime(2015, 1, 23, 12, 34, 56), asList("HEAD0"), createCommonClause(),
            createOrderByClause(), constant("TEST00"));
    // 
    assertEquals("text/csv", response.getContentType());
    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("text/csv;charset=UTF-8", response.getHeader("Content-Type"));
    assertEquals("attachment; filename=\"test_20150123123456.csv\"", response.getHeader("Content-Disposition"));
    assertEquals("\"HEAD0\"\r\n\"TEST00\"\r\n", response.getContentAsString());
}

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

@Test
public void testDownloadXlsxWithHeader() throws InvalidFormatException, IOException {
    // //ww  w .  ja v a  2 s .c o m
    MockHttpServletResponse response = new MockHttpServletResponse();
    // 
    tableDownloadOperation.downloadXls(response, "test_{0}.xlsx", new LocalDateTime(2015, 1, 23, 12, 34, 56),
            asList("HEAD0"), createCommonClause(), createOrderByClause(), constant("TEST00"),
            constantAs("TEST01", path(String.class, "head1")));
    // 
    assertEquals("application/vnd.ms-excel", response.getContentType());
    assertEquals("application/vnd.ms-excel", response.getHeader("Content-Type"));
    assertEquals("attachment; filename=\"test_20150123123456.xlsx\"",
            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(2, record.length);
        assertEquals("HEAD0", record[0]);
        assertEquals("HEAD1", record[1]);
        record = reader.read();
        assertEquals(2, record.length);
        assertEquals("TEST00", record[0]);
        assertEquals("TEST01", record[1]);
        assertNull(reader.read());
    }
}

From source file:org.jasig.cas.support.oauth.web.OAuth20WrapperControllerTests.java

@Test
public void verifyInvalidGrantTypeForTokenCtrls() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, "banana");

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.handleRequest(mockRequest, mockResponse);

    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("application/json", mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + new InvalidParameterException(OAuthConstants.GRANT_TYPE).getMessage() + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeTokenControllerTests.java

@Test
public void verifyOK() throws Exception {
    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(TOKEN_ID);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(TOKEN_ID)).thenReturn(accessToken);
    when(centralOAuthService.revokeToken(accessToken)).thenReturn(true);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.REVOKE_URL);
    mockRequest.setParameter(OAuthConstants.TOKEN, TOKEN_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//from  w ww . jav a  2  s  .  c  om
    assertEquals(HttpStatus.SC_NO_CONTENT, mockResponse.getStatus());
    assertNull(mockResponse.getContentType());
    assertEquals("null", mockResponse.getContentAsString());
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeTokenControllerTests.java

@Test
public void verifyNoToken() throws Exception {
    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(null)).thenThrow(new InvalidTokenException("error"));

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.REVOKE_URL);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//  ww w.  j  a v  a  2  s . c o m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST
            + "\",\"error_description\":\"Invalid Token\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeTokenControllerTests.java

@Test
public void verifyRevocationFail() throws Exception {
    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(TOKEN_ID);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(TOKEN_ID)).thenReturn(accessToken);
    when(centralOAuthService.revokeToken(accessToken)).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.REVOKE_URL);
    mockRequest.setParameter(OAuthConstants.TOKEN, TOKEN_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//from   w  w w  .  j  a  v a 2s  .co m
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.FAILED_TOKEN_REVOCATION_DESCRIPTION + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtilTest.java

@Test
public void testHandleCacheableResponseWithoutIfNoneMatch() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    byte[] data = "the response data".getBytes();
    String etag = "\"0" + DigestUtils.md5DigestAsHex(data) + '"';
    String contentType = "application/javascript;charset=UTF-8";
    ExtDirectSpringUtil.handleCacheableResponse(request, response, data, contentType);

    assertThat(response.getStatus()).isEqualTo(200);
    assertResponse(response, 5, etag, 6);
    assertThat(response.getContentLength()).isEqualTo(data.length);
    assertThat(response.getContentType()).isEqualTo(contentType);
    assertThat(response.getContentAsByteArray()).isEqualTo(data);
}

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

private void testGetRemoteObject(ObjectId oid) throws Exception {
    GeoGIG geogig = geogigData.getGeogig();

    final String resource = BASE_URL + "/repo/objects/";
    final String url = resource + oid.toString();

    MockHttpServletResponse servletResponse;
    InputStream responseStream;/*from w  w w.  j  av  a2 s .c  o  m*/

    servletResponse = getAsServletResponse(url);
    assertEquals(200, servletResponse.getStatus());

    String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
    assertEquals(contentType, servletResponse.getContentType());

    responseStream = getBinaryInputStream(servletResponse);

    ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;

    RevObject actual = factory.read(oid, responseStream);
    RevObject expected = geogig.command(RevObjectParse.class).setObjectId(oid).call().get();
    assertEquals(expected, actual);
}