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:ch.ralscha.extdirectspring.util.ExtDirectSpringUtilTest.java

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

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("If-None-Match", etag);
    MockHttpServletResponse response = new MockHttpServletResponse();

    ExtDirectSpringUtil.handleCacheableResponse(request, response, data, contentType);
    assertThat(response.getStatus()).isEqualTo(304);

    request = new MockHttpServletRequest();
    request.addHeader("If-None-Match", etag);
    response = new MockHttpServletResponse();
    data = "new response data".getBytes();
    etag = "\"0" + DigestUtils.md5DigestAsHex(data) + '"';
    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.jasig.cas.support.oauth.web.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoCode() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*  w ww .  j av a 2  s  .  co m*/
    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.CODE).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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoClientId() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//  w w  w. j a va 2  s . c  o m
    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.CLIENT_ID).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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoClientSecret() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//  w w  w  . ja  va 2 s . c  o  m
    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.CLIENT_SECRET).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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoRedirectUri() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*ww  w.jav a 2  s .c  o  m*/
    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.REDIRECT_URI).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.wrml.server.WrmlServletTest.java

@Test
public void requestWithBadHostHeader() throws ServletException, IOException {

    MockHttpServletRequest request = new MockHttpServletRequest();
    initMockHttpRequest(request, CAPRICA_SIX_ENDPOINT);
    request.setMethod(Method.Get.getProtocolGivenName());
    //request.addHeader(HttpHeaders.ACCEPT, JSON_MEDIA_TYPE);
    request.addHeader(WrmlServlet.WRML_HOST_HEADER_NAME, BAD_HOST_1);

    MockHttpServletResponse response = new MockHttpServletResponse();

    initMockWrmlRequest(request, Method.Get, CAPRICA_SIX_ENDPOINT, CAPRICA_SCHEMA_URI);

    _Servlet.service(request, response);

    // Verify Model Write
    Assert.assertTrue(response.getContentType().contains("text/plain"));
    Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
    Assert.assertTrue(response.getContentLength() > 0);
    Assert.assertTrue(response.getContentAsString().contains("moose::/squirrel:"));
}

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

@Test
public void verifyNoAccessToken() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*from  ww  w.  jav  a2  s  .com*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN
            + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_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:org.jasig.cas.support.oauth.web.OAuth20ProfileControllerTests.java

@Test
public void verifyNoTokenAndAuthHeaderIsMalformed() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.addHeader("Authorization", "Let me in i am authorized");
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

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

    final String expected = "{\"error\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN
            + "\",\"error_description\":\"" + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}";

    final ObjectMapper mapper = new ObjectMapper();
    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.wrml.server.WrmlServletTest.java

@Test
public void requestWithoutEntityButWithContentType() throws ServletException, IOException {

    MockHttpServletRequest request = new MockHttpServletRequest();
    initMockHttpRequest(request, DOCROOT_ENDPOINT);
    request.setMethod(Method.Save.getProtocolGivenName());
    request.setContentType(DEFAULT_CONTENT_TYPE);
    request.setContent(new byte[] {});

    MockHttpServletResponse response = new MockHttpServletResponse();

    initMockWrmlRequest(request, Method.Save, DOCROOT_ENDPOINT, CAPRICA_SCHEMA_URI);

    _Servlet.service(request, response);

    // Verify Model Write
    Assert.assertEquals(DEFAULT_CONTENT_TYPE, response.getContentType());
    Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    Assert.assertEquals(response.getContentAsByteArray().length, response.getContentLength());
}

From source file:org.wrml.server.WrmlServletTest.java

@Test
public void requestPostNoData() throws ServletException, IOException {

    MockHttpServletRequest request = new MockHttpServletRequest();

    initMockHttpRequest(request, DOCROOT_ENDPOINT);
    request.setMethod(Method.Save.getProtocolGivenName());
    request.setContentType(DEFAULT_CONTENT_TYPE);
    request.setContent(new byte[] {});

    MockHttpServletResponse response = new MockHttpServletResponse();

    initMockWrmlRequest(request, Method.Save, DOCROOT_ENDPOINT, CAPRICA_SCHEMA_URI);

    _Servlet.service(request, response);

    // Verify Model Write
    Assert.assertEquals(DEFAULT_CONTENT_TYPE, response.getContentType());
    Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    Assert.assertEquals(response.getContentAsByteArray().length, response.getContentLength());
}