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

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

Introduction

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

Prototype

public String getContentAsString() throws UnsupportedEncodingException 

Source Link

Document

Get the content of the response body as a String , using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type .

Usage

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

@Test
public void verifyNoGetForRevokeCtrls() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.REVOKE_URL);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("text/plain", mockResponse.getContentType());
    assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}

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

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

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("text/plain", mockResponse.getContentType());
    assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}

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

@Test
public void verifyNoGetForProfileCtrl() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.METADATA_URL);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

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

    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals("text/plain", mockResponse.getContentType());
    assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
}

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

@Test
public void verifyOK() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL);

    final WebApplicationService webApplicationService = mock(WebApplicationService.class);
    when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID);

    final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class);
    when(argumentExtractor.extractService(mockRequest)).thenReturn(webApplicationService);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);
    when(serviceTicket.getService()).thenReturn(webApplicationService);

    final Assertion assertion = mock(Assertion.class);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(serviceTicket);
    when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService))
            .thenReturn(assertion);//from  ww w. j a  v  a 2 s .  c o m

    final Set<String> scopes = new HashSet<>();
    scopes.add(SCOPE1);
    scopes.add(SCOPE2);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getScopes()).thenReturn(scopes);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService))
            .thenReturn(accessToken);

    final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);

    final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController();
    oauth20ServiceValidateController.initApplicationContext(webApplicationContext);
    oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor);
    oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService);
    oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals("", mockResponse.getContentAsString());

    final Map<String, Object> model = modelAndView.getModel();
    assertEquals(AT_ID, model.get(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN));
    assertEquals(scopes, model.get(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE));
}

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

@Test
public void verifyBypassCASWithNoService() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL);

    final WebApplicationService webApplicationService = mock(WebApplicationService.class);
    when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID);

    final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class);
    when(argumentExtractor.extractService(mockRequest)).thenReturn(null);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);
    when(serviceTicket.getService()).thenReturn(webApplicationService);

    final Assertion assertion = mock(Assertion.class);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(serviceTicket);
    when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService))
            .thenReturn(assertion);//from  ww w  . ja v  a2 s  .c om

    final Set<String> scopes = new HashSet<>();
    scopes.add(SCOPE1);
    scopes.add(SCOPE2);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getScopes()).thenReturn(scopes);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService))
            .thenReturn(accessToken);

    final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);

    final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController();
    oauth20ServiceValidateController.initApplicationContext(webApplicationContext);
    oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor);
    oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService);
    oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals("", mockResponse.getContentAsString());

    final Map<String, Object> model = modelAndView.getModel();
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN));
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE));
}

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

@Test
public void verifyBypassCASWithNoServiceTicketWrongSuccessView() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL);

    final WebApplicationService webApplicationService = mock(WebApplicationService.class);
    when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID);

    final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class);
    when(argumentExtractor.extractService(mockRequest)).thenReturn(webApplicationService);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);
    when(serviceTicket.getService()).thenReturn(webApplicationService);

    final Assertion assertion = mock(Assertion.class);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(null);
    when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService))
            .thenReturn(assertion);//  www  .jav a  2  s .c o  m

    final Set<String> scopes = new HashSet<>();
    scopes.add(SCOPE1);
    scopes.add(SCOPE2);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getScopes()).thenReturn(scopes);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService))
            .thenReturn(accessToken);

    final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);

    final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController();
    oauth20ServiceValidateController.initApplicationContext(webApplicationContext);
    oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor);
    oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService);
    oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals("", mockResponse.getContentAsString());

    final Map<String, Object> model = modelAndView.getModel();
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN));
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE));
}

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

@Test
public void verifyBypassCASWithWrongSuccessView() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL);

    final WebApplicationService webApplicationService = mock(WebApplicationService.class);
    when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID);

    final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class);
    when(argumentExtractor.extractService(mockRequest)).thenReturn(webApplicationService);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);
    when(serviceTicket.getService()).thenReturn(webApplicationService);

    final Assertion assertion = mock(Assertion.class);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);
    when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(serviceTicket);
    when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService))
            .thenThrow(new TicketCreationException());

    final Set<String> scopes = new HashSet<>();
    scopes.add(SCOPE1);/*from w ww  . j  a va2s.  c  o  m*/
    scopes.add(SCOPE2);

    final AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getId()).thenReturn(AT_ID);
    when(accessToken.getScopes()).thenReturn(scopes);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService))
            .thenReturn(accessToken);

    final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);

    final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController();
    oauth20ServiceValidateController.initApplicationContext(webApplicationContext);
    oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor);
    oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService);
    oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse);
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    assertEquals("", mockResponse.getContentAsString());

    final Map<String, Object> model = modelAndView.getModel();
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN));
    assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE));
}

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

/** Test for resource {@code /rest/<repository>/repo/manifest} */
@Test// w  w  w.  j a  va  2  s.com
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:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java

@Test
public void test_process_request_plain_some_range() throws Exception {
    final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setMethod("GET");
    httpServletRequest.setPathInfo("/input.dat");
    httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=-48");

    final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
    HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat",
            "application/pdf", INPUT_FILE, false);

    assertEquals("CcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",
            mockHttpServletResponse.getContentAsString());

    assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus());
    assertEquals("application/pdf", mockHttpServletResponse.getContentType());
    assertEquals("inline;filename=\"input.dat\"",
            mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION));
    assertEquals("48", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH));

}

From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java

@Test
public void test_process_request_plain_minus_range() throws Exception {
    final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setMethod("GET");
    httpServletRequest.setPathInfo("/input.dat");
    httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=-50");

    final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
    HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat",
            "application/pdf", INPUT_FILE, false);

    assertEquals("BbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",
            mockHttpServletResponse.getContentAsString());

    assertEquals(HttpServletResponse.SC_PARTIAL_CONTENT, mockHttpServletResponse.getStatus());
    assertEquals("application/pdf", mockHttpServletResponse.getContentType());
    assertEquals("inline;filename=\"input.dat\"",
            mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION));
    assertEquals("50", mockHttpServletResponse.getHeader(HttpHeaders.CONTENT_LENGTH));

}