Add custom HandlerMapping to allow more flexible request mapping

This commit is contained in:
Dave Syer
2017-03-31 13:22:48 +01:00
parent 787ab65d55
commit cadd5546da
8 changed files with 389 additions and 75 deletions

View File

@@ -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" });
}
}
}

View File

@@ -26,12 +26,13 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
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.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -64,6 +65,16 @@ public class RestApplicationTests {
@Autowired
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
public void wordsSSE() throws Exception {
assertThat(rest.exchange(
@@ -96,6 +107,14 @@ public class RestApplicationTests {
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
public void updates() throws Exception {
ResponseEntity<String> result = rest.exchange(
@@ -165,6 +184,23 @@ public class RestApplicationTests {
.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
public void uppercaseGet() {
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("[FOO]");
@@ -196,12 +232,14 @@ public class RestApplicationTests {
@Test
public void uppercaseJsonStream() throws Exception {
assertThat(rest
.exchange(RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
// TODO: make this work without newline separator
.body("{\"value\":\"foo\"}\n{\"value\":\"bar\"}"), String.class)
.getBody()).isEqualTo("{\"value\":\"FOO\"}{\"value\":\"BAR\"}");
assertThat(
rest.exchange(
RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
// TODO: make this work without newline separator
.body("{\"value\":\"foo\"}\n{\"value\":\"bar\"}"),
String.class).getBody())
.isEqualTo("{\"value\":\"FOO\"}{\"value\":\"BAR\"}");
}
@Test
@@ -217,12 +255,13 @@ public class RestApplicationTests {
return "data:" + StringUtils.arrayToDelimitedString(values, "\n\ndata:") + "\n\n";
}
@SpringBootApplication
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
public static class TestConfiguration {
private List<String> list = new ArrayList<>();
@Bean
@Bean({ "uppercase", "transform", "post/more" })
public Function<Flux<String>, Flux<String>> uppercase() {
return flux -> flux.log()
.map(value -> "[" + value.trim().toUpperCase() + "]");
@@ -247,7 +286,7 @@ public class RestApplicationTests {
});
}
@Bean
@Bean({ "words", "get/more" })
public Supplier<Flux<String>> words() {
return () -> Flux.fromArray(new String[] { "foo", "bar" });
}

View File

@@ -0,0 +1 @@
<html><body>Test</body></html>