This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 1f95559 [ISSUE #1966] Add integrated test for request plugin (#1998)
1f95559 is described below
commit 1f95559f360a31a8441b62dbc5f1f5d7bc59169e
Author: rom1c <[email protected]>
AuthorDate: Fri Sep 3 16:45:51 2021 +0800
[ISSUE #1966] Add integrated test for request plugin (#1998)
* 1. pom.xml add request plugin
2. add RequestPluginTest
3. add Request method
* 1. pom.xml add request plugin
2. add RequestPluginTest
3. add Request method
* if not modify hosts that shenyu-admin resolve error
* shenyu-admin hosts resolve?
* 1. pom.xml add request plugin
2. add RequestPluginTest
3. add Request method
* resolve conflicts
* ci
* ci
* 1. pom.xml add request plugin
2. add RequestPluginTest
3. add Request method
* 1. pom.xml add request plugin
2. add RequestPluginTest
3. add Request method
---
.../http/controller/HttpTestController.java | 69 ++++++++++-
.../test/http/combination/RequestPluginTest.java | 129 +++++++++++++++++++++
2 files changed, 192 insertions(+), 6 deletions(-)
diff --git
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
index 2bd0bba..6507993 100644
---
a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
+++
b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
@@ -20,14 +20,18 @@ package org.apache.shenyu.examples.http.controller;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.apache.shenyu.examples.http.dto.UserDTO;
import org.apache.shenyu.examples.http.result.ResultBean;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import java.util.HashMap;
+import java.util.Map;
/**
* TestController.
@@ -110,6 +114,7 @@ public class HttpTestController {
/**
* the waf pass.
*
+ * @return response.
* @return the string
*/
@PostMapping("/waf/pass")
@@ -122,6 +127,7 @@ public class HttpTestController {
/**
* the waf deny.
+ *
* @return response.
*/
@PostMapping("/waf/deny")
@@ -133,8 +139,59 @@ public class HttpTestController {
}
/**
+ * request Pass.
+ * @param requestParameter the requestParameter.
+ * @return ResultBean
+ */
+ @GetMapping("/request/parameter/pass")
+ public ResultBean requestParameter(@RequestParam("requestParameter") final
String requestParameter) {
+ ResultBean response = new ResultBean();
+ response.setCode(200);
+ response.setMsg("pass");
+
+ Map<String, Object> param = new HashMap<>();
+ param.put("requestParameter", requestParameter);
+ response.setData(param);
+ return response;
+ }
+
+ /**
+ * request Pass.
+ * @param requestHeader the requestHeader.
+ * @return ResultBean
+ */
+ @GetMapping("/request/header/pass")
+ public ResultBean requestHeader(@RequestHeader("requestHeader") final
String requestHeader) {
+ ResultBean response = new ResultBean();
+ response.setCode(200);
+ response.setMsg("pass");
+
+ Map<String, Object> param = new HashMap<>();
+ param.put("requestHeader", requestHeader);
+ response.setData(param);
+ return response;
+ }
+
+ /**
+ * request Pass.
+ * @param cookie the cookie.
+ * @return ResultBean
+ */
+ @GetMapping("/request/cookie/pass")
+ public ResultBean requestCookie(@CookieValue("cookie") final String
cookie) {
+ ResultBean response = new ResultBean();
+ response.setCode(200);
+ response.setMsg("pass");
+
+ Map<String, Object> param = new HashMap<>();
+ param.put("cookie", cookie);
+ response.setData(param);
+ return response;
+ }
+
+ /**
* post sentinel.
- * @return response
+ * @return response.
*/
@PostMapping("/sentinel/pass")
public ResultBean sentinelPass() {
diff --git
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RequestPluginTest.java
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RequestPluginTest.java
new file mode 100644
index 0000000..090da53
--- /dev/null
+++
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RequestPluginTest.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.integrated.test.http.combination;
+
+import org.apache.shenyu.common.dto.ConditionData;
+import org.apache.shenyu.common.dto.convert.RequestHandle;
+import org.apache.shenyu.common.enums.OperatorEnum;
+import org.apache.shenyu.common.enums.ParamTypeEnum;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.JsonUtils;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.apache.shenyu.web.controller.PluginController.RuleLocalData;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.HttpCookie;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+public final class RequestPluginTest extends AbstractPluginDataInit {
+
+ @BeforeClass
+ public static void setup() throws IOException {
+ String pluginResult = initPlugin(PluginEnum.SENTINEL.getName(),
"{\"model\":\"black\"}");
+ assertThat(pluginResult, is("success"));
+ String selectorAndRulesResult =
initSelectorAndRules(PluginEnum.REQUEST.getName(), "",
buildSelectorConditionList(), buildRuleLocalDataList());
+ assertThat(selectorAndRulesResult, is("success"));
+ }
+
+ @Test
+ public void test() throws IOException {
+ Map<String, Object> paramMap = new HashMap<>();
+
+ Map<String, Object> result =
HttpHelper.INSTANCE.getFromGateway("/http/test/request/parameter/pass?requestParameter=requestParameter",
paramMap, Map.class);
+ assertNotNull(result);
+ assertEquals("pass", result.get("msg"));
+ assertEquals("requestParameter",
GsonUtils.getInstance().convertToMap(String.valueOf(result.get("data"))).get("requestParameter"));
+
+ paramMap.clear();
+ paramMap.put("requestHeader", "requestHeader");
+ result =
HttpHelper.INSTANCE.getFromGateway("/http/test/request/header/pass", paramMap,
Map.class);
+ assertNotNull(result);
+ assertEquals("pass", result.get("msg"));
+ assertEquals("requestHeader",
GsonUtils.getInstance().convertToMap(String.valueOf(result.get("data"))).get("requestHeader"));
+
+ paramMap.clear();
+ paramMap.put("cookie", new HttpCookie("cookie", "cookie"));
+ result =
HttpHelper.INSTANCE.getFromGateway("/http/test/request/cookie/pass", paramMap,
Map.class);
+ assertNotNull(result);
+ assertEquals("pass", result.get("msg"));
+ assertEquals("cookie",
GsonUtils.getInstance().convertToMap(String.valueOf(result.get("data"))).get("cookie"));
+ }
+
+ private static List<ConditionData> buildSelectorConditionList() {
+ ConditionData conditionData = new ConditionData();
+ conditionData.setParamType(ParamTypeEnum.URI.getName());
+ conditionData.setOperator(OperatorEnum.MATCH.getAlias());
+ conditionData.setParamValue("/http/test/request/**");
+ return Collections.singletonList(conditionData);
+ }
+
+ private static List<RuleLocalData> buildRuleLocalDataList() {
+ List<RuleLocalData> ruleLocalDataList = new ArrayList<>();
+ ruleLocalDataList.add(buildRuleLocalData("/http/test/request/pass"));
+ return ruleLocalDataList;
+ }
+
+ private static RuleLocalData buildRuleLocalData(final String paramValue) {
+ final RuleLocalData ruleLocalData = new RuleLocalData();
+ RequestHandle requestHandle = new RequestHandle();
+ final RequestHandle.ShenyuRequestParameter requestParameter =
requestHandle.new ShenyuRequestParameter();
+ final RequestHandle.ShenyuRequestHeader requestHeader =
requestHandle.new ShenyuRequestHeader();
+ final RequestHandle.ShenyuCookie cookie = requestHandle.new
ShenyuCookie();
+
+ Map<String, String> paramMap = new HashMap<>();
+ paramMap.put("requestParameter", "requestParameter");
+ requestParameter.setSetParameters(paramMap);
+ requestHandle.setParameter(requestParameter);
+
+ paramMap.clear();
+ paramMap.put("requestHeader", "requestHeader");
+ requestHeader.setSetHeaders(paramMap);
+ requestHandle.setHeader(requestHeader);
+
+ paramMap.clear();
+ paramMap.put("cookie", "cookie");
+ cookie.setSetCookies(paramMap);
+ requestHandle.setCookie(cookie);
+
+ ruleLocalData.setRuleHandler(JsonUtils.toJson(requestHandle));
+ ConditionData conditionData = new ConditionData();
+ conditionData.setParamType(ParamTypeEnum.URI.getName());
+ conditionData.setOperator(OperatorEnum.EQ.getAlias());
+ conditionData.setParamValue(paramValue);
+
ruleLocalData.setConditionDataList(Collections.singletonList(conditionData));
+ return ruleLocalData;
+ }
+
+ @AfterClass
+ public static void clean() throws IOException {
+ cleanPluginData(PluginEnum.REQUEST.getName());
+ }
+}