restful routes predicates filters 的功能测试.

This commit is contained in:
huangsimin 2019-07-16 18:50:27 +08:00
parent 9c49ced6ed
commit 6037e1a58a
13 changed files with 164 additions and 48 deletions

View File

@ -151,26 +151,27 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
}
if (result == null) {
response.setComplete().block();
result = HttpStatus.OK.getReasonPhrase();
}
if (result.getClass() != String.class) {
response.writeWith(Mono.just(response.bufferFactory()
.wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes())))).block();
return;
}
if (result.getClass() == String.class) {
response.writeWith(Mono.just(
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
} else {
response.writeWith(Mono.just(response.bufferFactory()
.wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
}
response.writeWith(Mono.just(
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes())))).block();
return;
}
}
} else {
response.setStatusCode(statusCode);
}
}
result = statusCode.getReasonPhrase();
response.writeWith(Mono.just(
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes())))).block();
return;
// response.setComplete().block();
// return chain.filter(exchange);
}));
}

View File

@ -371,9 +371,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
String yamlField) {
List<String> filtersYaml = iter.get(yamlField);
if (filtersYaml != null) {
filters.addAll(defaultFilters);
for (String filterString : filtersYaml) {
filters.add(new FilterDefinition(filterString));
}

View File

@ -7,10 +7,6 @@ import org.apache.dubbo.rpc.service.GenericService;
public class Extract {
public static String getReferenceConfigKey(ReferenceConfig<GenericService> referenceConfig) {
String iName = referenceConfig.getInterface();
if (StringUtils.isBlank(iName)) {
Class<?> clazz = referenceConfig.getInterfaceClass();
iName = clazz.getName();
}
if (StringUtils.isBlank(iName)) {
throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig);
}

View File

@ -50,7 +50,7 @@ dubbo:
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
- Path=/dubbo/say
filters:
- SetRequestHeader=params, SetHeader

View File

@ -0,0 +1,33 @@
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar
restful: # filters 必须 有predicates uri只是预期到达的地址
routes:
- id: redirect_to
uri: http://localhost:8888/http/xixi
order: 11
predicates:
- Path=http://localhost:8888/http/xixi
filters:
- RedirectTo=302, http://localhost:8888/http/hello
dubbo:
routes:
- id: test
application: dubbo-exchange
# methods: # 如果没填就从 request拿 意味着所有接口都可以使用
# - name: Say
# param-types:
# - java.lang.String
# - name: Hello
connections: 4
group: test
registry: zookeeper://127.0.0.1:2181
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello

View File

@ -1,4 +1,4 @@
package cn.ecpark.service.usergw.utils;
package cn.ecpark.service.usergw;
import org.junit.Assert;
import org.junit.Test;
@ -6,11 +6,9 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.gateway.handler.FilteringWebHandler;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import cn.ecpark.service.usergw.App;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;

View File

@ -1,4 +1,4 @@
package cn.ecpark.service.usergw.utils;
package cn.ecpark.service.usergw;
import org.junit.Assert;
import org.junit.Test;
@ -6,13 +6,9 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import cn.ecpark.service.usergw.App;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClient.ResponseReceiver;
import reactor.netty.http.client.HttpClientResponse;
@ -33,30 +29,16 @@ public class TestHttp2DubboConfig2 {
HttpClient client = HttpClient.create();
ResponseReceiver<?> receiver;
String content;
// receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello").headers(
// h -> h.set("method", "Say").add("param-types", "java.lang.String").add("params", "Test-MyHttp2dubbo"))
// .get();
// HttpClientResponse resp = receiver.response().block();
// Assert.assertNotNull(resp);
// content = receiver.responseContent().asString().blockLast();
// Assert.assertEquals(resp.status().code(), 200);
// Assert.assertNotNull(content);
// Assert.assertEquals(content, "SetHeader");
receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello")
.headers(h -> h.set("method", "Hello")).get();
HttpClientResponse response = receiver.response().block();
Assert.assertNotNull(response);
// Assert.assertEquals(response.status().code(), 404);
int code = response.status().code();
Assert.assertEquals(response.status().code(), 404);
content = receiver.responseContent().asString().blockLast();
Assert.assertNull(content);
Assert.assertNotNull(content);
Assert.assertEquals(content, "Not Found");
}

View File

@ -0,0 +1,64 @@
package cn.ecpark.service.usergw;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.netty.http.client.HttpClient;
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)
@RunWith(SpringRunner.class)
@TestPropertySource(locations = { "application3.properties" })
public class TestHttp2DubboConfig3 {
@LocalServerPort
private int serverPort;
@Test
public void Test2RequestHttp2DubboSetHeader() {
// Test Base Url
HttpClient client = HttpClient.create();
ResponseReceiver<?> receiver;
String content;
receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello")
.headers(h -> h.set("method", "Hello")).get();
HttpClientResponse response = receiver.response().block();
Assert.assertNotNull(response);
Assert.assertEquals(response.status().code(), 200);
content = receiver.responseContent().asString().blockLast();
Assert.assertNotNull(content);
Assert.assertEquals(content, "Hello Dubbo");
}
@Test
public void Test3RestfulFilters() {
// Test Base Url
HttpClient client = HttpClient.create();
ResponseReceiver<?> receiver;
String content;
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");
}
}

View File

@ -0,0 +1,15 @@
spring.application.name=gateway
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
dubbo.protocol.name=dubbo
dubbo.protocol.port=20999
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.config-center.address=zookeeper://127.0.0.1:2181
dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
server.port=8888
# logging.level.org.springframework.cloud.gateway=debug
logging.file=logs/log
yame.config=test-gateway3.yaml

View File

@ -0,0 +1,10 @@
package cn.ecpark.service.usergw.config;
import org.junit.Test;
public class ParseConfig {
@Test
public void My() {
}
}

View File

@ -1,12 +1,17 @@
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;
@TestComponent
public class TestExtract {
@Test
@ -27,7 +32,21 @@ public class TestExtract {
}
@Test
public void TestUriQueryList() {
public void TestFirst2Case() {
String content = Convert.firstUpperCase("str");
Assert.assertEquals(content, "Str");
}
@Test
public void TestReferenceConfigKey() {
ReferenceConfig<GenericService> ref = new ReferenceConfig<GenericService>();
try {
Extract.getReferenceConfigKey(ref);
Assert.assertTrue(false);
} catch (Exception e) {
String m = e.getMessage();
assertTrue(m.startsWith("No interface info in ReferenceConfig"));
}
}
}