Browse Source
Retires HttpBinCompatibleController.java in favor of container. See gh-2949pull/3006/head
sgibb
1 year ago
13 changed files with 475 additions and 386 deletions
@ -1,236 +0,0 @@
@@ -1,236 +0,0 @@
|
||||
/* |
||||
* Copyright 2013-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.server.mvc.test; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Enumeration; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
import java.util.stream.Stream; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import jakarta.servlet.ServletException; |
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
import jakarta.servlet.http.Part; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.util.MultiValueMap; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PathVariable; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestHeader; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
|
||||
@RestController |
||||
@RequestMapping("/httpbin") |
||||
public class HttpBinCompatibleController { |
||||
|
||||
private static final Log log = LogFactory.getLog(HttpBinCompatibleController.class); |
||||
|
||||
private static final String HEADER_REQ_VARY = "X-Request-Vary"; |
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
||||
|
||||
@GetMapping("/") |
||||
public String home() { |
||||
return "httpbin compatible home"; |
||||
} |
||||
|
||||
@RequestMapping(path = "/headers", method = { RequestMethod.GET, RequestMethod.POST }, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> headers(HttpServletRequest request) { |
||||
Map<String, Object> result = new HashMap<>(); |
||||
result.put("headers", getHeaders(request)); |
||||
return result; |
||||
} |
||||
|
||||
/* |
||||
* @PatchMapping("/headers") public ResponseEntity<Map<String, Object>> |
||||
* headersPatch(ServerWebExchange exchange, |
||||
* |
||||
* @RequestBody Map<String, String> headersToAdd) { Map<String, Object> result = new |
||||
* HashMap<>(); result.put("headers", getHeaders(exchange)); |
||||
* ResponseEntity.BodyBuilder responseEntity = ResponseEntity.status(HttpStatus.OK); |
||||
* headersToAdd.forEach(responseEntity::header); |
||||
* |
||||
* return responseEntity.body(result); } |
||||
*/ |
||||
|
||||
@RequestMapping(path = "/multivalueheaders", method = { RequestMethod.GET, RequestMethod.POST }, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> multiValueHeaders(ServerWebExchange exchange) { |
||||
Map<String, Object> result = new HashMap<>(); |
||||
result.put("headers", exchange.getRequest().getHeaders()); |
||||
return result; |
||||
} |
||||
|
||||
@GetMapping(path = "/delay/{sec}/**", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> delay(HttpServletRequest request, @PathVariable int sec) throws InterruptedException { |
||||
int delay = Math.min(sec, 10) * 1000; |
||||
Thread.sleep(delay); |
||||
return get(request); |
||||
} |
||||
|
||||
@GetMapping(path = "/anything/{anything}", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> anything(HttpServletRequest request, @PathVariable(required = false) String anything) { |
||||
return get(request); |
||||
} |
||||
|
||||
@GetMapping(path = "/get", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> get(HttpServletRequest request) { |
||||
if (log.isDebugEnabled()) { |
||||
log.debug("httpbin /get"); |
||||
} |
||||
HashMap<String, Object> result = new HashMap<>(); |
||||
Map<String, String[]> params = request.getParameterMap(); |
||||
result.put("args", params); |
||||
result.put("headers", getHeaders(request)); |
||||
return result; |
||||
} |
||||
|
||||
@PostMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> postFormData(HttpServletRequest request, |
||||
@RequestParam MultiValueMap<String, MultipartFile> parts) throws ServletException, IOException { |
||||
Collection<Part> parts1 = request.getParts(); |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
HashMap<String, Object> files = new HashMap<>(); |
||||
ret.put("files", files); |
||||
|
||||
// StringDecoder decoder = StringDecoder.allMimeTypes(true);
|
||||
parts.values().stream().flatMap(List::stream).forEach(part -> { |
||||
String contentType = part.getContentType(); |
||||
long contentLength = part.getSize(); |
||||
// TODO: get part data
|
||||
files.put(part.getName(), "data:" + contentType + ";base64," + contentLength); |
||||
}); |
||||
return ret; |
||||
} |
||||
|
||||
@PostMapping(path = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> postUrlEncoded(HttpServletRequest request, |
||||
@RequestBody(required = false) MultiValueMap form) throws IOException { |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
ret.put("form", form); |
||||
return ret; |
||||
} |
||||
|
||||
@PostMapping(path = "/post", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> post(HttpServletRequest request, @RequestBody(required = false) String body) { |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
ret.put("data", body); |
||||
HashMap<String, Object> form = new HashMap<>(); |
||||
ret.put("form", form); |
||||
return ret; |
||||
// return exchange.getFormData().flatMap(map -> { for (Map.Entry<String,
|
||||
// List<String>>
|
||||
// entry : map.entrySet()) { for (String value : entry.getValue()) {
|
||||
// form.put(entry.getKey(), value); } } return Mono.just(ret); });
|
||||
} |
||||
|
||||
@GetMapping("/status/{status}") |
||||
public ResponseEntity<String> status(@PathVariable int status) { |
||||
return ResponseEntity.status(status).body("Failed with " + status); |
||||
} |
||||
|
||||
@RequestMapping(value = "/responseheaders/{status}", method = { RequestMethod.GET, RequestMethod.POST }) |
||||
public ResponseEntity<Map<String, Object>> responseHeaders(@PathVariable int status, ServerWebExchange exchange) { |
||||
HttpHeaders httpHeaders = exchange.getRequest().getHeaders().entrySet().stream() |
||||
.filter(entry -> entry.getKey().startsWith("X-Test-")) |
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, |
||||
(list1, list2) -> Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList()), |
||||
HttpHeaders::new)); |
||||
|
||||
return ResponseEntity.status(status).headers(httpHeaders).body(Collections.singletonMap("status", status)); |
||||
} |
||||
|
||||
@PostMapping(path = "/post/empty", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public String emptyResponse() { |
||||
return null; |
||||
} |
||||
|
||||
/* |
||||
* @GetMapping(path = "/gzip", produces = MediaType.APPLICATION_JSON_VALUE) public |
||||
* Mono<Void> gzip(ServerWebExchange exchange) throws IOException { if |
||||
* (log.isDebugEnabled()) { log.debug("httpbin /gzip"); } |
||||
* |
||||
* String jsonResponse = OBJECT_MAPPER.writeValueAsString("httpbin compatible home"); |
||||
* byte[] bytes = jsonResponse.getBytes(StandardCharsets.UTF_8); |
||||
* |
||||
* ServerHttpResponse response = exchange.getResponse(); |
||||
* response.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); DataBufferFactory |
||||
* dataBufferFactory = response.bufferFactory(); |
||||
* response.setStatusCode(HttpStatus.OK); |
||||
* |
||||
* ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream is = new |
||||
* GZIPOutputStream(bos); FileCopyUtils.copy(bytes, is); |
||||
* |
||||
* byte[] gzippedResponse = bos.toByteArray(); DataBuffer wrap = |
||||
* dataBufferFactory.wrap(gzippedResponse); return |
||||
* response.writeWith(Flux.just(wrap)); } |
||||
*/ |
||||
|
||||
@GetMapping("/vary-on-header/**") |
||||
public ResponseEntity<Map<String, Object>> varyOnAccept(HttpServletRequest request, |
||||
@RequestHeader(name = HEADER_REQ_VARY, required = false) String headerToVary) { |
||||
if (headerToVary == null) { |
||||
return ResponseEntity.badRequest().body(Map.of("error", HEADER_REQ_VARY + " header is mandatory")); |
||||
} |
||||
else { |
||||
var builder = ResponseEntity.ok(); |
||||
builder.varyBy(headerToVary); |
||||
return builder.body(headers(request)); |
||||
} |
||||
} |
||||
|
||||
public Map<String, String> getHeaders(HttpServletRequest req) { |
||||
HashMap<String, String> headers = new HashMap<>(); |
||||
Enumeration<String> headerNames = req.getHeaderNames(); |
||||
|
||||
while (headerNames.hasMoreElements()) { |
||||
String name = headerNames.nextElement(); |
||||
String value = null; |
||||
|
||||
Enumeration<String> values = req.getHeaders(name); |
||||
if (values.hasMoreElements()) { |
||||
value = values.nextElement(); |
||||
} |
||||
headers.put(name, value); |
||||
} |
||||
return headers; |
||||
// return request.headers().asHttpHeaders().toSingleValueMap();
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
/* |
||||
* Copyright 2013-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.server.mvc.test; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.testcontainers.containers.GenericContainer; |
||||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
||||
import org.testcontainers.utility.DockerImageName; |
||||
|
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.core.env.MapPropertySource; |
||||
import org.springframework.core.env.MutablePropertySources; |
||||
|
||||
public class HttpbinTestcontainers implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
||||
|
||||
// https://github.com/mccutchen/go-httpbin
|
||||
// https://hub.docker.com/r/mccutchen/go-httpbin
|
||||
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mccutchen/go-httpbin"); |
||||
|
||||
static final Logger logger = LoggerFactory.getLogger(HttpbinTestcontainers.class); |
||||
|
||||
/** |
||||
* Default httpbin port. |
||||
*/ |
||||
public static final int DEFAULT_PORT = 8080; |
||||
|
||||
/** |
||||
* Shared httpbin container. |
||||
*/ |
||||
public static GenericContainer<?> container = createContainer(); |
||||
|
||||
public static GenericContainer<?> createContainer() { |
||||
return new GenericContainer<>(DEFAULT_IMAGE_NAME).withExposedPorts(DEFAULT_PORT) |
||||
.waitingFor(new HttpWaitStrategy().forPort(DEFAULT_PORT)); |
||||
} |
||||
|
||||
@Override |
||||
public void initialize(ConfigurableApplicationContext context) { |
||||
start(); |
||||
|
||||
MutablePropertySources sources = context.getEnvironment().getPropertySources(); |
||||
|
||||
if (!sources.contains("httpbinTestcontainer")) { |
||||
boolean running = container.isRunning(); |
||||
Integer mappedPort = container.getMappedPort(DEFAULT_PORT); |
||||
HashMap<String, Object> map = new HashMap<>(); |
||||
map.put("httpbin.port", String.valueOf(mappedPort)); |
||||
map.put("httpbin.host", container.getHost()); |
||||
|
||||
sources.addFirst(new MapPropertySource("httpbinTestcontainer", map)); |
||||
} |
||||
} |
||||
|
||||
public static void start() { |
||||
if (!container.isRunning()) { |
||||
container.start(); |
||||
} |
||||
} |
||||
|
||||
public static Integer getPort() { |
||||
if (!container.isRunning()) { |
||||
throw new IllegalStateException("httpbin Testcontainer is not running"); |
||||
} |
||||
return container.getMappedPort(DEFAULT_PORT); |
||||
} |
||||
|
||||
public static String getHost() { |
||||
if (!container.isRunning()) { |
||||
throw new IllegalStateException("httpbin Testcontainer is not running"); |
||||
} |
||||
return container.getHost(); |
||||
} |
||||
|
||||
public static void initializeSystemProperties() { |
||||
start(); |
||||
|
||||
System.setProperty("httpbin.port", getPort().toString()); |
||||
System.setProperty("httpbin.host", getHost()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* Copyright 2013-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.server.mvc.test; |
||||
|
||||
import java.net.URI; |
||||
|
||||
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils; |
||||
import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchangeHandlerFunction; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.web.servlet.function.HandlerFilterFunction; |
||||
import org.springframework.web.servlet.function.HandlerFunction; |
||||
import org.springframework.web.servlet.function.ServerRequest; |
||||
import org.springframework.web.servlet.function.ServerResponse; |
||||
|
||||
public class HttpbinUriResolver |
||||
implements ProxyExchangeHandlerFunction.URIResolver, HandlerFilterFunction<ServerResponse, ServerResponse> { |
||||
|
||||
@Override |
||||
public URI apply(ServerRequest request) { |
||||
ApplicationContext context = MvcUtils.getApplicationContext(request); |
||||
Integer port = context.getEnvironment().getProperty("httpbin.port", Integer.class); |
||||
String host = context.getEnvironment().getProperty("httpbin.host"); |
||||
return URI.create(String.format("http://%s:%d", host, port)); |
||||
} |
||||
|
||||
@Override |
||||
public ServerResponse filter(ServerRequest request, HandlerFunction<ServerResponse> next) throws Exception { |
||||
URI uri = apply(request); |
||||
MvcUtils.setRequestUrl(request, uri); |
||||
return next.handle(request); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
/* |
||||
* Copyright 2013-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.server.mvc.test; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Enumeration; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import jakarta.servlet.ServletException; |
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.util.MultiValueMap; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestHeader; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
|
||||
@RestController |
||||
@RequestMapping("/test") |
||||
public class TestController { |
||||
|
||||
private static final String HEADER_REQ_VARY = "X-Request-Vary"; |
||||
|
||||
@GetMapping("/") |
||||
public String home() { |
||||
return "test controller home"; |
||||
} |
||||
|
||||
@RequestMapping(path = "/multivalueheaders", method = { RequestMethod.GET, RequestMethod.POST }, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> multiValueHeaders(ServerWebExchange exchange) { |
||||
Map<String, Object> result = new HashMap<>(); |
||||
result.put("headers", exchange.getRequest().getHeaders()); |
||||
return result; |
||||
} |
||||
|
||||
@PostMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> postFormData(HttpServletRequest request, |
||||
@RequestParam MultiValueMap<String, MultipartFile> parts) throws ServletException, IOException { |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
HashMap<String, Object> files = new HashMap<>(); |
||||
ret.put("files", files); |
||||
|
||||
parts.values().stream().flatMap(List::stream).forEach(part -> { |
||||
String contentType = part.getContentType(); |
||||
long contentLength = part.getSize(); |
||||
// TODO: get part data
|
||||
files.put(part.getName(), "data:" + contentType + ";base64," + contentLength); |
||||
}); |
||||
return ret; |
||||
} |
||||
|
||||
@PostMapping(path = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> postUrlEncoded(HttpServletRequest request, |
||||
@RequestBody(required = false) MultiValueMap form) throws IOException { |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
ret.put("form", form); |
||||
return ret; |
||||
} |
||||
|
||||
@PostMapping(path = "/post", produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public Map<String, Object> post(HttpServletRequest request, @RequestBody(required = false) String body) { |
||||
HashMap<String, Object> ret = new HashMap<>(); |
||||
ret.put("headers", getHeaders(request)); |
||||
ret.put("data", body); |
||||
HashMap<String, Object> form = new HashMap<>(); |
||||
ret.put("form", form); |
||||
return ret; |
||||
} |
||||
|
||||
@GetMapping("/vary-on-header/**") |
||||
public ResponseEntity<Map<String, Object>> varyOnAccept(HttpServletRequest request, |
||||
@RequestHeader(name = HEADER_REQ_VARY, required = false) String headerToVary) { |
||||
if (headerToVary == null) { |
||||
return ResponseEntity.badRequest().body(Map.of("error", HEADER_REQ_VARY + " header is mandatory")); |
||||
} |
||||
else { |
||||
var builder = ResponseEntity.ok(); |
||||
builder.varyBy(headerToVary); |
||||
Map<String, Object> result = new HashMap<>(); |
||||
result.put("headers", getHeaders(request)); |
||||
return builder.body(result); |
||||
} |
||||
} |
||||
|
||||
public Map<String, String> getHeaders(HttpServletRequest req) { |
||||
HashMap<String, String> headers = new HashMap<>(); |
||||
Enumeration<String> headerNames = req.getHeaderNames(); |
||||
|
||||
while (headerNames.hasMoreElements()) { |
||||
String name = headerNames.nextElement(); |
||||
String value = null; |
||||
|
||||
Enumeration<String> values = req.getHeaders(name); |
||||
if (values.hasMoreElements()) { |
||||
value = values.nextElement(); |
||||
} |
||||
headers.put(name, value); |
||||
} |
||||
return headers; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* Copyright 2013-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.server.mvc.test; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
public class TestUtils { |
||||
|
||||
public static Map<String, Object> getMap(Map<String, Object> map, String mapKey) { |
||||
assertThat(map).isNotEmpty().containsKey(mapKey); |
||||
Map<String, Object> headers = (Map<String, Object>) map.get(mapKey); |
||||
Map<String, Object> newHeaders = new HashMap<>(); |
||||
|
||||
// flatten map as mccutchen/go-httpbin only gets lists
|
||||
headers.forEach((key, value) -> { |
||||
if (value instanceof List<?> values) { |
||||
if (values.size() == 1) { |
||||
newHeaders.put(key, values.get(0)); |
||||
} |
||||
else { |
||||
newHeaders.put(key, values); |
||||
} |
||||
} |
||||
else { |
||||
newHeaders.put(key, value); |
||||
} |
||||
}); |
||||
return newHeaders; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue