diff --git a/src/test/java/ocean/gateway/filters/TokenFilterTests.java b/src/test/java/ocean/gateway/filters/TokenFilterTests.java index 1f91c84..c7db4df 100644 --- a/src/test/java/ocean/gateway/filters/TokenFilterTests.java +++ b/src/test/java/ocean/gateway/filters/TokenFilterTests.java @@ -19,6 +19,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.util.Assert; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.WebClient; import ocean.gateway.service.ServiceApplication; import ocean.gateway.service.filters.TokenFilter; @@ -32,36 +34,34 @@ public class TokenFilterTests { @LocalServerPort int randomServerPort; - @Autowired - private TestRestTemplate restTemplate ; - // private WebTestClient requests; String baseUri; + WebClient request; + + @Before + public void before() { + + request = WebClient.create(); + baseUri = "http://127.0.0.1:" + randomServerPort; + } - @Before - public void setup() { - baseUri = "http://localhost:" + randomServerPort; - } @Test public void GetHelloWithoutToken() { - ResponseEntity response = restTemplate.getForEntity(baseUri +"/hello", String.class); + ClientResponse response = request.get().uri(baseUri + "/hello").exchange().block(); + + 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 be HttpStatus.UNAUTHORIZED"); } @Test public void GetHelloWithToken() { - HttpHeaders headers = new HttpHeaders(); - String key = TokenFilter.TEST_TOKEN; - headers.add("token",key); - HttpEntity entity = new HttpEntity<>("", headers); + ClientResponse response = request.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block(); - - ResponseEntity response = restTemplate.exchange(baseUri + "/hello", HttpMethod.GET, entity, String.class); Assert.notNull(response, "The class must not be null"); - Assert.state(response.getStatusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED"); + Assert.state(response.statusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED"); } } \ No newline at end of file diff --git a/src/test/java/ocean/gateway/routes/BusinessTests.java b/src/test/java/ocean/gateway/routes/BusinessTests.java index 6aceec5..b289836 100644 --- a/src/test/java/ocean/gateway/routes/BusinessTests.java +++ b/src/test/java/ocean/gateway/routes/BusinessTests.java @@ -39,26 +39,19 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; @SpringBootTest(classes = { ServiceApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class BusinessTests { - // @RestController - // @RequestMapping(value = "/my") - // public class MyController { - // @RequestMapping(value = "/integration/{name}", method = RequestMethod.GET) - // public String integrationTest(@PathVariable String name) { - // return "name:" + name; - // } - // } - @LocalServerPort public int randomServerPort; public String baseUri; @Rule public WireMockRule routeTestServer = new WireMockRule(3030); - + WebClient request; @Before public void before() { + request = WebClient.create(); baseUri = "http://127.0.0.1:" + randomServerPort; + routeTestServer.stubFor(get(urlPathEqualTo("/hello")).willReturn( aResponse().withStatus(200).withBody("hello route"))); } @@ -71,35 +64,17 @@ public class BusinessTests { @Test public void TestRouteLocator() { - - WebClient cli = WebClient.create(); - ClientResponse response = cli.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block(); + ClientResponse response = request.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block(); String content = response.bodyToMono(String.class).block(); - 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)); + Assert.hasText(content, "hello route"); } - // @Configuration - // public class HttpServerConfig { - // @Autowired - // private Environment environment; - // private String serverPort; - // @Bean - // public HttpServer httpServer() { - // HttpServer server = HttpServer.create().host("http://localhost:" + serverPort); - // server.handle((req, res) -> res.sendString(Flux.just("HelloTest"))); - // return server; - // } - // } - - - - }