@@ -129,6 +129,62 @@ public void testProxyUrl1() throws Exception {
129
129
130
130
}
131
131
132
+ /**
133
+ * 本机ip有127.0.0.1、10.0.0.1
134
+ */
135
+ @ Test
136
+ public void testProxyUrl2 () throws Exception {
137
+ // 启动后端
138
+ vertx .createHttpServer ().requestHandler (req -> {
139
+ req .response ().end (req .absoluteURI ());
140
+ }).listen (888 );
141
+ // 启动代理
142
+ ReverseHttpProxy .create (vertx )
143
+ .addRoute (new ProxyRoute ()
144
+ .setSourceUrl ("/*" )
145
+ .setTargetUrl ("http://127.0.0.1:888" ), Integer .MAX_VALUE )
146
+ .addRoute (new ProxyRoute ()
147
+ .setSourceUrl ("/api/*" )
148
+ .setTargetUrl ("http://10.0.0.1:888" ), 2 )
149
+ .addRoute (new ProxyRoute ()
150
+ .setSourceUrl ("/service/*" )
151
+ .setTargetUrl ("http://10.0.0.1:888/api" ), 1 )
152
+ .addRoute (new ProxyRoute ()
153
+ .setSourceUrl ("/specific" )
154
+ .setTargetUrl ("http://10.0.0.1:888" ), 1 )
155
+ .addRoute (new ProxyRoute ()
156
+ .setSourceUrl ("/static/" )
157
+ .setTargetUrl ("http://10.0.0.1:888" ), 2 )
158
+ .port (8080 ).start ();
159
+
160
+ // 测试用例
161
+ // key为测试样例,value为正确返回结果
162
+ Map <String , String > cases = new LinkedHashMap <>();
163
+ cases .put ("http://127.0.0.1:8080/" , "http://127.0.0.1:888/" );
164
+ cases .put ("http://127.0.0.1:8080/test" , "http://127.0.0.1:888/test" );
165
+ cases .put ("http://127.0.0.1:8080/test/" , "http://127.0.0.1:888/test/" );
166
+ cases .put ("http://127.0.0.1:8080/path?x=1&y=2" , "http://127.0.0.1:888/path?x=1&y=2" );
167
+
168
+ cases .put ("http://127.0.0.1:8080/api/" , "http://10.0.0.1:888/" );
169
+ cases .put ("http://127.0.0.1:8080/api/v1/resource " , "http://10.0.0.1:888/v1/resource" );
170
+ cases .put ("http://127.0.0.1:8080/api/v2?param=abc" , "http://10.0.0.1:888/v2?param=abc" );
171
+
172
+
173
+ cases .put ("http://127.0.0.1:8080/service/" , "http://10.0.0.1:888/api/" );
174
+ cases .put ("http://127.0.0.1:8080/service/user/info" , "http://10.0.0.1:888/api/user/info" );
175
+ cases .put ("http://127.0.0.1:8080/service/query?id=42" , "http://10.0.0.1:888/api/query?id=42" );
176
+ cases .put ("http://127.0.0.1:8080/service/details?x=1&y=2" , "http://10.0.0.1:888/api/details?x=1&y=2" );
177
+
178
+
179
+ // 注意这种直接以端口结尾的,实际都是在端口后面加了一级/
180
+ cases .put ("http://127.0.0.1:8080/specific" , "http://10.0.0.1:888/" );
181
+ cases .put ("http://127.0.0.1:8080/specific?a=1" , "http://10.0.0.1:888/?a=1" );
182
+ cases .put ("http://127.0.0.1:8080/static/" , "http://10.0.0.1:888/" );
183
+ cases .put ("http://127.0.0.1:8080/static/img" , "http://127.0.0.1:888/static/img" );
184
+
185
+ proxyUrlCommon (cases );
186
+ }
187
+
132
188
private void proxyUrlCommon (Map <String , String > cases ) throws InterruptedException , ExecutionException {
133
189
HttpClient httpClient = Vertx .vertx ().createHttpClient ();
134
190
0 commit comments