You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by jo...@apache.org on 2022/01/04 13:08:03 UTC
[incubator-shenyu] branch master updated: [ISSUE #2679] Add integration test for RequestControllerTest (#2701)
This is an automated email from the ASF dual-hosted git repository.
jooks 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 f321def [ISSUE #2679] Add integration test for RequestControllerTest (#2701)
f321def is described below
commit f321def78f1e3552d12c8ed6ef449f5f65526a8e
Author: chuang <49...@users.noreply.github.com>
AuthorDate: Tue Jan 4 21:07:56 2022 +0800
[ISSUE #2679] Add integration test for RequestControllerTest (#2701)
* [ISSUE #2679] Add integration test for RequestControllerTest
Add integration test for ShenyuClientPathControllerTest
Add @ShenyuSpringMvcClient to ShenyuClientPathController
* [ISSUE #2679] Add integration test for RequestControllerTest
Add integration test for ShenyuClientPathControllerTest
Add @ShenyuSpringMvcClient to ShenyuClientPathController
* [ISSUE #2679] Add integration test for RequestControllerTest
Add integration test for ShenyuClientPathControllerTest
Add @ShenyuSpringMvcClient to ShenyuClientPathController
---
.../controller/ShenyuClientPathController.java | 1 +
.../integratedtest/common/helper/HttpHelper.java | 6 ++-
.../test/http/RequestControllerTest.java | 59 ++++++++++++++++++++++
.../test/http/ShenyuClientPathControllerTest.java | 52 +++++++++++++++++++
4 files changed, 117 insertions(+), 1 deletion(-)
diff --git a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
index 29d4575..377f513 100644
--- a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
+++ b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/ShenyuClientPathController.java
@@ -49,6 +49,7 @@ public class ShenyuClientPathController {
* @return result
*/
@RequestMapping("shenyu/client/hi")
+ @ShenyuSpringMvcClient(path = "shenyu/client/hi", desc = "shenyu client annotation register")
public String hello(final String name) {
return "hi! " + name + "! " + HELLO_SUFFIX;
}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/helper/HttpHelper.java b/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/helper/HttpHelper.java
index b7208d9..213e0f0 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/helper/HttpHelper.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-common/src/main/java/org/apache/shenyu/integratedtest/common/helper/HttpHelper.java
@@ -182,7 +182,11 @@ public class HttpHelper {
Response response = client.newCall(request).execute();
String respBody = Objects.requireNonNull(response.body()).string();
LOG.info("getFromGateway({}) resp({})", path, respBody);
- return GSON.fromJson(respBody, type);
+ try {
+ return GSON.fromJson(respBody, type);
+ } catch (Exception e) {
+ return (S) respBody;
+ }
}
/**
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/RequestControllerTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/RequestControllerTest.java
new file mode 100644
index 0000000..bada56c
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/RequestControllerTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+import okhttp3.Response;
+import org.apache.shenyu.integratedtest.common.AbstractTest;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public final class RequestControllerTest extends AbstractTest {
+
+ @Test
+ public void testRequestHeader() throws IOException {
+ Map<String, Object> headers = new HashMap<>(2, 1);
+ headers.put("header_key1", "header_key1");
+ Response response = HttpHelper.INSTANCE.getResponseFromGateway("/http/request/header", headers);
+ String body = Objects.requireNonNull(response.body()).string();
+ assertTrue(body.contains("response success:"));
+ }
+
+ @Test
+ public void testRequestParameter() throws IOException {
+ String body = HttpHelper.INSTANCE.postGateway("/http/request/parameter?parameter_key1=chuang", String.class);
+ assertEquals("response success: {parameter_key1=[chuang]}", body);
+ }
+
+ @Test
+ public void testRequestCookie() throws IOException {
+ String userId = "chuang";
+ Map<String, Object> headers = new HashMap<>(2, 1);
+ headers.put("Cookie", "userId=" + userId + "");
+ Response response = HttpHelper.INSTANCE.getResponseFromGateway("/http/request/cookie", headers);
+ String body = Objects.requireNonNull(response.body()).string();
+ assertEquals("response success: {userId=[userId=" + userId + "]}", body);
+ }
+}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/ShenyuClientPathControllerTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/ShenyuClientPathControllerTest.java
new file mode 100644
index 0000000..4dea35f
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/ShenyuClientPathControllerTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+import org.apache.shenyu.integratedtest.common.AbstractTest;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+
+public final class ShenyuClientPathControllerTest extends AbstractTest {
+
+ private static final String HELLO_SUFFIX = "I'm Shenyu-Gateway System. Welcome!";
+
+ @Test
+ public void testHello() throws IOException {
+ String body = HttpHelper.INSTANCE.getFromGateway("/http/shenyu/client/hello", String.class);
+ assertEquals("hello! " + HELLO_SUFFIX, body);
+ }
+
+ @Test
+ public void testHi() throws IOException {
+ String name = "chuang";
+ String body = HttpHelper.INSTANCE.getFromGateway("/http/shenyu/client/hi?name=" + name + "", String.class);
+ assertEquals("hi! " + name + "! " + HELLO_SUFFIX, body);
+ }
+
+ @Test
+ public void testPost() throws IOException {
+ String name = "chuang";
+ String body = HttpHelper.INSTANCE.postGateway("/http/shenyu/client/post/hi?name=" + name + "", String.class);
+ assertEquals("[post method result]:hi! " + name + "! " + HELLO_SUFFIX, body);
+ }
+
+}