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.wrml.server.WrmlServletTest.java

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

    MockHttpServletRequest request = new MockHttpServletRequest();
    initMockHttpRequest(request, CAPRICA_SIX_SPOOF_2_ENDPOINT);
    request.setMethod(Method.Get.getProtocolGivenName());

    MockHttpServletResponse response = new MockHttpServletResponse();

    initMockWrmlRequest(request, Method.Get, CAPRICA_SIX_SPOOF_2_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());
}

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

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

    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);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    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 va2 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\":\""
            + OAuthConstants.INVALID_CODE_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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyNoRegisteredService() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(null);

    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);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    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 . jav  a2 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\":\""
            + OAuthConstants.INVALID_CLIENT_ID_OR_SECRET_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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyWrongSecret() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, CLIENT_SECRET);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(service);

    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, WRONG_CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/* w  w w  . j  a va 2 s .  c om*/
    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\":\""
            + OAuthConstants.INVALID_CLIENT_ID_OR_SECRET_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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyRedirectUriDoesNotStartWithServiceId() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);

    final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, CLIENT_SECRET);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(service);

    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);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, OTHER_REDIRECT_URI);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);//www.j a v  a2  s .c  om
    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\":\""
            + OAuthConstants.INVALID_REDIRECT_URI_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.OAuth20TokenAuthorizationCodeControllerTests.java

@Test
public void verifyInvalidGrantType() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);
    when(authorizationCode.getType()).thenReturn(TokenType.PERSONAL);

    final OAuthRegisteredService service = getRegisteredService(REDIRECT_URI, CLIENT_SECRET);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(CODE, AuthorizationCode.class)).thenReturn(authorizationCode);
    when(centralOAuthService.getRegisteredService(CLIENT_ID)).thenReturn(service);

    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);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);

    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 ww  w .  j a  va2s .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_GRANT + "\",\"error_description\":\""
            + OAuthConstants.INVALID_GRANT_TYPE_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.eclipse.virgo.apps.repository.web.RepositoryControllerTests.java

@Test
public void getIndex() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setRequestURI("http://localhost:8080/org.eclipse.virgo.server.repository/my-repo");
    request.setMethod("GET");

    byte[] indexBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    RepositoryIndex repositoryIndex = createMock(RepositoryIndex.class);
    expect(repositoryIndex.getInputStream()).andReturn(new ByteArrayInputStream(indexBytes)).anyTimes();
    expect(repositoryIndex.getETag()).andReturn("123456789").anyTimes();
    expect(repositoryIndex.getLength()).andReturn(indexBytes.length).anyTimes();

    expect(this.repositoryManager.getIndex("my-repo")).andReturn(repositoryIndex);

    replay(this.repositoryManager, repositoryIndex);

    repositoryController.getIndex(request, response);

    verify(this.repositoryManager, repositoryIndex);

    assertEquals("application/org.eclipse.virgo.repository.Index", response.getContentType());
    assertArrayEquals(indexBytes, response.getContentAsByteArray());
}

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

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

    MockHttpServletRequest request = new MockHttpServletRequest();
    initMockHttpRequest(request, DOCROOT_SLASH_ENDPOINT);
    request.setMethod(Method.Get.getProtocolGivenName());

    MockHttpServletResponse response = new MockHttpServletResponse();

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

    _Servlet.service(request, response);

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

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

@Test
public void verifyOKWithPersonalToken() throws Exception {
    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);/* w  w  w. j  a  v a2s  . c o m*/
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(ID);
    when(principal.getAttributes()).thenReturn(map);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final Service service = new SimpleWebApplicationServiceImpl("id");

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getType()).thenReturn(TokenType.PERSONAL);
    when(accessToken.getService()).thenReturn(service);
    when(accessToken.getTicketGrantingTicket()).thenReturn(ticketGrantingTicket);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getToken(AT_ID, AccessToken.class)).thenReturn(accessToken);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.PROFILE_URL);
    mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, AT_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);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final ObjectMapper mapper = new ObjectMapper();

    final String expected = "{\"id\":\"" + ID + "\"}";
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());
}

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

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

    MockHttpServletRequest request = new MockHttpServletRequest();
    initMockHttpRequest(request, CAPRICA_SIX_SPOOF_1_ENDPOINT);
    request.setMethod(Method.Get.getProtocolGivenName());
    request.addHeader(WrmlServlet.WRML_HOST_HEADER_NAME, LOCALHOST);

    MockHttpServletResponse response = new MockHttpServletResponse();

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

    _Servlet.service(request, response);

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