This is an automated email from the ASF dual-hosted git repository.

zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new a2e626f7e [Task #3740] add upload file example and integrated test. 
(#3810)
a2e626f7e is described below

commit a2e626f7e01c1c2c2af7608b9f0d1931eb6d9b34
Author: lahmxu <[email protected]>
AuthorDate: Fri Aug 5 21:01:23 2022 +0800

    [Task #3740] add upload file example and integrated test. (#3810)
    
    * [feature:example #3740] add file upload example for shenyu-examples-https
    
    * [feature:integrated test #3740] add file upload integration test for 
shenyu-integrated-test-https
    
    * [feature:example #3740] add upload example for shenyu-examples-springcloud
    
    * [feature:integrated test #3740] add file upload integration test for 
shenyu-integrated-test-spring-cloud
---
 .../src/main/http/https-test-api-local.http        | 37 ++++++++++
 .../src/main/http/https-test-api.http              | 37 ++++++++++
 .../https/controller/UploadController.java         | 48 +++++++++++++
 .../src/main/http/spring-cloud-test-api-local.http | 12 ++++
 .../src/main/http/spring-cloud-test-api.http       | 12 ++++
 .../springcloud/controller/UploadController.java   | 49 +++++++++++++
 .../integrated/test/https/HttpsUploadFileTest.java | 78 ++++++++++++++++++++
 .../springcloud/SpringCloudPluginUploadTest.java   | 84 ++++++++++++++++++++++
 8 files changed, 357 insertions(+)

diff --git 
a/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api-local.http 
b/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api-local.http
new file mode 100644
index 000000000..30660f43e
--- /dev/null
+++ 
b/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api-local.http
@@ -0,0 +1,37 @@
+#
+# 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.
+#
+
+# Use gateway proxy
+# if you used IDEA, start example project. click green button.Quick access!
+
+### shengyu https example
+GET https://localhost:8190/order/findById?id=3
+Accept: application/json
+Content-Type: application/json
+
+
+### shengyu https upload file example
+POST https://localhost:8190/upload/file
+Cache-Control: no-cache
+Content-Type: multipart/form-data; boundary=WebAppBoundary
+
+--WebAppBoundary
+Content-Disposition: form-data; name="file"; filename="1.txt";
+Content-Type: multipart/form-data
+
+< ../../../../shenyu-examples-common/src/main/resources/1.txt
+--WebAppBoundary--
diff --git 
a/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api.http 
b/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api.http
new file mode 100644
index 000000000..10750c8e4
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-https/src/main/http/https-test-api.http
@@ -0,0 +1,37 @@
+#
+# 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.
+#
+
+# Use gateway proxy
+# if you used IDEA, start example project. click green button.Quick access!
+
+### shengyu https example
+GET http://localhost:9195/order/order/findById?id=3
+Accept: application/json
+Content-Type: application/json
+
+
+### shengyu https upload file example
+POST http://localhost:9195/order/upload/file
+Cache-Control: no-cache
+Content-Type: multipart/form-data; boundary=WebAppBoundary
+
+--WebAppBoundary
+Content-Disposition: form-data; name="file"; filename="1.txt";
+Content-Type: multipart/form-data
+
+< ../../../../shenyu-examples-common/src/main/resources/1.txt
+--WebAppBoundary--
diff --git 
a/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/UploadController.java
 
b/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/UploadController.java
new file mode 100644
index 000000000..ac4a72c49
--- /dev/null
+++ 
b/shenyu-examples/shenyu-examples-https/src/main/java/org/apache/shenyu/examples/https/controller/UploadController.java
@@ -0,0 +1,48 @@
+/*
+ * 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.examples.https.controller;
+
+import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
+import org.springframework.http.MediaType;
+import org.springframework.http.codec.multipart.FilePart;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * UploadController.
+ */
+@RestController
+@RequestMapping("/upload")
+@ShenyuSpringMvcClient("/upload/**")
+public class UploadController {
+
+    /**
+     * upload file.
+     *
+     * @param file
+     * @return filename
+     */
+    @PostMapping(value = "/file", consumes = 
{MediaType.MULTIPART_FORM_DATA_VALUE})
+    @ShenyuSpringMvcClient("/file")
+    public String uploadFile(@RequestPart("file") MultipartFile file){
+        return file.getOriginalFilename();
+    }
+}
diff --git 
a/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api-local.http
 
b/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api-local.http
index 758b4df02..9304e2164 100644
--- 
a/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api-local.http
+++ 
b/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api-local.http
@@ -93,3 +93,15 @@ Content-Type: application/json
 POST http://localhost:8884/class/annotation/post
 Accept: application/json
 Content-Type: application/json
+
+### shengyu local used upload file
+POST http://localhost:8884/upload/file
+Cache-Control: no-cache
+Content-Type: multipart/form-data; boundary=WebAppBoundary
+
+--WebAppBoundary
+Content-Disposition: form-data; name="file"; filename="1.txt";
+Content-Type: multipart/form-data
+
+< ../../../../shenyu-examples-common/src/main/resources/1.txt
+--WebAppBoundary--
diff --git 
a/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api.http
 
b/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api.http
index 81a8762bd..f1e979da7 100644
--- 
a/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api.http
+++ 
b/shenyu-examples/shenyu-examples-springcloud/src/main/http/spring-cloud-test-api.http
@@ -95,3 +95,15 @@ Content-Type: application/json
 POST http://localhost:9195/springcloud/class/annotation/post
 Accept: application/json
 Content-Type: application/json
+
+### shengyu getway proxy used upload file
+POST http://localhost:9195/springcloud/upload/file
+Cache-Control: no-cache
+Content-Type: multipart/form-data; boundary=WebAppBoundary
+
+--WebAppBoundary
+Content-Disposition: form-data; name="file"; filename="1.txt";
+Content-Type: multipart/form-data
+
+< ../../../../shenyu-examples-common/src/main/resources/1.txt
+--WebAppBoundary--
diff --git 
a/shenyu-examples/shenyu-examples-springcloud/src/main/java/org/apache/shenyu/examples/springcloud/controller/UploadController.java
 
b/shenyu-examples/shenyu-examples-springcloud/src/main/java/org/apache/shenyu/examples/springcloud/controller/UploadController.java
new file mode 100644
index 000000000..d6aa25c89
--- /dev/null
+++ 
b/shenyu-examples/shenyu-examples-springcloud/src/main/java/org/apache/shenyu/examples/springcloud/controller/UploadController.java
@@ -0,0 +1,49 @@
+/*
+ * 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.examples.springcloud.controller;
+
+import org.apache.shenyu.client.springcloud.annotation.ShenyuSpringCloudClient;
+import org.apache.shenyu.examples.springcloud.dto.UserDTO;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * UploadController.
+ */
+@RestController
+@RequestMapping("/upload")
+@ShenyuSpringCloudClient(path = "/upload")
+public class UploadController {
+
+    /**
+     * Upload file.
+     *
+     * @param file file
+     * @return filename
+     */
+    @PostMapping(value = "/file", consumes = 
{MediaType.MULTIPART_FORM_DATA_VALUE})
+    @ShenyuSpringCloudClient(path = "/file")
+    public String post(@RequestPart("file") MultipartFile file) {
+        return file.getOriginalFilename();
+    }
+}
diff --git 
a/shenyu-integrated-test/shenyu-integrated-test-https/src/test/java/org/apache/shenyu/integrated/test/https/HttpsUploadFileTest.java
 
b/shenyu-integrated-test/shenyu-integrated-test-https/src/test/java/org/apache/shenyu/integrated/test/https/HttpsUploadFileTest.java
new file mode 100644
index 000000000..1c59484c3
--- /dev/null
+++ 
b/shenyu-integrated-test/shenyu-integrated-test-https/src/test/java/org/apache/shenyu/integrated/test/https/HttpsUploadFileTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.https;
+
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
+import org.apache.shenyu.integratedtest.common.AbstractTest;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public final class HttpsUploadFileTest extends AbstractTest {
+
+    private static final String FILE_PATH = "1.txt";
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(HttpsUploadFileTest.class);
+
+    @BeforeAll
+    public static void setup() throws IOException {
+        Path filePath = Paths.get(FILE_PATH);
+        if (!Files.exists(filePath)) {
+            Files.createFile(filePath);
+        }
+        try {
+            BufferedWriter bufferedWriterOne = 
Files.newBufferedWriter(filePath);
+            bufferedWriterOne.write("111");
+            bufferedWriterOne.flush();
+            bufferedWriterOne.close();
+        } catch (IOException e) {
+            LOG.error("write file fail", e);
+        }
+    }
+
+    @Test
+    public void testHttpsUploadFile() throws Exception {
+        File file = new File(FILE_PATH);
+        RequestBody fileBody = 
RequestBody.create(MediaType.parse("multipart/form-data"), file);
+        MultipartBody requestBody = new MultipartBody.Builder()
+                .setType(MultipartBody.FORM)
+                .addFormDataPart("file", FILE_PATH, fileBody)
+                .build();
+        final String response = 
HttpHelper.INSTANCE.postGateway("/order/upload/file", requestBody, 
String.class);
+        assertEquals(FILE_PATH, response);
+    }
+
+    @AfterAll
+    public static void clean() throws IOException {
+        Files.delete(Paths.get(FILE_PATH));
+    }
+}
diff --git 
a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java
 
b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java
new file mode 100644
index 000000000..a7e40cc72
--- /dev/null
+++ 
b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.integratedtest.springcloud;
+
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class SpringCloudPluginUploadTest extends AbstractPluginDataInit {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(SpringCloudPluginUploadTest.class);
+
+    private static final String FILE_PATH = "1.txt";
+
+    @BeforeAll
+    public static void setup() throws IOException {
+        String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), 
"");
+        assertThat(pluginResult, is("success"));
+
+        Path filePath = Paths.get(FILE_PATH);
+        if (!Files.exists(filePath)) {
+            Files.createFile(filePath);
+        }
+        try {
+            BufferedWriter bufferedWriterOne = 
Files.newBufferedWriter(filePath);
+            bufferedWriterOne.write("111");
+            bufferedWriterOne.flush();
+            bufferedWriterOne.close();
+        } catch (IOException e) {
+            LOG.error("write file fail", e);
+        }
+    }
+
+    @Test
+    public void testUploadFile() throws IOException {
+        File file = new File(FILE_PATH);
+        RequestBody fileBody = 
RequestBody.create(MediaType.parse("multipart/form-data"), file);
+        MultipartBody requestBody = new MultipartBody.Builder()
+                .setType(MultipartBody.FORM)
+                .addFormDataPart("file", FILE_PATH, fileBody)
+                .build();
+        final String response = 
HttpHelper.INSTANCE.postGateway("/springcloud/upload/file", requestBody, 
String.class);
+        assertEquals(FILE_PATH, response);
+    }
+
+    @AfterAll
+    public static void clean() throws IOException {
+        Files.delete(Paths.get(FILE_PATH));
+    }
+}

Reply via email to