完成路由测试
This commit is contained in:
parent
0704f177bf
commit
818a03ecd9
10
pom.xml
10
pom.xml
|
@ -65,6 +65,14 @@
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
|
<artifactId>wiremock-standalone</artifactId>
|
||||||
|
<version>2.23.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -208,8 +216,6 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
|
@ -1,7 +1,11 @@
|
||||||
package ocean.gateway.service.routes;
|
package ocean.gateway.service.routes;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.springframework.boot.SpringBootConfiguration;
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
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.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@ -11,10 +15,16 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
@SpringBootConfiguration
|
@SpringBootConfiguration
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Business {
|
public class Business {
|
||||||
|
|
||||||
|
protected static AsyncBuilder helloRoute(PredicateSpec r) {
|
||||||
|
AsyncBuilder builder = r.path("/hello").uri("http://localhost:3030/hello");
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
||||||
log.trace("path_route");
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class TokenFilterTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestRestTemplate restTemplate ;
|
private TestRestTemplate restTemplate ;
|
||||||
|
|
||||||
private WebTestClient requests;
|
// private WebTestClient requests;
|
||||||
|
|
||||||
String baseUri;
|
String baseUri;
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ public class TokenFilterTests {
|
||||||
public void GetHelloWithToken() {
|
public void GetHelloWithToken() {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
String key = TokenFilter.TEST_TOKEN;
|
String key = TokenFilter.TEST_TOKEN;
|
||||||
|
|
||||||
headers.add("token",key);
|
headers.add("token",key);
|
||||||
HttpEntity<String> entity = new HttpEntity<>("", headers);
|
HttpEntity<String> entity = new HttpEntity<>("", headers);
|
||||||
|
|
||||||
|
|
|
@ -1,42 +1,39 @@
|
||||||
package ocean.gateway.routes;
|
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.After;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Rule;
|
||||||
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.SpringBootConfiguration;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
import org.springframework.cloud.gateway.route.Route;
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
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 ocean.gateway.service.ServiceApplication;
|
||||||
import reactor.core.publisher.Flux;
|
import ocean.gateway.service.filters.TokenFilter;
|
||||||
import reactor.core.publisher.Mono;
|
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)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = { ServiceApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = { ServiceApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
@ -51,44 +48,38 @@ public class BusinessTests {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public HttpServer server;
|
@LocalServerPort
|
||||||
private String serverPort;
|
public int randomServerPort;
|
||||||
DisposableServer ds;
|
public String baseUri;
|
||||||
|
|
||||||
// @BeforeClass
|
@Rule
|
||||||
// public void setup(){
|
public WireMockRule routeTestServer = new WireMockRule(3030);
|
||||||
// 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");
|
@Before
|
||||||
// }
|
public void before() {
|
||||||
|
|
||||||
// @AfterClass
|
baseUri = "http://127.0.0.1:" + randomServerPort;
|
||||||
// public void stop() {
|
routeTestServer.stubFor(get(urlPathEqualTo("/hello")).willReturn(
|
||||||
// if(server != null) {
|
aResponse().withStatus(200).withBody("hello route")));
|
||||||
// ds.dispose();
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Autowired
|
@After
|
||||||
private TestRestTemplate restTemplate ;
|
public void after() {
|
||||||
|
routeTestServer.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestRouteLocator() {
|
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();
|
||||||
|
|
||||||
|
Assert.hasText(content, "hello route");
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUri +"/test123", String.class);
|
|
||||||
Assert.notNull(response, "The class must not be null");
|
Assert.notNull(response, "The class must not be null");
|
||||||
Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user