Skip to content

Commit dbff4be

Browse files
committed
fix: 无法解析 localhost 这类省略 http:// 的 url
1 parent ba47523 commit dbff4be

File tree

17 files changed

+441
-235
lines changed

17 files changed

+441
-235
lines changed

forest-core/src/main/java/com/dtflys/forest/mapping/MappingURLTemplate.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,36 @@ public ForestURL render(Object[] args, ForestQueryMap queries) {
236236
}
237237
}
238238
if (host == null && StringUtils.isEmpty(path) && StringUtils.isNotEmpty(urlBuilder)) {
239-
if (path == null) {
240-
path = new StringBuilder();
239+
if (scheme == null) {
240+
String urlStr = urlBuilder.toString();
241+
if (!urlStr.startsWith("/")) {
242+
String[] urlStrGroup = urlStr.split(":");
243+
if (urlStrGroup.length > 1) {
244+
String urlGroup0 = urlStrGroup[0];
245+
String urlGroup1 = urlStrGroup[1];
246+
try {
247+
port = Integer.valueOf(urlGroup1);
248+
host = urlGroup0;
249+
} catch (Throwable th) {
250+
if (path == null) {
251+
path = new StringBuilder();
252+
}
253+
path.append(builder);
254+
}
255+
} else {
256+
String urlGroup0 = urlStrGroup[0];
257+
if (urlGroup0.equals("localhost") ||
258+
urlGroup0.matches("^(((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d)|([1-9]\\d)|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))$")) {
259+
host = urlGroup0;
260+
}
261+
}
262+
}
263+
} else {
264+
if (path == null) {
265+
path = new StringBuilder();
266+
}
267+
path.append(builder);
241268
}
242-
path.append(builder);
243269
}
244270
if (StringUtils.isNotEmpty(path)) {
245271
String[] group = path.toString().split("#", 2);

forest-core/src/test/java/com/dtflys/test/http/TestHttpClientHttpPorxy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public void afterRequests() {
5050
@Test
5151
public void testHttpClientHttpProxy() {
5252
//模拟https失败
53-
server.enqueue(new MockResponse().setBody(EXPECTED));
54-
String text = testHttpClientHttpProxyClient.testHttpProxy();
55-
System.out.println(text);
53+
// server.enqueue(new MockResponse().setBody(EXPECTED));
54+
// String text = testHttpClientHttpProxyClient.testHttpProxy();
55+
// System.out.println(text);
5656
// Assert.assertTrue(text != null && text.indexOf("百度一下,你就知道") != -1);
5757
}
5858

forest-core/src/test/java/com/dtflys/test/http/TestPostClient.java

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020
import com.dtflys.test.http.model.JsonTestUser3;
2121
import com.dtflys.test.http.model.JsonTestUser4;
2222
import com.dtflys.test.http.model.UserParam;
23-
import com.dtflys.test.http.model.XmlTestParam;
2423
import com.google.common.collect.Lists;
2524
import okhttp3.mockwebserver.MockResponse;
2625
import okhttp3.mockwebserver.MockWebServer;
2726
import org.junit.BeforeClass;
2827
import org.junit.Rule;
2928
import org.junit.Test;
3029
import org.mockito.Mockito;
31-
import org.slf4j.Logger;
32-
import org.slf4j.LoggerFactory;
3330

3431
import java.io.UnsupportedEncodingException;
3532
import java.net.URLEncoder;
@@ -1133,134 +1130,4 @@ public void postFormListWithBodyAnn_jsonParam() throws UnsupportedEncodingExcept
11331130
}
11341131

11351132

1136-
@Test
1137-
public void testPostXml() throws InterruptedException {
1138-
server.enqueue(new MockResponse().setBody(EXPECTED));
1139-
XmlTestParam testParam = new XmlTestParam();
1140-
testParam.setA(1);
1141-
testParam.setB(2);
1142-
assertThat(postClient.postXml(testParam))
1143-
.isNotNull()
1144-
.isEqualTo(EXPECTED);
1145-
mockRequest(server)
1146-
.assertMethodEquals("POST")
1147-
.assertPathEquals("/xml")
1148-
.assertHeaderEquals("Content-Type", "application/xml")
1149-
.assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1150-
"<misc>\n" +
1151-
" <a>1</a>\n" +
1152-
" <b>2</b>\n" +
1153-
"</misc>\n");
1154-
}
1155-
1156-
@Test
1157-
public void testPostXmlInDataProperty() throws InterruptedException {
1158-
server.enqueue(new MockResponse().setBody(EXPECTED));
1159-
XmlTestParam testParam = new XmlTestParam();
1160-
testParam.setA(1);
1161-
testParam.setB(2);
1162-
assertThat(postClient.postXmlInDataProperty(testParam))
1163-
.isNotNull()
1164-
.isEqualTo(EXPECTED);
1165-
mockRequest(server)
1166-
.assertMethodEquals("POST")
1167-
.assertPathEquals("/xml")
1168-
.assertHeaderEquals("Content-Type", "application/xml")
1169-
.assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1170-
"<misc>\n" +
1171-
" <a>1</a>\n" +
1172-
" <b>2</b>\n" +
1173-
"</misc>\n");
1174-
}
1175-
1176-
@Test
1177-
public void testPostXmlInDataProperty2() throws InterruptedException {
1178-
server.enqueue(new MockResponse().setBody(EXPECTED));
1179-
XmlTestParam testParam = new XmlTestParam();
1180-
testParam.setA(1);
1181-
testParam.setB(2);
1182-
assertThat(postClient.postXmlInDataProperty2(testParam))
1183-
.isNotNull()
1184-
.isEqualTo(EXPECTED);
1185-
mockRequest(server)
1186-
.assertMethodEquals("POST")
1187-
.assertPathEquals("/xml")
1188-
.assertHeaderEquals("Content-Type", "application/xml")
1189-
.assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1190-
"<misc>\n" +
1191-
" <a>1</a>\n" +
1192-
" <b>2</b>\n" +
1193-
"</misc>\n");
1194-
}
1195-
1196-
@Test
1197-
public void testPostXmlWithXMLBodyAnn() throws InterruptedException {
1198-
server.enqueue(new MockResponse().setBody(EXPECTED));
1199-
XmlTestParam testParam = new XmlTestParam();
1200-
testParam.setA(1);
1201-
testParam.setB(2);
1202-
assertThat(postClient.postXmlWithXMLBodyAnn(testParam))
1203-
.isNotNull()
1204-
.isEqualTo(EXPECTED);
1205-
mockRequest(server)
1206-
.assertMethodEquals("POST")
1207-
.assertPathEquals("/xml")
1208-
.assertHeaderEquals("Content-Type", "application/xml")
1209-
.assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1210-
"<misc>\n" +
1211-
" <a>1</a>\n" +
1212-
" <b>2</b>\n" +
1213-
"</misc>\n");
1214-
}
1215-
1216-
@Test
1217-
public void testPostXmlBodyString() throws InterruptedException {
1218-
server.enqueue(new MockResponse().setBody(EXPECTED));
1219-
assertThat(postClient.postXmlBodyString(
1220-
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1221-
"<misc>\n" +
1222-
" <a>1</a>\n" +
1223-
" <b>2</b>\n" +
1224-
"</misc>\n"))
1225-
.isNotNull()
1226-
.isEqualTo(EXPECTED);
1227-
mockRequest(server)
1228-
.assertMethodEquals("POST")
1229-
.assertPathEquals("/xml")
1230-
.assertHeaderEquals("Content-Type", "application/xml")
1231-
.assertBodyEquals(
1232-
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1233-
"<misc>\n" +
1234-
" <a>1</a>\n" +
1235-
" <b>2</b>\n" +
1236-
"</misc>\n");
1237-
}
1238-
1239-
1240-
@Test
1241-
public void testPostXmlWithXMLBodyAnnAndReturnObj() throws InterruptedException {
1242-
server.enqueue(new MockResponse()
1243-
.setBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1244-
"<misc>\n" +
1245-
" <a>3</a>\n" +
1246-
" <b>4</b>\n" +
1247-
"</misc>\n"));
1248-
XmlTestParam testParam = new XmlTestParam();
1249-
testParam.setA(1);
1250-
testParam.setB(2);
1251-
assertThat(postClient.postXmlWithXMLBodyAnnAndReturnObj(testParam))
1252-
.isNotNull()
1253-
.extracting(XmlTestParam::getA, XmlTestParam::getB)
1254-
.contains(10, 20);
1255-
mockRequest(server)
1256-
.assertMethodEquals("POST")
1257-
.assertPathEquals("/xml-response")
1258-
.assertHeaderEquals("Content-Type", "application/xml")
1259-
.assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
1260-
"<misc>\n" +
1261-
" <a>1</a>\n" +
1262-
" <b>2</b>\n" +
1263-
"</misc>\n");
1264-
}
1265-
12661133
}

