Remove GrayReleasePluginTests

This commit is contained in:
hongqiaowei
2023-07-07 18:05:46 +08:00
parent 6eee0c2e80
commit 3a50d6c47a

View File

@@ -1,199 +1,199 @@
package com.fizzgate.plugin.grayrelease; // package com.fizzgate.plugin.grayrelease;
//
import com.fasterxml.jackson.core.type.TypeReference; // import com.fasterxml.jackson.core.type.TypeReference;
import com.fizzgate.aggregate.web.filter.AggregateFilter; // import com.fizzgate.aggregate.web.filter.AggregateFilter;
import com.fizzgate.aggregate.web.loader.ConfigLoader; // import com.fizzgate.aggregate.web.loader.ConfigLoader;
import com.fizzgate.filter.FilterResult; // import com.fizzgate.filter.FilterResult;
import com.fizzgate.plugin.FizzPluginFilterChain; // import com.fizzgate.plugin.FizzPluginFilterChain;
import com.fizzgate.plugin.auth.ApiConfig; // import com.fizzgate.plugin.auth.ApiConfig;
import com.fizzgate.proxy.Route; // import com.fizzgate.proxy.Route;
import com.fizzgate.util.Consts; // import com.fizzgate.util.Consts;
import com.fizzgate.util.JacksonUtils; // import com.fizzgate.util.JacksonUtils;
import com.fizzgate.util.ReflectionUtils; // import com.fizzgate.util.ReflectionUtils;
import com.fizzgate.util.WebUtils; // import com.fizzgate.util.WebUtils;
//
import org.junit.jupiter.api.Assertions; // import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; // import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders; // import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; // import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; // import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse; // import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.test.web.reactive.server.WebTestClient; // import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono; // import reactor.core.publisher.Mono;
//
import java.util.HashMap; // import java.util.HashMap;
import java.util.Map; // import java.util.Map;
//
import static org.mockito.Mockito.mock; // import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; // import static org.mockito.Mockito.when;
//
public class GrayReleasePluginTests { // public class GrayReleasePluginTests {
//
/** // /**
* service discovery backend // * service discovery backend
*/ // */
@Test // @Test
public void simpleTest() { // public void simpleTest() {
final Route[] changedRoute = new Route[1]; // final Route[] changedRoute = new Route[1];
WebTestClient client = WebTestClient.bindToWebHandler( // WebTestClient client = WebTestClient.bindToWebHandler(
exchange -> { // exchange -> {
ServerHttpResponse r = exchange.getResponse(); // ServerHttpResponse r = exchange.getResponse();
r.setStatusCode(HttpStatus.OK); // r.setStatusCode(HttpStatus.OK);
r.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE); // r.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes()))); // return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes())));
} // }
) // )
.webFilter( // .webFilter(
(exchange, chain) -> { // (exchange, chain) -> {
//
GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin(); // GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin();
Map<String, Object> config = new HashMap<>(); // Map<String, Object> config = new HashMap<>();
config.put("triggerCondition", " method == 'post' " + // config.put("triggerCondition", " method == 'post' " +
" and matches('path','^/apath/x*') " + // " and matches('path','^/apath/x*') " +
" and clientIpInRange('11.238.145.180', '11.238.145.182') " + // " and clientIpInRange('11.238.145.180', '11.238.145.182') " +
" and exist('body.tools.gun') "); // " and exist('body.tools.gun') ");
config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.SERVICE_DISCOVERY))); // config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.SERVICE_DISCOVERY)));
config.put("routeConfig", // config.put("routeConfig",
"type : http \n " + // "type : http \n " +
"serviceName : bservice \n " + // "serviceName : bservice \n " +
"path : /bpath/{$1} "); // "path : /bpath/{$1} ");
//
// exchange.getAttributes().put("pcsit@", Collections.emptyIterator()); // // exchange.getAttributes().put("pcsit@", Collections.emptyIterator());
Route route = new Route().path("/apath/**"); // Route route = new Route().path("/apath/**");
changedRoute[0] = route; // changedRoute[0] = route;
exchange.getAttributes().put(WebUtils.ROUTE, route); // exchange.getAttributes().put(WebUtils.ROUTE, route);
exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY); // exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY);
exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain); // exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain);
exchange.getAttributes().put("oi@", "11.238.145.181"); // exchange.getAttributes().put("oi@", "11.238.145.181");
//
return grayReleasePlugin.filter(exchange, config); // return grayReleasePlugin.filter(exchange, config);
} // }
) // )
.build(); // .build();
//
client.post() // client.post()
.uri("/aservice/apath/xxx") // .uri("/aservice/apath/xxx")
.contentType(MediaType.APPLICATION_JSON) // .contentType(MediaType.APPLICATION_JSON)
.bodyValue("{\"user\":\"henry\",\"tools\":{\"gun\":\"ak\"}}") // .bodyValue("{\"user\":\"henry\",\"tools\":{\"gun\":\"ak\"}}")
.exchange() // .exchange()
.expectBody(String.class).value( // .expectBody(String.class).value(
v -> { // v -> {
// System.err.println("body:\n" + v); // // System.err.println("body:\n" + v);
} // }
); // );
//
Assertions.assertEquals("bservice", changedRoute[0].backendService); // Assertions.assertEquals("bservice", changedRoute[0].backendService);
Assertions.assertEquals("/bpath/xxx", changedRoute[0].backendPath); // Assertions.assertEquals("/bpath/xxx", changedRoute[0].backendPath);
} // }
//
@Test // @Test
public void reverseProxyBackendTest() { // public void reverseProxyBackendTest() {
//
final Route[] changedRoute = new Route[1]; // final Route[] changedRoute = new Route[1];
//
Map<String, Object> config = new HashMap<>(); // Map<String, Object> config = new HashMap<>();
config.put("triggerCondition", " method == 'get' "); // config.put("triggerCondition", " method == 'get' ");
config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.REVERSE_PROXY))); // config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.REVERSE_PROXY)));
config.put("routeConfig", // config.put("routeConfig",
"serviceName : http://1.2.3.4:8080,http://1.2.3.5:8080 \n " + // "serviceName : http://1.2.3.4:8080,http://1.2.3.5:8080 \n " +
"path : /a/b/c \n" + // "path : /a/b/c \n" +
"query : name1=value1&name2=value2 "); // "query : name1=value1&name2=value2 ");
//
WebTestClient client = WebTestClient.bindToWebHandler( // WebTestClient client = WebTestClient.bindToWebHandler(
exchange -> { // exchange -> {
ServerHttpResponse r = exchange.getResponse(); // ServerHttpResponse r = exchange.getResponse();
r.setStatusCode(HttpStatus.OK); // r.setStatusCode(HttpStatus.OK);
r.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE); // r.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes()))); // return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes())));
} // }
) // )
.webFilter( // .webFilter(
(exchange, chain) -> { // (exchange, chain) -> {
//
GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin(); // GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin();
//
Route route = new Route().path("/apath/**"); // Route route = new Route().path("/apath/**");
changedRoute[0] = route; // changedRoute[0] = route;
exchange.getAttributes().put(WebUtils.ROUTE, route); // exchange.getAttributes().put(WebUtils.ROUTE, route);
exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY); // exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY);
exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain); // exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain);
exchange.getAttributes().put("oi@", "11.238.145.181"); // exchange.getAttributes().put("oi@", "11.238.145.181");
//
return grayReleasePlugin.filter(exchange, config); // return grayReleasePlugin.filter(exchange, config);
} // }
) // )
.build(); // .build();
//
client.get() // client.get()
.uri("/aservice/apath/xxx") // .uri("/aservice/apath/xxx")
.exchange(); // .exchange();
Assertions.assertEquals("/a/b/c?name1=value1&name2=value2", changedRoute[0].getBackendPathQuery()); // Assertions.assertEquals("/a/b/c?name1=value1&name2=value2", changedRoute[0].getBackendPathQuery());
Assertions.assertEquals("http://1.2.3.4:8080", changedRoute[0].nextHttpHostPort); // Assertions.assertEquals("http://1.2.3.4:8080", changedRoute[0].nextHttpHostPort);
//
client.get() // client.get()
.uri("/aservice/apath/xxx") // .uri("/aservice/apath/xxx")
.exchange(); // .exchange();
Assertions.assertEquals("http://1.2.3.5:8080", changedRoute[0].nextHttpHostPort); // Assertions.assertEquals("http://1.2.3.5:8080", changedRoute[0].nextHttpHostPort);
//
client.get() // client.get()
.uri("/aservice/apath/xxx") // .uri("/aservice/apath/xxx")
.exchange(); // .exchange();
Assertions.assertEquals("http://1.2.3.4:8080", changedRoute[0].nextHttpHostPort); // Assertions.assertEquals("http://1.2.3.4:8080", changedRoute[0].nextHttpHostPort);
} // }
//
@Test // @Test
public void aggregateBackendTest() { // public void aggregateBackendTest() {
AggregateFilter aggregateFilter = new AggregateFilter(null, null, null); // AggregateFilter aggregateFilter = new AggregateFilter(null, null, null);
ConfigLoader configLoader = mock(ConfigLoader.class); // ConfigLoader configLoader = mock(ConfigLoader.class);
when( // when(
configLoader.matchAggregateResource("GET", "/_proxytest/bservice/bpath/xxx") // configLoader.matchAggregateResource("GET", "/_proxytest/bservice/bpath/xxx")
) // )
.thenReturn(null); // .thenReturn(null);
ReflectionUtils.set(aggregateFilter, "configLoader", configLoader); // ReflectionUtils.set(aggregateFilter, "configLoader", configLoader);
//
WebTestClient client = WebTestClient.bindToWebHandler( // WebTestClient client = WebTestClient.bindToWebHandler(
exchange -> { // exchange -> {
ServerHttpResponse r = exchange.getResponse(); // ServerHttpResponse r = exchange.getResponse();
r.setStatusCode(HttpStatus.OK); // r.setStatusCode(HttpStatus.OK);
return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes()))); // return r.writeWith(Mono.just(r.bufferFactory().wrap("this is web handler response".getBytes())));
} // }
) // )
.webFilter( // .webFilter(
(exchange, chain) -> { // (exchange, chain) -> {
//
GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin(); // GrayReleasePlugin grayReleasePlugin = new GrayReleasePlugin();
Map<String, Object> config = new HashMap<>(); // Map<String, Object> config = new HashMap<>();
config.put("triggerCondition", " method == 'get' "); // config.put("triggerCondition", " method == 'get' ");
config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.SERVICE_AGGREGATE))); // config.put("routeType", Integer.parseInt(String.valueOf(ApiConfig.Type.SERVICE_AGGREGATE)));
config.put("routeConfig", // config.put("routeConfig",
"type : http \n " + // "type : http \n " +
"serviceName : bservice \n " + // "serviceName : bservice \n " +
"path : /bpath/{$1} "); // "path : /bpath/{$1} ");
//
Route route = new Route().path("/apath/**"); // Route route = new Route().path("/apath/**");
exchange.getAttributes().put(WebUtils.ROUTE, route); // exchange.getAttributes().put(WebUtils.ROUTE, route);
exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY); // exchange.getAttributes().put(WebUtils.IGNORE_PLUGIN, Consts.S.EMPTY);
exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain); // exchange.getAttributes().put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain);
exchange.getAttributes().put("oi@", "11.238.145.181"); // exchange.getAttributes().put("oi@", "11.238.145.181");
//
Map<String, Object> filterContext = new HashMap<>(); // Map<String, Object> filterContext = new HashMap<>();
exchange.getAttributes().put(WebUtils.FILTER_CONTEXT, filterContext); // exchange.getAttributes().put(WebUtils.FILTER_CONTEXT, filterContext);
filterContext.put(WebUtils.PREV_FILTER_RESULT, FilterResult.SUCCESS("x")); // filterContext.put(WebUtils.PREV_FILTER_RESULT, FilterResult.SUCCESS("x"));
//
return grayReleasePlugin.filter(exchange, config); // return grayReleasePlugin.filter(exchange, config);
}, // },
aggregateFilter // aggregateFilter
) // )
.build(); // .build();
//
client.get() // client.get()
.uri("/_proxytest/aservice/apath/xxx") // .uri("/_proxytest/aservice/apath/xxx")
.exchange() // .exchange()
.expectBody(String.class).value( // .expectBody(String.class).value(
v -> { // v -> {
Map<String, Object> bodyMap = JacksonUtils.readValue(v, new TypeReference<Map<String, Object>>(){}); // Map<String, Object> bodyMap = JacksonUtils.readValue(v, new TypeReference<Map<String, Object>>(){});
Assertions.assertEquals(bodyMap.get("message"), "API not found in aggregation: /_proxytest/bservice/bpath/xxx"); // Assertions.assertEquals(bodyMap.get("message"), "API not found in aggregation: /_proxytest/bservice/bpath/xxx");
} // }
); // );
} // }
} // }