还没找到httpserver 启动方法

This commit is contained in:
eson 2019-06-03 02:24:33 +08:00
parent a9681f1a39
commit 0704f177bf
3 changed files with 108 additions and 6 deletions

View File

@ -206,6 +206,10 @@
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,7 +1,9 @@
package ocean.gateway.filters;
import java.lang.reflect.Field;
import java.time.Duration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,6 +17,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
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 ocean.gateway.service.ServiceApplication;
@ -32,17 +35,25 @@ public class TokenFilterTests {
@Autowired
private TestRestTemplate restTemplate ;
private WebTestClient requests;
String baseUri;
@Before
public void setup() {
baseUri = "http://localhost:" + randomServerPort;
}
@Test
public void GetHelloWithoutToken() {
ResponseEntity<String> response = restTemplate.getForEntity("http://127.0.0.1:" + randomServerPort +"/hello", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(baseUri +"/hello", String.class);
Assert.notNull(response, "The class must not be null");
Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
}
@Test
public void GetHelloWithToken() {
String uri = "http://127.0.0.1:" + randomServerPort +"/hello";
HttpHeaders headers = new HttpHeaders();
String key = TokenFilter.TEST_TOKEN;
@ -50,7 +61,7 @@ public class TokenFilterTests {
HttpEntity<String> entity = new HttpEntity<>("", headers);
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
ResponseEntity<String> 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");
}

View File

@ -1,27 +1,114 @@
package ocean.gateway.routes;
import static org.junit.Assert.assertThat;
import java.net.URI;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
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.http.HttpStatus;
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 lombok.extern.slf4j.Slf4j;
import ocean.gateway.service.ServiceApplication;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;
import reactor.netty.http.server.HttpServerRequest;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@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;
// }
// }
public HttpServer server;
private String serverPort;
DisposableServer ds;
// @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");
// }
// @AfterClass
// public void stop() {
// if(server != null) {
// ds.dispose();
// }
// }
@Autowired
private TestRestTemplate restTemplate ;
@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";
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");
}
// @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;
// }
// }
}