diff --git a/pom.xml b/pom.xml
index 04002c8..c1f03ce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -65,6 +65,14 @@
org.projectlombok
lombok
+
+
+ com.github.tomakehurst
+ wiremock-standalone
+ 2.23.2
+ test
+
+
@@ -207,8 +215,6 @@
-
-
diff --git a/src/main/java/ocean/gateway/service/routes/Business.java b/src/main/java/ocean/gateway/service/routes/Business.java
index 1b6aa94..785db34 100644
--- a/src/main/java/ocean/gateway/service/routes/Business.java
+++ b/src/main/java/ocean/gateway/service/routes/Business.java
@@ -1,7 +1,11 @@
package ocean.gateway.service.routes;
+import java.util.function.Function;
+
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cloud.gateway.route.RouteLocator;
+import org.springframework.cloud.gateway.route.Route.AsyncBuilder;
+import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@@ -11,10 +15,16 @@ import lombok.extern.slf4j.Slf4j;
@SpringBootConfiguration
@Slf4j
public class Business {
+
+ protected static AsyncBuilder helloRoute(PredicateSpec r) {
+ AsyncBuilder builder = r.path("/hello").uri("http://localhost:3030/hello");
+ return builder;
+ }
+
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
log.trace("path_route");
- return builder.routes().route("path_route", r -> r.path("/hello").uri("http://localhost:3030/hello")).build();
+ return builder.routes().route( Business::helloRoute ).build();
}
diff --git a/src/test/java/ocean/gateway/filters/TokenFilterTests.java b/src/test/java/ocean/gateway/filters/TokenFilterTests.java
index c13cb70..1f91c84 100644
--- a/src/test/java/ocean/gateway/filters/TokenFilterTests.java
+++ b/src/test/java/ocean/gateway/filters/TokenFilterTests.java
@@ -35,7 +35,7 @@ public class TokenFilterTests {
@Autowired
private TestRestTemplate restTemplate ;
- private WebTestClient requests;
+ // private WebTestClient requests;
String baseUri;
@@ -56,7 +56,6 @@ public class TokenFilterTests {
public void GetHelloWithToken() {
HttpHeaders headers = new HttpHeaders();
String key = TokenFilter.TEST_TOKEN;
-
headers.add("token",key);
HttpEntity entity = new HttpEntity<>("", headers);
diff --git a/src/test/java/ocean/gateway/routes/BusinessTests.java b/src/test/java/ocean/gateway/routes/BusinessTests.java
index cc03bcb..6aceec5 100644
--- a/src/test/java/ocean/gateway/routes/BusinessTests.java
+++ b/src/test/java/ocean/gateway/routes/BusinessTests.java
@@ -1,42 +1,39 @@
package ocean.gateway.routes;
-import static org.junit.Assert.assertThat;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
-import java.net.URI;
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.After;
-import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.cloud.gateway.route.Route;
-import org.springframework.cloud.gateway.route.RouteLocator;
-import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.core.env.Environment;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
-import org.springframework.http.server.reactive.HttpHandler;
-import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
-import org.springframework.web.reactive.function.server.RouterFunction;
-import org.springframework.web.reactive.function.server.RouterFunctions;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
-import lombok.extern.slf4j.Slf4j;
import ocean.gateway.service.ServiceApplication;
-import reactor.core.publisher.Flux;
+import ocean.gateway.service.filters.TokenFilter;
import reactor.core.publisher.Mono;
-import reactor.netty.DisposableServer;
-import reactor.netty.http.server.HttpServer;
-import reactor.netty.http.server.HttpServerRequest;
+import static com.github.tomakehurst.wiremock.client.WireMock.any;
+import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ServiceApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@@ -51,44 +48,38 @@ public class BusinessTests {
// }
// }
- public HttpServer server;
- private String serverPort;
- DisposableServer ds;
+ @LocalServerPort
+ public int randomServerPort;
+ public String baseUri;
- // @BeforeClass
- // public void setup(){
- // serverPort = "3023";
- // server = HttpServer.create().host("http://localhost").port(3023);
- // server.route(routes -> routes.get("/test123", (req, res) -> res.sendString(Flux.just("test123"))));
- // ds = server.bind().block();
-
- // System.out.println("123");
- // }
+ @Rule
+ public WireMockRule routeTestServer = new WireMockRule(3030);
- // @AfterClass
- // public void stop() {
- // if(server != null) {
- // ds.dispose();
- // }
- // }
+ @Before
+ public void before() {
- @Autowired
- private TestRestTemplate restTemplate ;
+ baseUri = "http://127.0.0.1:" + randomServerPort;
+ routeTestServer.stubFor(get(urlPathEqualTo("/hello")).willReturn(
+ aResponse().withStatus(200).withBody("hello route")));
+ }
+
+ @After
+ public void after() {
+ routeTestServer.shutdown();
+ }
@Test
public void TestRouteLocator() {
- serverPort = "3023";
- server = HttpServer.create().host("http://localhost").port(3023);
- server.route(routes -> routes.get("/test123", (req, res) -> res.sendString(Flux.just("test123"))));
- server.bind().block();
- String baseUri = "http://127.0.0.1:3023";
+ WebClient cli = WebClient.create();
+ ClientResponse response = cli.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block();
+ String content = response.bodyToMono(String.class).block();
-
- ResponseEntity response = restTemplate.getForEntity(baseUri +"/test123", String.class);
- Assert.notNull(response, "The class must not be null");
- Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
+ Assert.hasText(content, "hello route");
+ Assert.notNull(response, "The class must not be null");
+ Assert.state(response.statusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED");
+ Assert.state(response.statusCode() == HttpStatus.OK, String.format("the statusCode is %s, not is %s\n", response.statusCode(), HttpStatus.OK));
}