mirror of
https://github.com/foss42/apidash.git
synced 2025-12-08 22:20:44 +08:00
Merge pull request #847 from surelyruchi/resolve-url-parsing-ip-port
Fix: prevent malformed URL error when parsing IP:PORT by adding defau…
This commit is contained in:
@@ -30,6 +30,8 @@ final kSupportedUriSchemes =
|
|||||||
SupportedUriSchemes.values.map((i) => i.name).toList();
|
SupportedUriSchemes.values.map((i) => i.name).toList();
|
||||||
const kDefaultUriScheme = SupportedUriSchemes.https;
|
const kDefaultUriScheme = SupportedUriSchemes.https;
|
||||||
final kLocalhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$');
|
final kLocalhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$');
|
||||||
|
final kIPHostRegex =
|
||||||
|
RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}(:\d+)?(/.*)?$');
|
||||||
|
|
||||||
const kMethodsWithBody = [
|
const kMethodsWithBody = [
|
||||||
HTTPVerb.post,
|
HTTPVerb.post,
|
||||||
|
|||||||
@@ -30,9 +30,10 @@ String stripUrlParams(String url) {
|
|||||||
return (null, "URL is missing!");
|
return (null, "URL is missing!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kLocalhostRegex.hasMatch(url)) {
|
if (kLocalhostRegex.hasMatch(url) || kIPHostRegex.hasMatch(url)) {
|
||||||
url = '${SupportedUriSchemes.http.name}://$url';
|
url = '${SupportedUriSchemes.http.name}://$url';
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri? uri = Uri.tryParse(url);
|
Uri? uri = Uri.tryParse(url);
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
return (null, "Check URL (malformed)");
|
return (null, "Check URL (malformed)");
|
||||||
|
|||||||
@@ -62,6 +62,37 @@ void main() {
|
|||||||
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Testing getValidRequestUri with IP URL without port or path', () {
|
||||||
|
String url1 = "8.8.8.8";
|
||||||
|
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8');
|
||||||
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing getValidRequestUri with IP URL with port', () {
|
||||||
|
String url1 = "8.8.8.8:8080";
|
||||||
|
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 8080);
|
||||||
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing getValidRequestUri with IP URL with port and path', () {
|
||||||
|
String url1 = "8.8.8.8:8080/hello";
|
||||||
|
Uri uri1Expected =
|
||||||
|
Uri(scheme: 'http', host: '8.8.8.8', port: 8080, path: '/hello');
|
||||||
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing getValidRequestUri with IP URL with http prefix', () {
|
||||||
|
String url1 = "http://8.8.8.8:3080";
|
||||||
|
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 3080);
|
||||||
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing getValidRequestUri with IP URL with https prefix', () {
|
||||||
|
String url1 = "https://8.8.8.8:8080";
|
||||||
|
Uri uri1Expected = Uri(scheme: 'https', host: '8.8.8.8', port: 8080);
|
||||||
|
expect(getValidRequestUri(url1, []), (uri1Expected, null));
|
||||||
|
});
|
||||||
|
|
||||||
test('Testing getValidRequestUri for normal values', () {
|
test('Testing getValidRequestUri for normal values', () {
|
||||||
String url1 = "https://api.apidash.dev/country/data";
|
String url1 = "https://api.apidash.dev/country/data";
|
||||||
const kvRow1 = NameValueModel(name: "code", value: "US");
|
const kvRow1 = NameValueModel(name: "code", value: "US");
|
||||||
|
|||||||
Reference in New Issue
Block a user