90%单元测试覆盖率, 基本可以使用.

This commit is contained in:
huangsimin 2019-07-17 17:59:58 +08:00
parent 6037e1a58a
commit 0c042beb3f
8 changed files with 105 additions and 71 deletions

View File

@ -21,7 +21,7 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<dubbo.version>2.7.2</dubbo.version>
</properties>

View File

@ -8,7 +8,6 @@ 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.web.server.ServerWebExchange;
import lombok.extern.slf4j.Slf4j;
@ -17,48 +16,48 @@ import reactor.core.publisher.Mono;
@Slf4j
@SpringBootConfiguration
@Order(-200)
public class VerifyFilter {
public class VerifyFilter implements GlobalFilter {
// @Override
// public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// if (true)
// return chain.filter(exchange);
// // TODO: 添加权限
// ServerHttpRequest request = exchange.getRequest();
// HttpHeaders header = request.getHeaders();
if (true)
return chain.filter(exchange);
// TODO: 添加权限
ServerHttpRequest request = exchange.getRequest();
HttpHeaders header = request.getHeaders();
// ServerHttpResponse response = exchange.getResponse();
// HttpHeaders respHeader = response.getHeaders();
// respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
// respHeader.add("version", "v0.0.1");
ServerHttpResponse response = exchange.getResponse();
HttpHeaders respHeader = response.getHeaders();
respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
respHeader.add("version", "v0.0.1");
// String token = header.getFirst("token");
// if (token != null) {
// //
// if (VerifyToken(token)) {
// log.info("request token is Verify Successed: {}", token);
// return chain.filter(exchange);
// }
// log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
// } else {
// log.warn("request token is empty: {}", request.getURI());
// }
String token = header.getFirst("token");
if (token != null) {
//
if (VerifyToken(token)) {
log.info("request token is Verify Successed: {}", token);
return chain.filter(exchange);
}
log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
} else {
log.warn("request token is empty: {}", request.getURI());
}
// if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
// log.error("if the status code has not been set because the HTTP response is already committed");
// }
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();
// }
return response.setComplete();
}
// public static final String TEST_TOKEN = "yame";
public static final String TEST_TOKEN = "yame";
// public boolean VerifyToken(String token) {
// if (token.equals(TEST_TOKEN)) {
// return true;
// }
// return false;
// }
public boolean VerifyToken(String token) {
if (token.equals(TEST_TOKEN)) {
return true;
}
return false;
}
}

View File

@ -1,9 +1,6 @@
package cn.ecpark.service.usergw.biz.filters.factory;
import java.lang.annotation.Retention;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
@ -12,28 +9,19 @@ import java.util.List;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.rpc.service.GenericService;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
import org.springframework.cloud.gateway.handler.FilteringWebHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
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.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
import lombok.extern.slf4j.Slf4j;
@ -107,7 +95,7 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
// setResponseStatus(exchange, HttpStatus.INTERNAL_SERVER_ERROR);
// return chain.filter(exchange);
// }
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
ServerHttpRequest req = exchange.getRequest();

View File

@ -91,7 +91,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
List<String> defaultFiltersYaml = null;
Map<String, Object> restfulYaml = null;
Map<String, Object> dubboYaml = null;
Object unknown;
unknown = configYaml.get("default-filters");
if (unknown != null) {

View File

@ -7,22 +7,22 @@ restful:
uri: http://httpbin.org:80/*
order: 9
predicates:
- Path=/get
- Header=XX, \d+
- Path=/get
- Header=XX, \d+
- id: path_route12;
uri: http://httpbin.org:80/*
order: 9
predicates:
- Path=/get
- Header=XX, \d+
- Path=/get
- Header=XX, \d+
- id: path_route13
uri: http://httpbin.org:80/*
order: 9
predicates:
- Path=/get
- Header=XX, \d+
- Path=/get
- Header=XX, \d+
# - id: redirect_to
# uri: http://localhost/test/**
# order: 11

View File

@ -1,15 +1,26 @@
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
restful: # filters 必须 有predicates uri只是预期到达的地址
routes:
- id: redirect_to
- id: haha
uri: http://localhost:4444
order: 0
predicates:
- Path=/http/hello
- id: rewrite
uri: http://localhost:4444
order: 0
predicates:
- Path=/http/rewrite
filters:
- RewritePath=/http/rewrite, /http/hello
- id: xixi
uri: http://localhost:8888/http/xixi
order: 11
predicates:
- Path=http://localhost:8888/http/xixi
- Path=/http/xixi
filters:
- RedirectTo=302, http://localhost:8888/http/hello
- RedirectTo=302, http://localhost:4444/http/hello
dubbo:
routes:
@ -28,6 +39,6 @@ dubbo:
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
- Path=/dubbo/hello

View File

@ -1,5 +1,9 @@
package cn.ecpark.service.usergw;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -14,7 +18,7 @@ import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;
// TODO: 添加附加Dubbo Service for Test启动进程
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@RunWith(SpringRunner.class)
@TestPropertySource(locations = { "application3.properties" })
public class TestHttp2DubboConfig3 {
@ -43,21 +47,55 @@ public class TestHttp2DubboConfig3 {
@Test
@SuppressWarnings("unchecked")
public void Test3RestfulFilters() {
// Test Base Url
HttpClient client = HttpClient.create();
ResponseReceiver<?> receiver;
String content;
// Test 302
receiver = client.baseUrl("http://localhost:" + serverPort + "/http/xixi").get();
HttpClientResponse response = receiver.response().block();
Assert.assertNotNull(response);
Assert.assertEquals(response.status().code(), 302);
content = receiver.responseContent().asString().blockLast();
Assert.assertNotNull(content);
Assert.assertEquals(content, "Hello Dubbo");
Assert.assertNull(content);
// Test RedirectTo
receiver = client.baseUrl("http://localhost:" + serverPort + "/http/xixi").followRedirect(true).get();
response = receiver.response().block();
Assert.assertEquals(response.status().code(), 200);
content = receiver.responseContent().asString().blockLast();
Assert.assertEquals(content, "Http Hello");
// 测试没配置的路由
receiver = client.baseUrl("http://localhost:" + serverPort + "/xixi").followRedirect(true).get();
response = receiver.response().block();
Assert.assertEquals(response.status().code(), 404);
content = receiver.responseContent().asString().blockLast();
Assert.assertNotNull(content);
Object rjson = JSON.parse(content);
Map<String, Object> rmap = (Map<String, Object>)rjson;
Assert.assertEquals(rmap.get("error").toString(), "Not Found");
// 测试普通路由
receiver = client.baseUrl("http://localhost:" + serverPort + "/http/hello").get();
response = receiver.response().block();
Assert.assertEquals(response.status().code(), 200);
content = receiver.responseContent().asString().blockLast();
Assert.assertNotNull(content);
// 测试普通路由
receiver = client.baseUrl("http://localhost:" + serverPort + "/http/rewrite").get();
response = receiver.response().block();
Assert.assertEquals(response.status().code(), 200);
content = receiver.responseContent().asString().blockLast();
Assert.assertEquals(content, "Http Hello");
}

View File

@ -1,13 +1,11 @@
package cn.ecpark.service.usergw.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.test.context.TestComponent;
import cn.ecpark.service.usergw.utils.Extract.GenericServiceBase;