/** * Assert the values of the response header as String values. * @since 4.3 */ public ResultMatcher stringValues(final String name, final String... values) { return result -> { List<Object> actual = result.getResponse().getHeaderValues(name); assertEquals("Response header '" + name + "'", Arrays.asList(values), actual); }; }
private List<NameValuePair> responseHeaders() { Collection<String> headerNames = this.response.getHeaderNames(); List<NameValuePair> responseHeaders = new ArrayList<>(headerNames.size()); for (String headerName : headerNames) { List<Object> headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } return responseHeaders; }
@Override public void match(MvcResult result) { List<Object> actual = result.getResponse().getHeaderValues(name); assertEquals("Response header '" + name + "'", Arrays.asList(values), actual); } };
/** * Assert the values of the response header as String values. * @since 4.3 */ public ResultMatcher stringValues(final String name, final String... values) { return result -> { List<Object> actual = result.getResponse().getHeaderValues(name); assertEquals("Response header '" + name + "'", Arrays.asList(values), actual); }; }
private List<NameValuePair> responseHeaders() { Collection<String> headerNames = this.response.getHeaderNames(); List<NameValuePair> responseHeaders = new ArrayList<>(headerNames.size()); for (String headerName : headerNames) { List<Object> headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } return responseHeaders; }
private List<NameValuePair> responseHeaders() { Collection<String> headerNames = this.response.getHeaderNames(); List<NameValuePair> responseHeaders = new ArrayList<NameValuePair>(headerNames.size()); for (String headerName : headerNames) { List<Object> headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } for (Cookie cookie : this.response.getCookies()) { responseHeaders.add(new NameValuePair("Set-Cookie", valueOfCookie(cookie))); } return responseHeaders; }
/** * Asserts that the specified nearest value has been used and check the corresponding HTTP * warning * * @param layerId * @param expectedValue */ protected void assertNoNearestWarning(String layerId, String dimension) { String expected = "99 No nearest value found on " + layerId + ": " + dimension; MockHttpServletResponse response = getLastResponse(); List<Object> values = response.getHeaderValues(HttpHeaders.WARNING); Object found = values.stream().filter(v -> expected.equals(v)).findFirst().orElse(null); assertNotNull( "Could not find\n" + expected + "\n among the following warnings:\n" + values, found); } }
/** * Asserts that the specified nearest value has been used and check the corresponding HTTP * warning * * @param layerId * @param expectedValue */ protected void assertNearestTimeWarning(String layerId, String expectedValue) { String expected = "99 Nearest value used: time=" + expectedValue + " " + ResourceInfo.TIME_UNIT + " (" + layerId + ")"; MockHttpServletResponse response = getLastResponse(); List<Object> values = response.getHeaderValues(HttpHeaders.WARNING); Object found = values.stream().filter(v -> expected.equals(v)).findFirst().orElse(null); assertNotNull( "Could not find\n" + expected + "\n among the following warnings:\n" + values, found); }
/** * Checks that the last HTTP response had the expected number of "Warning" headers * * @param expectedValue */ protected void assertWarningCount(int expectedValue) { MockHttpServletResponse response = getLastResponse(); List<Object> values = response.getHeaderValues(HttpHeaders.WARNING); assertNotNull(values); assertEquals( "Expected to find a different number of warnings", expectedValue, values.size()); }
for (Object value : servletResponse.getHeaderValues(name)) { response.headers().add(name, value);
for (Object value : servletResponse.getHeaderValues(name)) { response.headers().add(name, value);
@Override public void messageReceived(ChannelHandlerContext ctx, HttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST); return; } MockHttpServletRequest servletRequest = createServletRequest(request); MockHttpServletResponse servletResponse = new MockHttpServletResponse(); this.servlet.service(servletRequest, servletResponse); HttpResponseStatus status = HttpResponseStatus.valueOf(servletResponse.getStatus()); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); for (String name : servletResponse.getHeaderNames()) { for (Object value : servletResponse.getHeaderValues(name)) { response.addHeader(name, value); } } // Write the initial line and the header. ctx.write(response); InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray()); // Write the content. ChannelFuture writeFuture = ctx.write(new ChunkedStream(contentStream)); writeFuture.addListener(ChannelFutureListener.CLOSE); }
@Override public void messageReceived(ChannelHandlerContext ctx, HttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST); return; } MockHttpServletRequest servletRequest = createServletRequest(request); MockHttpServletResponse servletResponse = new MockHttpServletResponse(); this.servlet.service(servletRequest, servletResponse); HttpResponseStatus status = HttpResponseStatus.valueOf(servletResponse.getStatus()); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); for (String name : servletResponse.getHeaderNames()) { for (Object value : servletResponse.getHeaderValues(name)) { response.addHeader(name, value); } } // Write the initial line and the header. ctx.write(response); InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray()); // Write the content. ChannelFuture writeFuture = ctx.write(new ChunkedStream(contentStream)); writeFuture.addListener(ChannelFutureListener.CLOSE); }