Refine search
/** * Assert the response status code is equal to an integer value. * Use the {@code StatusResultMatchers.isEqualTo} extension in Kotlin. */ public ResultMatcher is(final int status) { return result -> assertEquals("Response status", status, result.getResponse().getStatus()); }
/** * Assert a request attribute value with the given Hamcrest {@link Matcher}. */ @SuppressWarnings("unchecked") public <T> ResultMatcher attribute(final String name, final Matcher<T> matcher) { return result -> { T value = (T) result.getRequest().getAttribute(name); assertThat("Request attribute '" + name + "'", value, matcher); }; }
/** * Assert the selected view name. */ public ResultMatcher name(final String expectedViewName) { return result -> { ModelAndView mav = result.getModelAndView(); if (mav == null) { fail("No ModelAndView found"); } assertEquals("View name", expectedViewName, mav.getViewName()); }; }
/** * Assert the result from asynchronous processing. * <p>This method can be used when a controller method returns {@link Callable} * or {@link WebAsyncTask}. The value matched is the value returned from the * {@code Callable} or the exception raised. */ public <T> ResultMatcher asyncResult(final Object expectedResult) { return result -> { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); assertEquals("Async result", expectedResult, result.getAsyncResult()); }; }
/** * Assert the result from asynchronous processing with the given matcher. * <p>This method can be used when a controller method returns {@link Callable} * or {@link WebAsyncTask}. */ @SuppressWarnings("unchecked") public <T> ResultMatcher asyncResult(final Matcher<T> matcher) { return result -> { HttpServletRequest request = result.getRequest(); assertAsyncStarted(request); assertThat("Async result", (T) result.getAsyncResult(), matcher); }; }
/** * Assert the response body content with a Hamcrest {@link Matcher}. * <pre class="code"> * mockMvc.perform(get("/path")) * .andExpect(content().string(containsString("text"))); * </pre> */ public ResultMatcher string(final Matcher<? super String> matcher) { return result -> assertThat("Response content", result.getResponse().getContentAsString(), matcher); }
/** * Evaluate the XPath and assert that content doesn't exist. */ public ResultMatcher doesNotExist() { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.doesNotExist(response.getContentAsByteArray(), getDefinedEncoding(response)); }; }
/** * Assert the response body content as a String. */ public ResultMatcher string(final String expectedContent) { return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsString()); }
private ClientHttpResponse getClientHttpResponse( HttpMethod httpMethod, URI uri, HttpHeaders requestHeaders, byte[] requestBody) { try { MockHttpServletResponse servletResponse = this.mockMvc .perform(request(httpMethod, uri).content(requestBody).headers(requestHeaders)) .andReturn() .getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse)); return clientResponse; } catch (Exception ex) { byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8); return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR); } }
/** * Assert the response status code with the given Hamcrest {@link Matcher}. * Use the {@code StatusResultMatchers.isEqualTo} extension in Kotlin. */ public ResultMatcher is(final Matcher<Integer> matcher) { return result -> assertThat("Response status", result.getResponse().getStatus(), matcher); }
/** * Assert the primary value of the named response header as a {@code long}. * <p>The {@link ResultMatcher} returned by this method throws an * {@link AssertionError} if the response does not contain the specified * header, or if the supplied {@code value} does not match the primary value. */ public ResultMatcher longValue(final String name, final long value) { return result -> { MockHttpServletResponse response = result.getResponse(); assertTrue("Response does not contain header '" + name + "'", response.containsHeader(name)); String headerValue = response.getHeader(name); if (headerValue != null) { assertEquals("Response header '" + name + "'", value, Long.parseLong(headerValue)); } }; }
/** * Assert the ServletResponse content type after parsing it as a MediaType. * The given content type must fully match including type, sub-type, and * parameters. For checking only the type and sub-type see * {@link #contentTypeCompatibleWith(MediaType)}. */ public ResultMatcher contentType(final MediaType contentType) { return result -> { String actual = result.getResponse().getContentType(); assertTrue("Content type not set", actual != null); if (actual != null) { assertEquals("Content type", contentType, MediaType.parseMediaType(actual)); } }; }
/** * Assert the primary value of the response header as a String value. */ public ResultMatcher string(final String name, final String value) { return result -> assertEquals("Response header '" + name + "'", value, result.getResponse().getHeader(name)); }
/** * Assert the response body content as a byte array. */ public ResultMatcher bytes(final byte[] expectedContent) { return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsByteArray()); }
/** * Assert the primary value of the response header with the given Hamcrest * String {@code Matcher}. */ public ResultMatcher string(final String name, final Matcher<? super String> matcher) { return result -> assertThat("Response header '" + name + "'", result.getResponse().getHeader(name), matcher); }
/** * Assert the ServletResponse content type is compatible with the given * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}. */ public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) { return result -> { String actual = result.getResponse().getContentType(); assertTrue("Content type not set", actual != null); if (actual != null) { MediaType actualContentType = MediaType.parseMediaType(actual); assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType)); } }; }
/** * Asserts the request was redirected to the given URL. * <p>This method accepts only exact matches. * @param expectedUrl the exact URL expected */ public static ResultMatcher redirectedUrl(String expectedUrl) { return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); }
/** * Assert the Servlet response error message. */ public ResultMatcher reason(final String reason) { return result -> assertEquals("Response status reason", reason, result.getResponse().getErrorMessage()); }
/** * Print {@link MvcResult} details. */ @Override public final void handle(MvcResult result) throws Exception { this.printer.printHeading("MockHttpServletRequest"); printRequest(result.getRequest()); this.printer.printHeading("Handler"); printHandler(result.getHandler(), result.getInterceptors()); this.printer.printHeading("Async"); printAsyncResult(result); this.printer.printHeading("Resolved Exception"); printResolvedException(result.getResolvedException()); this.printer.printHeading("ModelAndView"); printModelAndView(result.getModelAndView()); this.printer.printHeading("FlashMap"); printFlashMap(RequestContextUtils.getOutputFlashMap(result.getRequest())); this.printer.printHeading("MockHttpServletResponse"); printResponse(result.getResponse()); }
/** * Assert a cookie does not exist. Note that the existence check is * irrespective of whether max age is 0, i.e. expired. */ public ResultMatcher doesNotExist(final String name) { return result -> { Cookie cookie = result.getResponse().getCookie(name); assertTrue("Unexpected cookie with name '" + name + "'", cookie == null); }; }