test code is success!

This commit is contained in:
huangsimin 2019-05-31 16:45:29 +08:00
parent f825294e79
commit 12f2a596bb
5 changed files with 61 additions and 13 deletions

View File

@ -1,12 +1,15 @@
package ocean.gateway.service; package ocean.gateway.service;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
@SpringBootApplication(scanBasePackages = {"ocean.gateway.service.*"}) @SpringBootApplication(scanBasePackages = {"ocean.gateway.service.*"})
@ComponentScan @ComponentScan
@SpringBootConfiguration
public class ServiceApplication { public class ServiceApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args); SpringApplication.run(ServiceApplication.class, args);

View File

@ -4,14 +4,21 @@ import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@Slf4j
@Component @Component
@Order(0) @SpringBootConfiguration
@Order(-200)
public class TokenFilter implements GlobalFilter { public class TokenFilter implements GlobalFilter {
public TokenFilter() { public TokenFilter() {
@ -19,7 +26,23 @@ public class TokenFilter implements GlobalFilter {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return null; ServerHttpRequest request = exchange.getRequest();
HttpHeaders header = request.getHeaders();
String token = header.getFirst("token");
if (token != null) {
//
return chain.filter(exchange);
} else {
log.warn("request token is empty: {}", request.getURI());
}
ServerHttpResponse response = exchange.getResponse();
if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
log.error("if the status code has not been set because the HTTP response is already committed");
}
return response.setComplete();
} }

View File

@ -13,7 +13,7 @@ import lombok.extern.slf4j.Slf4j;
public class Business { public class Business {
@Bean @Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
log.warn("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("path_route", r -> r.path("/hello").uri("http://localhost:3030/hello")).build();
} }

View File

@ -0,0 +1,29 @@
package ocean.gateway.filters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import ocean.gateway.service.ServiceApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TokenFilterTests {
@Autowired
private TestRestTemplate restTemplate ;
@Test
public void GetHello() {
ResponseEntity<String> response = restTemplate.getForEntity("http://127.0.0.1:8080/hello", String.class);
Assert.notNull(response, "The class must not be null");
Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
}
}

View File

@ -1,16 +1,9 @@
package ocean.gateway.service; package ocean.gateway.service;
import org.junit.Test; // @RunWith(SpringRunner.class)
import org.junit.runner.RunWith; // @SpringBootTest(classes = {SpringApplication.class})
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceApplicationTests { public class ServiceApplicationTests {
@Test
public void contextLoads() {
}
} }