forest-core/src/test/java/com/dtflys/test/http/client/PostClient.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import com.dtflys.forest.backend.ContentType;
55
import com.dtflys.forest.http.ForestRequest;
66
import com.dtflys.forest.http.ForestResponse;
7-
import com.dtflys.forest.http.Lazy;
87
import com.dtflys.test.http.model.*;
98
import com.dtflys.test.http.model.UserParam;
10-
import com.dtflys.test.http.model.XmlTestParam;
119
import com.dtflys.test.interceptor.PostHeadInterceptor;
12-
import com.dtflys.test.interceptor.XmlResponseInterceptor;
1310

1411
import java.util.List;
1512
import java.util.Map;
@@ -501,41 +498,4 @@ String postJsonObjFromMultipleBodyAnnParams2(
501498
String postJsonDate(@Body JsonTestDate jsonTestDate);
502499

503500

504-
@Request(
505-
url = "http://localhost:{port}/xml",
506-
type = "post",
507-
contentType = "application/xml"
508-
)
509-
String postXml(@Body(filter = "xml") XmlTestParam testParam);
510-
511-
512-
@Request(
513-
url = "http://localhost:{port}/xml",
514-
type = "post",
515-
contentType = "application/xml",
516-
data = "{xml(misc)}"
517-
)
518-
String postXmlInDataProperty(@DataVariable("misc") XmlTestParam testParam);
519-
520-
521-
@Request(
522-
url = "http://localhost:{port}/xml",
523-
type = "post",
524-
contentType = "application/xml",
525-
data = "{xml($0)}"
526-
)
527-
String postXmlInDataProperty2(XmlTestParam testParam);
528-
529-
530-
@Post("http://localhost:{port}/xml")
531-
String postXmlWithXMLBodyAnn(@XMLBody XmlTestParam testParam);
532-
533-
534-
@Post("http://localhost:{port}/xml")
535-
String postXmlBodyString(@XMLBody String xml);
536-
537-
@Post(url = "http://localhost:{port}/xml-response", interceptor = XmlResponseInterceptor.class)
538-
XmlTestParam postXmlWithXMLBodyAnnAndReturnObj(@XMLBody XmlTestParam testParam);
539-
540-
541501
}

