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

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

Introduction

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

Prototype

@Nullable
    public String getErrorMessage() 

Source Link

Usage

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

@Test
public void testGetProductsPagingValidation() throws Exception {
    MockHttpServletResponse response = getAsServletResponse(
            "/rest/oseo/collections/SENTINEL2/products?offset=-1");
    assertEquals(400, response.getStatus());
    assertThat(response.getErrorMessage(), containsString("offset"));

    response = getAsServletResponse("/rest/oseo/collections/SENTINEL2/products?limit=-1");
    assertEquals(400, response.getStatus());
    assertThat(response.getErrorMessage(), containsString("limit"));

    response = getAsServletResponse("/rest/oseo/collections/SENTINEL2/products?limit=1000");
    assertEquals(400, response.getStatus());
    assertThat(response.getErrorMessage(), containsString("limit"));
}

From source file:org.geoserver.security.BruteForceAttackTest.java

private void testParallelLogin(String expectedMessage, Function<Integer, String> userNameGenerator)
        throws InterruptedException, ExecutionException {
    // idea, setup several threads to do the same failing auth in parallel,
    // ensuring they are all ready to go at the same time using a latch
    final int NTHREADS = 32;
    ExecutorService service = Executors.newFixedThreadPool(NTHREADS);
    CountDownLatch latch = new CountDownLatch(NTHREADS);
    AtomicInteger concurrentLoginsPrevented = new AtomicInteger(0);
    List<Future<?>> futures = new ArrayList<>();
    long start = System.currentTimeMillis();
    for (int i = 0; i < NTHREADS; i++) {
        final int idx = i;
        Future<?> future = service.submit(() -> {
            // mark and ready and wait for others
            latch.countDown();/*from   w  w  w  .  ja  v  a  2s.  c  o  m*/
            latch.await();

            // execute request timing how long it took
            MockHttpServletRequest request = createRequest(HELLO_GET_REQUEST);
            request.setMethod("GET");
            request.setContent(new byte[] {});
            String userName = userNameGenerator.apply(idx);
            String token = userName + ":foobar";
            request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
            MockHttpServletResponse response = dispatch(request, "UTF-8");

            // check the response and see the error message
            assertEquals(401, response.getStatus());
            final String message = response.getErrorMessage();
            // System.out.println(message);
            if (message.contains(expectedMessage)) {
                concurrentLoginsPrevented.incrementAndGet();
            }

            return null;
        });
        futures.add(future);
    }

    // wait for termination
    for (Future<?> future : futures) {
        future.get();
    }
    long awaitTime = System.currentTimeMillis() - start;
    service.shutdown();

    // now, either the threads all serialized and waited (extremely unlikely, but
    // not impossible) or at least one got bumped immediately with a concurrent login message
    assertTrue(awaitTime > NTHREADS * 1000 || concurrentLoginsPrevented.get() > 0);

}

From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java

/**
 * @param requestType//w  w w  .  java  2 s.co  m
 * @param contentType
 * @param url
 * @param data
 * @param statusCode
 */
protected MockHttpServletResponse sendData(String requestType, String contentType, String url, String data,
        int statusCode) throws Exception {
    MockHttpServletRequest request = createRequest(servletContext, requestType, url, getUser(), getUserRoles());
    request.setContentType(contentType);

    if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
        request.setParameters(parseParamData(data));
        request.setContent(new byte[] {});
    } else {
        request.setContent(data.getBytes());
    }

    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);

    LOG.debug("Received response: {}", stringifyResponse(response));
    assertEquals(response.getErrorMessage(), statusCode, response.getStatus());

    return response;
}

From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java

/**
 * @param requestType// w ww  . j ava 2 s . c  om
 * @param contentType
 * @param url
 * @param data
 * @param statusCode
 */
protected MockHttpServletResponse sendData(String requestType, String contentType, String url, String data,
        int statusCode) throws Exception {
    MockHttpServletRequest request = createRequest(requestType, url);
    request.setContentType(contentType);

    if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
        request.setParameters(parseParamData(data));
        request.setContent(new byte[] {});
    } else {
        request.setContent(data.getBytes());
    }

    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);

    LOG.debug("Received response: {}", stringifyResponse(response));
    assertEquals(response.getErrorMessage(), statusCode, response.getStatus());

    return response;
}