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

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

Introduction

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

Prototype

@Override
    public int getStatus() 

Source Link

Usage

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;// w w  w  .  j av a  2s. 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);
}

From source file:org.openmrs.module.owa.filter.OwaFilterTest.java

/**
 * Test that the OwaFilter class actually services from the base path defined in the global
 * property and ignore any other requests.
 *///ww  w .  java2  s  .c  o m
@Test
public void testOwaFilterUsesGlobalProperty() throws Exception {
    OwaFilter owaFilter = new OwaFilter();
    owaFilter.init(filterConfig);

    MockFilterChain mockFilterChain = new MockFilterChain();
    MockHttpServletResponse rsp = new MockHttpServletResponse();

    // First make sure that it works with the default base URL
    Context.getAdministrationService()
            .saveGlobalProperty(new GlobalProperty(AppManager.KEY_APP_BASE_URL, DEFAULT_APP_BASE_URL));

    MockHttpServletRequest req = new MockHttpServletRequest("GET", DEFAULT_APP_BASE_URI + SOME_PATH_IN_APP);
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertEquals(FILE_SERVLET_REDIRECT_URL + SOME_PATH_IN_APP, rsp.getForwardedUrl());

    // Now try a custom base URL
    Context.getAdministrationService()
            .saveGlobalProperty(new GlobalProperty(AppManager.KEY_APP_BASE_URL, SOME_RANDOM_BASE_URL));
    mockFilterChain = new MockFilterChain();
    req = new MockHttpServletRequest("GET", SOME_RANDOM_BASE_URI + SOME_PATH_IN_APP);
    rsp = new MockHttpServletResponse();
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertEquals(FILE_SERVLET_REDIRECT_URL + SOME_PATH_IN_APP, rsp.getForwardedUrl());

    // Ensure non-OWA base URLs are ignored
    req = new MockHttpServletRequest("GET", DEFAULT_APP_BASE_URI + SOME_PATH_IN_APP); // we can reuse this URL because the global property has been reset
    rsp = new MockHttpServletResponse();
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertNull(rsp.getForwardedUrl());
}

From source file:ch.silviowangler.dox.web.DocumentControllerTest.java

@Test
public void getTheDocumentContentExpectNotFound2()
        throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException {

    MockHttpServletResponse response = new MockHttpServletResponse();
    long expectedDocumentId = 2L;
    when(documentService.findPhysicalDocument(expectedDocumentId))
            .thenThrow(new DocumentNotInStoreException("adsfdfadsf", expectedDocumentId));
    controller.getDocument(expectedDocumentId, response);
    assertThat(response.getStatus(), is(SC_NOT_FOUND));
}

From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java

public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {//from   ww w. j  av a2  s . c om
        mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
        Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
        headers = new HashMap<>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
            String methodName = stackTraceElement.getMethodName();

            if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) {

                String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

                testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test");

                headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":"
                        + stackTraceElement.getLineNumber() + ")");

                break;
            }
        }
    }

    if (data != null) {
        mockHttpServletRequest.setContent(data);

        String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
            mockHttpServletRequest.setContentType(contentType);
        } else {
            mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
        }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        mockHttpServletRequest.addHeader(key, value);
    }

    try {
        MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

        webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

        int statusCode = mockHttpServletResponse.getStatus();
        byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

        Map<String, String> responseHeaders = new HashMap<>();

        for (String name : mockHttpServletResponse.getHeaderNames()) {
            responseHeaders.put(name, mockHttpServletResponse.getHeader(name));
        }

        return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:nl.dtls.fairdatapoint.api.controller.DataAccessorControllerTest.java

/**
 * The default content type is text/turtle, when the accept header is not
 * set the default content type is served. This test is excepted to pass.
 * //from  w w w  .jav a  2 s.c  om
 * @throws Exception 
 */
@Test
public void noAcceptHeader() throws Exception {
    MockHttpServletRequest request;
    MockHttpServletResponse response;
    Object handler;

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("GET");
    request.setRequestURI("/textmining/gene-disease-association_lumc/sparql");
    handler = handlerMapping.getHandler(request).getHandler();
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
}

From source file:nl.dtls.fairdatapoint.api.controller.MetadataControllerTest.java

/**
 * The default content type is text/turtle, when the accept header is not
 * set the default content type is served. This test is excepted to pass.
 * //from  w ww  . j av  a  2 s.  c o  m
 * @throws Exception 
 */
@Test
public void noAcceptHeader() throws Exception {
    MockHttpServletRequest request;
    MockHttpServletResponse response;
    Object handler;

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("GET");
    request.setRequestURI("/textmining");
    handler = handlerMapping.getHandler(request).getHandler();
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
}

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   w w  w  .  j av a 2s  . 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);// ww  w.ja va 2 s. co  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 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);/*from   w  w  w .  jav  a  2s. 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 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  www  . j  a  va 2s.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));
}