Add custom HandlerMapping to allow more flexible request mapping
This commit is contained in:
@@ -98,56 +98,80 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
Map<String, Supplier<?>> suppliers) {
|
Map<String, Supplier<?>> suppliers) {
|
||||||
Map<String, Supplier<?>> result = new HashMap<>();
|
Map<String, Supplier<?>> result = new HashMap<>();
|
||||||
for (String key : suppliers.keySet()) {
|
for (String key : suppliers.keySet()) {
|
||||||
if (this.suppliers.contains(key)) {
|
Supplier<?> target = target(suppliers.get(key), mapper, key);
|
||||||
@SuppressWarnings("unchecked")
|
result.put(key, target);
|
||||||
Supplier<Flux<?>> supplier = (Supplier<Flux<?>>) suppliers.get(key);
|
for (String name : registry.getAliases(key)) {
|
||||||
result.put(key, wrapSupplier(supplier, mapper, key));
|
result.put(name, target);
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.put(key, suppliers.get(key));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Supplier<?> target(Supplier<?> target, ObjectMapper mapper, String key) {
|
||||||
|
if (this.suppliers.contains(key)) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Supplier<Flux<?>> supplier = (Supplier<Flux<?>>) target;
|
||||||
|
return wrapSupplier(supplier, mapper, key);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Function<?, ?>> wrapFunctions(ObjectMapper mapper,
|
public Map<String, Function<?, ?>> wrapFunctions(ObjectMapper mapper,
|
||||||
Map<String, Function<?, ?>> functions) {
|
Map<String, Function<?, ?>> functions) {
|
||||||
Map<String, Function<?, ?>> result = new HashMap<>();
|
Map<String, Function<?, ?>> result = new HashMap<>();
|
||||||
for (String key : functions.keySet()) {
|
for (String key : functions.keySet()) {
|
||||||
if (this.functions.contains(key)) {
|
Function<?, ?> target = target(functions.get(key), mapper, key);
|
||||||
@SuppressWarnings("unchecked")
|
result.put(key, target);
|
||||||
Function<Flux<?>, Flux<?>> function = (Function<Flux<?>, Flux<?>>) functions
|
for (String name : registry.getAliases(key)) {
|
||||||
.get(key);
|
result.put(name, target);
|
||||||
result.put(key, wrapFunction(function, mapper, key));
|
|
||||||
}
|
|
||||||
else if (!isFluxFunction(key, functions.get(key))) {
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
FluxFunction value = new FluxFunction(functions.get(key));
|
|
||||||
result.put(key, value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.put(key, functions.get(key));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Function<?, ?> target(Function<?, ?> target, ObjectMapper mapper,
|
||||||
|
String key) {
|
||||||
|
if (this.functions.contains(key)) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Function<Flux<?>, Flux<?>> function = (Function<Flux<?>, Flux<?>>) target;
|
||||||
|
return wrapFunction(function, mapper, key);
|
||||||
|
}
|
||||||
|
else if (!isFluxFunction(key, target)) {
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
FluxFunction value = new FluxFunction(target);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Consumer<?>> wrapConsumers(ObjectMapper mapper,
|
public Map<String, Consumer<?>> wrapConsumers(ObjectMapper mapper,
|
||||||
Map<String, Consumer<?>> consumers) {
|
Map<String, Consumer<?>> consumers) {
|
||||||
Map<String, Consumer<?>> result = new HashMap<>();
|
Map<String, Consumer<?>> result = new HashMap<>();
|
||||||
for (String key : consumers.keySet()) {
|
for (String key : consumers.keySet()) {
|
||||||
if (this.consumers.contains(key)) {
|
Consumer<?> target = target(consumers.get(key), mapper, key);
|
||||||
@SuppressWarnings("unchecked")
|
result.put(key, target);
|
||||||
Consumer<Flux<?>> consumer = (Consumer<Flux<?>>) consumers.get(key);
|
for (String name : registry.getAliases(key)) {
|
||||||
result.put(key, wrapConsumer(consumer, mapper, key));
|
result.put(name, target);
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.put(key, consumers.get(key));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Consumer<?> target(Consumer<?> target, ObjectMapper mapper, String key) {
|
||||||
|
if (this.consumers.contains(key)) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Consumer<Flux<?>> consumer = (Consumer<Flux<?>>) target;
|
||||||
|
return wrapConsumer(consumer, mapper, key);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
|
||||||
throws BeansException {
|
throws BeansException {
|
||||||
|
|||||||
@@ -20,20 +20,18 @@ import java.util.function.Consumer;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
||||||
import org.springframework.cloud.function.registry.FunctionCatalog;
|
|
||||||
import org.springframework.cloud.function.support.FluxSupplier;
|
import org.springframework.cloud.function.support.FluxSupplier;
|
||||||
import org.springframework.cloud.function.support.FunctionUtils;
|
import org.springframework.cloud.function.support.FunctionUtils;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestAttribute;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@@ -42,46 +40,46 @@ import reactor.core.publisher.Mono;
|
|||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
@RestController
|
@Component
|
||||||
@ConditionalOnClass(RestController.class)
|
|
||||||
@RequestMapping("${spring.cloud.function.web.path:}")
|
|
||||||
public class FunctionController {
|
public class FunctionController {
|
||||||
|
|
||||||
@Value("${debug:${DEBUG:false}}")
|
@Value("${debug:${DEBUG:false}}")
|
||||||
private boolean debug = false;
|
private boolean debug = false;
|
||||||
|
|
||||||
private final FunctionCatalog functions;
|
@PostMapping(path = "/**")
|
||||||
|
@ResponseBody
|
||||||
@Autowired
|
public ResponseEntity<Flux<String>> post(
|
||||||
public FunctionController(FunctionCatalog catalog) {
|
@RequestAttribute(required = false, name = "org.springframework.cloud.function.web.FunctionHandlerMapping.function") Function<Flux<?>, Flux<?>> function,
|
||||||
this.functions = catalog;
|
@RequestAttribute(required = false, name = "org.springframework.cloud.function.web.FunctionHandlerMapping.consumer") Consumer<Flux<?>> consumer,
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(path = "/{name}")
|
|
||||||
public ResponseEntity<Flux<String>> function(@PathVariable String name,
|
|
||||||
@RequestBody Flux<String> body) {
|
@RequestBody Flux<String> body) {
|
||||||
Function<Flux<?>, Flux<?>> function = functions.lookupFunction(name);
|
|
||||||
if (function != null) {
|
if (function != null) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Flux<String> result = (Flux<String>) function.apply(body);
|
Flux<String> result = (Flux<String>) function.apply(body);
|
||||||
return ResponseEntity.ok().body(debug ? result.log() : result);
|
return ResponseEntity.ok().body(debug ? result.log() : result);
|
||||||
}
|
}
|
||||||
Consumer<Flux<?>> consumer = functions.lookupConsumer(name);
|
|
||||||
if (consumer != null) {
|
if (consumer != null) {
|
||||||
body = body.cache(); // send a copy back to the caller
|
body = body.cache(); // send a copy back to the caller
|
||||||
consumer.accept(body);
|
consumer.accept(body);
|
||||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(body);
|
return ResponseEntity.status(HttpStatus.ACCEPTED).body(body);
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("no such function: " + name);
|
throw new IllegalArgumentException("no such function");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/{name}")
|
@GetMapping(path = "/**")
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@ResponseBody
|
||||||
public Flux<String> supplier(@PathVariable String name) {
|
public Object get(
|
||||||
Supplier<Object> supplier = functions.lookupSupplier(name);
|
@RequestAttribute(required = false, name = "org.springframework.cloud.function.web.FunctionHandlerMapping.function") Function<Flux<?>, Flux<?>> function,
|
||||||
if (supplier == null) {
|
@RequestAttribute(required = false, name = "org.springframework.cloud.function.web.FunctionHandlerMapping.supplier") Supplier<Flux<?>> supplier,
|
||||||
throw new IllegalArgumentException("no such supplier: " + name);
|
@RequestAttribute(required = false, name = "org.springframework.cloud.function.web.FunctionHandlerMapping.argument") String argument,
|
||||||
|
@RequestAttribute("org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping") String path) {
|
||||||
|
if (function != null) {
|
||||||
|
return value(function, argument);
|
||||||
}
|
}
|
||||||
|
return supplier(supplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
private Flux<String> supplier(Supplier<Flux<?>> supplier) {
|
||||||
if (!FunctionUtils.isFluxSupplier(supplier)) {
|
if (!FunctionUtils.isFluxSupplier(supplier)) {
|
||||||
supplier = new FluxSupplier(supplier);
|
supplier = new FluxSupplier(supplier);
|
||||||
}
|
}
|
||||||
@@ -89,15 +87,10 @@ public class FunctionController {
|
|||||||
return debug ? result.log() : result;
|
return debug ? result.log() : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/{name}/{value}")
|
private Mono<String> value(Function<Flux<?>, Flux<?>> function,
|
||||||
public Mono<String> value(@PathVariable String name, @PathVariable String value) {
|
@PathVariable String value) {
|
||||||
Function<Flux<?>, Flux<?>> function = functions.lookupFunction(name);
|
@SuppressWarnings({ "unchecked" })
|
||||||
if (function != null) {
|
Mono<String> result = Mono.from((Flux<String>) function.apply(Flux.just(value)));
|
||||||
@SuppressWarnings({ "unchecked" })
|
return debug ? result.log() : result;
|
||||||
Mono<String> result = Mono
|
|
||||||
.from((Flux<String>) function.apply(Flux.just(value)));
|
|
||||||
return debug ? result.log() : result;
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("no such function: " + name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2015 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
|
||||||
|
*
|
||||||
|
* http://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.function.web;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.cloud.function.registry.FunctionCatalog;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(RequestMappingHandlerMapping.class)
|
||||||
|
public class FunctionHandlerMapping extends RequestMappingHandlerMapping
|
||||||
|
implements InitializingBean {
|
||||||
|
|
||||||
|
public static final String FUNCTION = FunctionHandlerMapping.class.getName()
|
||||||
|
+ ".function";
|
||||||
|
public static final String CONSUMER = FunctionHandlerMapping.class.getName()
|
||||||
|
+ ".consumer";
|
||||||
|
public static final String SUPPLIER = FunctionHandlerMapping.class.getName()
|
||||||
|
+ ".supplier";
|
||||||
|
public static final String ARGUMENT = FunctionHandlerMapping.class.getName()
|
||||||
|
+ ".argument";
|
||||||
|
private final FunctionCatalog functions;
|
||||||
|
|
||||||
|
private final FunctionController controller;
|
||||||
|
|
||||||
|
@Value("${spring.cloud.function.web.path:}")
|
||||||
|
private String prefix = "";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public FunctionHandlerMapping(FunctionCatalog catalog) {
|
||||||
|
this.functions = catalog;
|
||||||
|
setOrder(super.getOrder() - 5);
|
||||||
|
this.controller = new FunctionController();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
super.afterPropertiesSet();
|
||||||
|
detectHandlerMethods(controller);
|
||||||
|
while (prefix.endsWith("/")) {
|
||||||
|
prefix = prefix.substring(0, prefix.length() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected HandlerMethod getHandlerInternal(HttpServletRequest request)
|
||||||
|
throws Exception {
|
||||||
|
HandlerMethod handler = super.getHandlerInternal(request);
|
||||||
|
if (handler == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String path = (String) request
|
||||||
|
.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||||
|
if (path == null) {
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
if (findFunctionForGet(request, path) != null) {
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
if (findFunctionForPost(request, path) != null) {
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object findFunctionForPost(HttpServletRequest request, String path) {
|
||||||
|
if (!request.getMethod().equals("POST")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
path = path.startsWith("/") ? path.substring(1) : path;
|
||||||
|
Consumer<Object> consumer = functions.lookupConsumer(path);
|
||||||
|
if (consumer != null) {
|
||||||
|
request.setAttribute(CONSUMER, consumer);
|
||||||
|
return consumer;
|
||||||
|
}
|
||||||
|
Function<Object, Object> function = functions.lookupFunction(path);
|
||||||
|
if (function != null) {
|
||||||
|
request.setAttribute(FUNCTION, function);
|
||||||
|
return function;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object findFunctionForGet(HttpServletRequest request, String path) {
|
||||||
|
if (!request.getMethod().equals("GET")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
path = path.startsWith("/") ? path.substring(1) : path;
|
||||||
|
Supplier<Object> supplier = functions.lookupSupplier(path);
|
||||||
|
if (supplier != null) {
|
||||||
|
request.setAttribute(SUPPLIER, supplier);
|
||||||
|
return supplier;
|
||||||
|
}
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
String name = path;
|
||||||
|
String value = null;
|
||||||
|
for (String element : path.split("/")) {
|
||||||
|
if (builder.length() > 0) {
|
||||||
|
builder.append("/");
|
||||||
|
}
|
||||||
|
builder.append(element);
|
||||||
|
name = builder.toString();
|
||||||
|
value = path.length() > name.length() ? path.substring(name.length() + 1)
|
||||||
|
: null;
|
||||||
|
Function<Object, Object> function = functions.lookupFunction(name);
|
||||||
|
if (function != null) {
|
||||||
|
request.setAttribute(FUNCTION, function);
|
||||||
|
request.setAttribute(ARGUMENT, value);
|
||||||
|
return function;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2015 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
|
||||||
|
*
|
||||||
|
* http://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.function.web;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface FunctionMapping {
|
||||||
|
|
||||||
|
String name() default "";
|
||||||
|
|
||||||
|
@AliasFor("path")
|
||||||
|
String[] value() default {};
|
||||||
|
|
||||||
|
@AliasFor("value")
|
||||||
|
String[] path() default {};}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
org.springframework.cloud.function.web.FunctionController,\
|
org.springframework.cloud.function.web.FunctionHandlerMapping,\
|
||||||
org.springframework.cloud.function.web.flux.ReactorAutoConfiguration
|
org.springframework.cloud.function.web.flux.ReactorAutoConfiguration
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2015 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
|
||||||
|
*
|
||||||
|
* http://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.function.web;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.RequestEntity;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.cloud.function.web.path=/functions")
|
||||||
|
public class PrefixTests {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate rest;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void words() throws Exception {
|
||||||
|
ResponseEntity<String> result = rest
|
||||||
|
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
|
||||||
|
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(result.getBody()).isEqualTo("foobar");
|
||||||
|
}
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@org.springframework.boot.test.context.TestConfiguration
|
||||||
|
protected static class TestConfiguration {
|
||||||
|
@Bean({ "words", "get/more" })
|
||||||
|
public Supplier<Flux<String>> words() {
|
||||||
|
return () -> Flux.fromArray(new String[] { "foo", "bar" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,12 +26,13 @@ import java.util.function.Consumer;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
@@ -64,6 +65,16 @@ public class RestApplicationTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TestConfiguration test;
|
private TestConfiguration test;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
test.list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void staticResource() throws Exception {
|
||||||
|
assertThat(rest.getForObject("/test.html", String.class)).contains("<body>Test");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void wordsSSE() throws Exception {
|
public void wordsSSE() throws Exception {
|
||||||
assertThat(rest.exchange(
|
assertThat(rest.exchange(
|
||||||
@@ -96,6 +107,14 @@ public class RestApplicationTests {
|
|||||||
assertThat(result.getBody()).isEqualTo("foobar");
|
assertThat(result.getBody()).isEqualTo("foobar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getMore() throws Exception {
|
||||||
|
ResponseEntity<String> result = rest
|
||||||
|
.exchange(RequestEntity.get(new URI("/get/more")).build(), String.class);
|
||||||
|
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(result.getBody()).isEqualTo("foobar");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updates() throws Exception {
|
public void updates() throws Exception {
|
||||||
ResponseEntity<String> result = rest.exchange(
|
ResponseEntity<String> result = rest.exchange(
|
||||||
@@ -165,6 +184,23 @@ public class RestApplicationTests {
|
|||||||
.isEqualTo("[FOO][BAR]");
|
.isEqualTo("[FOO][BAR]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void transform() {
|
||||||
|
assertThat(rest.postForObject("/transform", "foo\nbar", String.class))
|
||||||
|
.isEqualTo("[FOO][BAR]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postMore() {
|
||||||
|
assertThat(rest.postForObject("/post/more", "foo\nbar", String.class))
|
||||||
|
.isEqualTo("[FOO][BAR]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postMoreFoo() {
|
||||||
|
assertThat(rest.getForObject("/post/more/foo", String.class)).isEqualTo("[FOO]");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uppercaseGet() {
|
public void uppercaseGet() {
|
||||||
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("[FOO]");
|
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("[FOO]");
|
||||||
@@ -196,12 +232,14 @@ public class RestApplicationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uppercaseJsonStream() throws Exception {
|
public void uppercaseJsonStream() throws Exception {
|
||||||
assertThat(rest
|
assertThat(
|
||||||
.exchange(RequestEntity.post(new URI("/maps"))
|
rest.exchange(
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
RequestEntity.post(new URI("/maps"))
|
||||||
// TODO: make this work without newline separator
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body("{\"value\":\"foo\"}\n{\"value\":\"bar\"}"), String.class)
|
// TODO: make this work without newline separator
|
||||||
.getBody()).isEqualTo("{\"value\":\"FOO\"}{\"value\":\"BAR\"}");
|
.body("{\"value\":\"foo\"}\n{\"value\":\"bar\"}"),
|
||||||
|
String.class).getBody())
|
||||||
|
.isEqualTo("{\"value\":\"FOO\"}{\"value\":\"BAR\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -217,12 +255,13 @@ public class RestApplicationTests {
|
|||||||
return "data:" + StringUtils.arrayToDelimitedString(values, "\n\ndata:") + "\n\n";
|
return "data:" + StringUtils.arrayToDelimitedString(values, "\n\ndata:") + "\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@SpringBootApplication
|
@EnableAutoConfiguration
|
||||||
|
@org.springframework.boot.test.context.TestConfiguration
|
||||||
public static class TestConfiguration {
|
public static class TestConfiguration {
|
||||||
|
|
||||||
private List<String> list = new ArrayList<>();
|
private List<String> list = new ArrayList<>();
|
||||||
|
|
||||||
@Bean
|
@Bean({ "uppercase", "transform", "post/more" })
|
||||||
public Function<Flux<String>, Flux<String>> uppercase() {
|
public Function<Flux<String>, Flux<String>> uppercase() {
|
||||||
return flux -> flux.log()
|
return flux -> flux.log()
|
||||||
.map(value -> "[" + value.trim().toUpperCase() + "]");
|
.map(value -> "[" + value.trim().toUpperCase() + "]");
|
||||||
@@ -247,7 +286,7 @@ public class RestApplicationTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean({ "words", "get/more" })
|
||||||
public Supplier<Flux<String>> words() {
|
public Supplier<Flux<String>> words() {
|
||||||
return () -> Flux.fromArray(new String[] { "foo", "bar" });
|
return () -> Flux.fromArray(new String[] { "foo", "bar" });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
<html><body>Test</body></html>
|
||||||
Reference in New Issue
Block a user