forest-core/src/test/java/com/dtflys/test/http/client/XmlClient.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

forest-core/src/test/java/com/dtflys/test/http/encoder/TestEncoderClient.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ public void testEncoder2() {
6767
.assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED)
6868
.assertBodyEquals("{\"name\":\"AAA\",\"value\":\"BBB\"}");
6969

70-
server.enqueue(new MockResponse().setBody(EXPECTED));
71-
request = encoderClient.testEncoder2("xml", entry);
72-
assertThat(request).isNotNull();
73-
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.XML);
74-
request.execute();
75-
mockRequest(server)
76-
.assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED)
77-
.assertBodyEquals(
78-
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
79-
"<entry>\n" +
80-
" <name>AAA</name>\n" +
81-
" <value>BBB</value>\n" +
82-
"</entry>\n");
83-
8470
server.enqueue(new MockResponse().setBody(EXPECTED));
8571
request = encoderClient.testEncoder2("form", entry);
8672
assertThat(request).isNotNull();

forest-jaxb/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@
4343
<scope>test</scope>
4444
</dependency>
4545

46+
<dependency>
47+
<groupId>com.dtflys.forest</groupId>
48+
<artifactId>forest-mock</artifactId>
49+
<version>1.5.30</version>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>com.alibaba</groupId>
55+
<artifactId>fastjson</artifactId>
56+
<version>1.2.83</version>
57+
<scope>provided</scope>
58+
</dependency>
59+
60+
4661
</dependencies>
4762

4863

0 commit comments

Comments
 (0)