|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet.client; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import jakarta.servlet.http.Cookie; |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | + |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.http.HttpMethod; |
| 28 | +import org.springframework.http.HttpStatus; |
| 29 | +import org.springframework.http.HttpStatusCode; |
| 30 | +import org.springframework.http.client.ClientHttpRequest; |
| 31 | +import org.springframework.http.client.ClientHttpRequestFactory; |
| 32 | +import org.springframework.http.client.ClientHttpResponse; |
| 33 | +import org.springframework.mock.http.client.MockClientHttpRequest; |
| 34 | +import org.springframework.mock.http.client.MockClientHttpResponse; |
| 35 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 36 | +import org.springframework.test.web.servlet.MockMvc; |
| 37 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 38 | +import org.springframework.util.Assert; |
| 39 | +import org.springframework.util.StringUtils; |
| 40 | + |
| 41 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; |
| 42 | + |
| 43 | +/** |
| 44 | + * A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}. |
| 45 | + * |
| 46 | + * @author Rossen Stoyanchev |
| 47 | + * @author Rob Worsnop |
| 48 | + * @since 7.0 |
| 49 | + */ |
| 50 | +class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory { |
| 51 | + |
| 52 | + private final MockMvc mockMvc; |
| 53 | + |
| 54 | + |
| 55 | + MockMvcClientHttpRequestFactory(MockMvc mockMvc) { |
| 56 | + Assert.notNull(mockMvc, "MockMvc must not be null"); |
| 57 | + this.mockMvc = mockMvc; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Override |
| 62 | + public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) { |
| 63 | + return new MockClientHttpRequest(httpMethod, uri) { |
| 64 | + @Override |
| 65 | + public ClientHttpResponse executeInternal() { |
| 66 | + return getClientHttpResponse(httpMethod, uri, getHeaders(), getBodyAsBytes()); |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + private ClientHttpResponse getClientHttpResponse( |
| 72 | + HttpMethod httpMethod, URI uri, HttpHeaders requestHeaders, byte[] requestBody) { |
| 73 | + |
| 74 | + try { |
| 75 | + Cookie[] cookies = parseCookies(requestHeaders.get(HttpHeaders.COOKIE)); |
| 76 | + MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri) |
| 77 | + .content(requestBody).headers(requestHeaders); |
| 78 | + if (cookies.length > 0) { |
| 79 | + requestBuilder.cookie(cookies); |
| 80 | + } |
| 81 | + MockHttpServletResponse servletResponse = this.mockMvc |
| 82 | + .perform(requestBuilder) |
| 83 | + .andReturn() |
| 84 | + .getResponse(); |
| 85 | + |
| 86 | + HttpStatusCode status = HttpStatusCode.valueOf(servletResponse.getStatus()); |
| 87 | + byte[] body = servletResponse.getContentAsByteArray(); |
| 88 | + if (body.length == 0) { |
| 89 | + String error = servletResponse.getErrorMessage(); |
| 90 | + if (StringUtils.hasLength(error)) { |
| 91 | + // sendError message as default body |
| 92 | + body = error.getBytes(StandardCharsets.UTF_8); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); |
| 97 | + clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse)); |
| 98 | + return clientResponse; |
| 99 | + } |
| 100 | + catch (Exception ex) { |
| 101 | + byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8); |
| 102 | + return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static Cookie[] parseCookies(@Nullable List<String> headerValues) { |
| 107 | + if (headerValues == null) { |
| 108 | + return new Cookie[0]; |
| 109 | + } |
| 110 | + return headerValues.stream() |
| 111 | + .flatMap(header -> StringUtils.commaDelimitedListToSet(header).stream()) |
| 112 | + .map(MockMvcClientHttpRequestFactory::parseCookie) |
| 113 | + .toArray(Cookie[]::new); |
| 114 | + } |
| 115 | + |
| 116 | + private static Cookie parseCookie(String cookie) { |
| 117 | + String[] parts = StringUtils.split(cookie, "="); |
| 118 | + Assert.isTrue(parts != null && parts.length == 2, "Invalid cookie: '" + cookie + "'"); |
| 119 | + return new Cookie(parts[0], parts[1]); |
| 120 | + } |
| 121 | + |
| 122 | + private HttpHeaders getResponseHeaders(MockHttpServletResponse response) { |
| 123 | + HttpHeaders headers = new HttpHeaders(); |
| 124 | + for (String name : response.getHeaderNames()) { |
| 125 | + List<String> values = response.getHeaders(name); |
| 126 | + for (String value : values) { |
| 127 | + headers.add(name, value); |
| 128 | + } |
| 129 | + } |
| 130 | + return headers; |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments