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

sabersola 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 b96b7d7  Add integrated test for jwt (#1920)
b96b7d7 is described below

commit b96b7d73136b8c39688b89bab0573f8ccde0f427
Author: Kunshuai Zhu <[email protected]>
AuthorDate: Thu Aug 12 20:40:49 2021 -0500

    Add integrated test for jwt (#1920)
---
 .../admin-config/schema.sql                        | 10 +++-
 .../shenyu-integrated-test-http/pom.xml            |  8 +++
 .../apache/shenyu/integratedtest/http/JwtTest.java | 57 ++++++++++++++++++++++
 3 files changed, 74 insertions(+), 1 deletion(-)

diff --git 
a/shenyu-integrated-test/shenyu-integrated-test-http/admin-config/schema.sql 
b/shenyu-integrated-test/shenyu-integrated-test-http/admin-config/schema.sql
index 1d032b2..ed6440d 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/admin-config/schema.sql
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/admin-config/schema.sql
@@ -54,4 +54,12 @@ INSERT IGNORE INTO rule (id, selector_id, match_mode, name, 
enabled, loged, sort
 INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1424403555627470848', '1424403555619082240', 'uri', '=', '/', 
'/http/test/path/111', '2021-08-09 00:22:01', '2021-08-09 00:22:01');
 INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1424404768645353472', '1424403555619082240', 'query', '=', 'name', 
'redirectToOuterPath', '2021-08-09 00:22:01', '2021-08-09 00:22:01');
 INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1424405341339815936', '1424405341335621632', 'uri', '=', '/', 
'/http/test/path/111', '2021-08-09 00:30:21', '2021-08-09 00:30:22');
-INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1424405341344010240', '1424405341335621632', 'query', '=', 'name', 
'redirectToInnerPath', '2021-08-09 00:30:21', '2021-08-09 00:30:22');
\ No newline at end of file
+INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1424405341344010240', '1424405341335621632', 'query', '=', 'name', 
'redirectToInnerPath', '2021-08-09 00:30:21', '2021-08-09 00:30:22');
+
+/** prepare jwt **/
+UPDATE plugin SET config = 
'{"secretKey":"key00000","filterPath":"/http/test/path/1111/name"}', enabled = 
1 WHERE id = '19';
+INSERT IGNORE INTO selector (id, plugin_id, name, match_mode, type, sort, 
handle, enabled, loged, continued, date_created, date_updated) VALUES 
('1425390877916254208', '19', 'http-jwt', 0, 1, 1, null, 1, 1, 1,'2021-08-11 
17:37:22','2021-08-11 21:08:25');
+INSERT IGNORE INTO selector_condition (id, selector_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1425390877920448512', '1425390877916254208', 'uri', 'match', '/', 
'/http/**','2021-08-11 17:37:22','2021-08-11 17:37:22');
+INSERT IGNORE INTO rule (id, selector_id, match_mode, name, enabled, loged, 
sort, date_created, date_updated) VALUES ('1425390970652315648', 
'1425390877916254208', 1, 'http-jwt', 1, 1, 1,'2021-08-11 17:37:45','2021-08-11 
21:07:26');
+INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1425450010986262528', '1425390970652315648', 'uri', '=', '/', 
'/http/test/findByUserId','2021-08-04 13:09:32','2021-08-04 13:09:32');
+INSERT IGNORE INTO rule_condition (id, rule_id, param_type, operator, 
param_name, param_value, date_created, date_updated) VALUES 
('1425479564999307264', '1425390970652315648', 'uri', '=', '/', 
'/http/test/path/1111/name','2021-08-04 13:09:32','2021-08-04 13:09:32');
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml 
b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
index 03c3cb4..b85c8fa 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml
@@ -81,6 +81,14 @@
     </dependency>
     <!-- apache shenyu redirect plugin end-->
 
+    <!-- shenyu jwt plugin starter-->
+    <dependency>
+      <groupId>org.apache.shenyu</groupId>
+      <artifactId>shenyu-spring-boot-starter-plugin-jwt</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <!-- shenyu jwt plugin end-->
+
     <!--shenyu data sync start use websocket-->
     <dependency>
       <groupId>org.apache.shenyu</groupId>
diff --git 
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integratedtest/http/JwtTest.java
 
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integratedtest/http/JwtTest.java
new file mode 100644
index 0000000..4381bc9
--- /dev/null
+++ 
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integratedtest/http/JwtTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.http;
+
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.apache.shenyu.integratedtest.http.helper.HttpHelper;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class JwtTest extends AbstractTest {
+
+    @Test
+    public void testJwt() throws IOException {
+        final String key = "key00000";
+        final String testPath = "/http/test/findByUserId?userId=1001";
+        final String filterPath = "/http/test/path/1111/name";
+        final String token = 
Jwts.builder().setId("1001").signWith(SignatureAlgorithm.HS256, 
key.getBytes(StandardCharsets.UTF_8)).compact();
+        Map<String, Object> headers = new HashMap<>();
+
+        // send request to filterPath
+        Map<String, Object> filterResponse = 
HttpHelper.INSTANCE.getFromGateway(filterPath, Map.class);
+        assertThat(filterResponse.get("userId"), is("1111"));
+
+        // send request with fake jwt
+        headers.put("token", "fake.token.me");
+        Map<String, Object> errorResponse = 
HttpHelper.INSTANCE.getFromGateway(testPath, headers, Map.class);
+        assertThat(errorResponse.get("message"), is("Illegal authorization"));
+
+        // send request with jwt
+        headers.put("token", token);
+        Map<String, Object> correctResponse = 
HttpHelper.INSTANCE.getFromGateway(testPath, headers, Map.class);
+        assertThat(correctResponse.get("userId"), is("1001"));
+    }
+}

Reply via email to