public byte[] getContentAsByteArray() { Assert.state(this.response instanceof MockHttpServletResponse, "MockHttpServletResponse required"); return ((MockHttpServletResponse) this.response).getContentAsByteArray(); }
/** * Assert the response body content as a byte array. */ public ResultMatcher bytes(final byte[] expectedContent) { return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsByteArray()); }
/** * Evaluate the XPath and assert the {@link Boolean} value found. */ public ResultMatcher booleanValue(final Boolean value) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertBoolean(response.getContentAsByteArray(), getDefinedEncoding(response), value); }; }
/** * 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)); }; }
/** * Evaluate the XPath and assert the number of nodes found. */ public ResultMatcher nodeCount(final int expectedCount) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNodeCount(response.getContentAsByteArray(), getDefinedEncoding(response), expectedCount); }; }
/** * Evaluate the XPath and assert the {@link Double} value found with the * given Hamcrest {@link Matcher}. */ public ResultMatcher number(final Matcher<? super Double> matcher) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNumber(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); }; }
/** * Apply the XPath and assert the {@link String} value found. */ public ResultMatcher string(final String expectedValue) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertString(response.getContentAsByteArray(), getDefinedEncoding(response), expectedValue); }; }
/** * Evaluate the XPath and assert that content exists. */ public ResultMatcher exists() { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.exists(response.getContentAsByteArray(), getDefinedEncoding(response)); }; }
/** * Evaluate the XPath and assert the number of nodes found with the given * Hamcrest {@link Matcher}. */ public ResultMatcher nodeCount(final Matcher<Integer> matcher) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNodeCount(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); }; }
/** * Evaluate the XPath and assert the {@link Double} value found. */ public ResultMatcher number(final Double expectedValue) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNumber(response.getContentAsByteArray(), getDefinedEncoding(response), expectedValue); }; }
/** * Evaluate the XPath and assert the {@link Node} content found with the * given Hamcrest {@link Matcher}. */ public ResultMatcher node(final Matcher<? super Node> matcher) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNode(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); }; }
/** * Apply the XPath and assert the {@link String} value found with the given * Hamcrest {@link Matcher}. */ public ResultMatcher string(final Matcher<? super String> matcher) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertString(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); }; }
private WebResponseData webResponseData() throws IOException { List<NameValuePair> responseHeaders = responseHeaders(); int statusCode = (this.response.getRedirectedUrl() != null ? HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus()); String statusMessage = statusMessage(statusCode); return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders); }
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); } }
@Test public void servletWriterCommittedWhenBufferSizeExceeded() throws IOException { assertFalse(response.isCommitted()); response.getWriter().write("X"); assertFalse(response.isCommitted()); int size = response.getBufferSize(); char[] data = new char[size]; Arrays.fill(data, 'p'); response.getWriter().write(data); assertTrue(response.isCommitted()); assertEquals(size + 1, response.getContentAsByteArray().length); }
@Test public void servletWriterCommittedOnWriterFlush() throws IOException { assertFalse(response.isCommitted()); response.getWriter().write("X"); assertFalse(response.isCommitted()); response.getWriter().flush(); assertTrue(response.isCommitted()); assertEquals(1, response.getContentAsByteArray().length); }
@Test public void servletOutputStreamCommittedOnOutputStreamFlush() throws IOException { assertFalse(response.isCommitted()); response.getOutputStream().write('X'); assertFalse(response.isCommitted()); response.getOutputStream().flush(); assertTrue(response.isCommitted()); assertEquals(1, response.getContentAsByteArray().length); }
@Test // SPR-16683 public void servletWriterCommittedOnWriterClose() throws IOException { assertFalse(response.isCommitted()); response.getWriter().write("X"); assertFalse(response.isCommitted()); response.getWriter().close(); assertTrue(response.isCommitted()); assertEquals(1, response.getContentAsByteArray().length); }
@Test public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException { assertFalse(response.isCommitted()); response.getOutputStream().write('X'); assertFalse(response.isCommitted()); int size = response.getBufferSize(); response.getOutputStream().write(new byte[size]); assertTrue(response.isCommitted()); assertEquals(size + 1, response.getContentAsByteArray().length); }
@Test public void servletOutputStreamCommittedOnFlushBuffer() throws IOException { assertFalse(response.isCommitted()); response.getOutputStream().write('X'); assertFalse(response.isCommitted()); response.flushBuffer(); assertTrue(response.isCommitted()); assertEquals(1, response.getContentAsByteArray().length); }