完成路由测试

This commit is contained in:
huangsimin 2019-06-03 18:35:28 +08:00
parent 0704f177bf
commit 818a03ecd9
4 changed files with 59 additions and 53 deletions

10
pom.xml
View File

@ -65,6 +65,14 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.23.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
@ -207,8 +215,6 @@
</executions>
</plugin>
</plugins>
</build>

View File

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

View File

@ -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<String> entity = new HttpEntity<>("", headers);

View File

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