Java Code Examples for org.springframework.mock.web.MockHttpServletResponse#getHeaderNames()
The following examples show how to use
org.springframework.mock.web.MockHttpServletResponse#getHeaderNames() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserControllerTest.java From springboot-learning-example with Apache License 2.0 | 6 votes |
@Test public void getUser() throws Exception { MvcResult result= mockMvc.perform(get("/users/update/{id}/", 1)) .andExpect(status().isOk()) .andExpect(view().name("userForm")) .andExpect(MockMvcResultMatchers.model().attributeExists("action")) .andExpect(model().attribute("user", hasProperty("id", is(1L)))) .andExpect(model().attribute("user", hasProperty("name", is("Sergey")))) .andExpect(model().attribute("user", hasProperty("age", is(24)))) .andExpect(model().attribute("user", hasProperty("birthday", is("1994-01-01")))) .andReturn(); MockHttpServletResponse mockResponse=result.getResponse(); assertThat(mockResponse.getContentType()).isEqualTo("text/html;charset=UTF-8"); Collection<String> responseHeaders = mockResponse.getHeaderNames(); assertNotNull(responseHeaders); assertEquals(2, responseHeaders.size()); assertEquals("Check for Content-Type header", "Accept-Language", responseHeaders.iterator().next()); String responseAsString=mockResponse.getContentAsString(); assertTrue(responseAsString.contains("用户管理")); }
Example 2
Source File: PrintingResultHandler.java From spring-analysis-note with MIT License | 5 votes |
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { headers.put(name, response.getHeaders(name)); } return headers; }
Example 3
Source File: MockMvcClientHttpRequestFactory.java From spring-analysis-note with MIT License | 5 votes |
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { List<String> values = response.getHeaders(name); for (String value : values) { headers.add(name, value); } } return headers; }
Example 4
Source File: PrintingResultHandler.java From java-technology-stack with MIT License | 5 votes |
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { headers.put(name, response.getHeaders(name)); } return headers; }
Example 5
Source File: MockMvcClientHttpRequestFactory.java From java-technology-stack with MIT License | 5 votes |
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { List<String> values = response.getHeaders(name); for (String value : values) { headers.add(name, value); } } return headers; }
Example 6
Source File: MockHttpClient.java From karate with MIT License | 5 votes |
private static void logResponseHeaders(StringBuilder sb, int id, MockHttpServletResponse response) { Set<String> keys = new TreeSet(response.getHeaderNames()); for (String key : keys) { List<String> entries = response.getHeaders(key); sb.append(id).append(' ').append('<').append(' ') .append(key).append(": ").append(entries.size() == 1 ? entries.get(0) : entries).append('\n'); } }
Example 7
Source File: PrintingResultHandler.java From spring4-understanding with Apache License 2.0 | 5 votes |
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { headers.put(name, response.getHeaders(name)); } return headers; }
Example 8
Source File: MockMvcClientHttpRequestFactory.java From spring4-understanding with Apache License 2.0 | 5 votes |
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { List<String> values = response.getHeaders(name); for (String value : values) { headers.add(name, value); } } return headers; }
Example 9
Source File: ServletNettyHandler.java From nettyholdspringmvc with Apache License 2.0 | 5 votes |
@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); }
Example 10
Source File: ServletNettyChannelHandler.java From netty-cookbook with Apache License 2.0 | 5 votes |
@Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception { if (!fullHttpRequest.getDecoderResult().isSuccess()) { sendError(channelHandlerContext, BAD_REQUEST); return; } MockHttpServletRequest servletRequest = createHttpServletRequest(fullHttpRequest); 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 (String value : servletResponse.getHeaders(name)) { response.headers().add(name, value); } } // Write the initial line and the header. channelHandlerContext.write(response); InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray()); // Write the content and flush it. ChannelFuture writeFuture = channelHandlerContext.writeAndFlush(new ChunkedStream(contentStream)); writeFuture.addListener(ChannelFutureListener.CLOSE); }
Example 11
Source File: MockMvcClientHttpRequestFactory.java From initializr with Apache License 2.0 | 5 votes |
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) { HttpHeaders headers = new HttpHeaders(); for (String name : response.getHeaderNames()) { List<String> values = response.getHeaders(name); for (String value : values) { headers.add(name, value); } } return headers; }
Example 12
Source File: DapTestCommon.java From tds with BSD 3-Clause "New" or "Revised" License | 4 votes |
public HttpResponse execute(HttpRequestBase rq) throws IOException { URI uri = rq.getURI(); DapController controller = getController(uri); StandaloneMockMvcBuilder mvcbuilder = MockMvcBuilders.standaloneSetup(controller); mvcbuilder.setValidator(new TestServlet.NullValidator()); MockMvc mockMvc = mvcbuilder.build(); MockHttpServletRequestBuilder mockrb = MockMvcRequestBuilders.get(uri); // We need to use only the path part mockrb.servletPath(uri.getPath()); // Move any headers from rq to mockrb Header[] headers = rq.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Header h = headers[i]; mockrb.header(h.getName(), h.getValue()); } // Since the url has the query parameters, // they will automatically be parsed and added // to the rb parameters. // Finally set the resource dir mockrb.requestAttr("RESOURCEDIR", this.resourcepath); // Now invoke the servlet MvcResult result; try { result = mockMvc.perform(mockrb).andReturn(); } catch (Exception e) { throw new IOException(e); } // Collect the output MockHttpServletResponse res = result.getResponse(); byte[] byteresult = res.getContentAsByteArray(); // Convert to HttpResponse HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, res.getStatus(), ""); if (response == null) throw new IOException("HTTPMethod.executeMock: Response was null"); Collection<String> keys = res.getHeaderNames(); // Move headers to the response for (String key : keys) { List<String> values = res.getHeaders(key); for (String v : values) { response.addHeader(key, v); } } ByteArrayEntity entity = new ByteArrayEntity(byteresult); String sct = res.getContentType(); entity.setContentType(sct); response.setEntity(entity); return response; }
Example 13
Source File: MockHttpClient.java From karate with MIT License | 4 votes |
@Override protected HttpResponse makeHttpRequest(HttpBody entity, ScenarioContext context) { logger.info("making mock http client request: {} - {}", request.getMethod(), getRequestUri()); MockHttpServletRequest req = requestBuilder.buildRequest(getServletContext()); byte[] bytes; if (entity != null) { bytes = entity.getBytes(); req.setContentType(entity.getContentType()); if (entity.isMultiPart()) { for (MultiPartItem item : entity.getParts()) { MockMultiPart part = new MockMultiPart(item); req.addPart(part); if (!part.isFile()) { req.addParameter(part.getName(), part.getValue()); } } } else if (entity.isUrlEncoded()) { req.addParameters(entity.getParameters()); } else { req.setContent(bytes); } } else { bytes = null; } MockHttpServletResponse res = new MockHttpServletResponse(); logRequest(req, bytes); long startTime = System.currentTimeMillis(); try { getServlet(request).service(req, res); } catch (Exception e) { String message = e.getMessage(); if (message == null && e.getCause() != null) { message = e.getCause().getMessage(); } logger.error("mock servlet request failed: {}", message); throw new RuntimeException(e); } HttpResponse response = new HttpResponse(startTime, System.currentTimeMillis()); bytes = res.getContentAsByteArray(); logResponse(res, bytes); response.setUri(getRequestUri()); response.setBody(bytes); response.setStatus(res.getStatus()); for (Cookie c : res.getCookies()) { com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue()); cookie.put(DOMAIN, c.getDomain()); cookie.put(PATH, c.getPath()); cookie.put(SECURE, c.getSecure() + ""); cookie.put(MAX_AGE, c.getMaxAge() + ""); cookie.put(VERSION, c.getVersion() + ""); response.addCookie(cookie); } for (String headerName : res.getHeaderNames()) { response.putHeader(headerName, res.getHeaders(headerName)); } return response; }
Example 14
Source File: ContractResultHandler.java From spring-cloud-contract with Apache License 2.0 | 4 votes |
private void addResponseHeaders(ResponseDefinitionBuilder definition, MockHttpServletResponse input) { for (String name : input.getHeaderNames()) { definition.withHeader(name, input.getHeader(name)); } }