Merge branch 'main' into add-feature-scripts

This commit is contained in:
Ankit Mahato
2025-06-21 20:39:25 +05:30
committed by GitHub
27 changed files with 314 additions and 60 deletions

View File

@@ -30,6 +30,8 @@ final kSupportedUriSchemes =
SupportedUriSchemes.values.map((i) => i.name).toList();
const kDefaultUriScheme = SupportedUriSchemes.https;
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 = [
HTTPVerb.post,

View File

@@ -2,6 +2,7 @@ import 'package:apidash_core/consts.dart';
import 'package:seed/seed.dart';
import '../models/models.dart';
import 'graphql_utils.dart';
import 'package:json5/json5.dart' as json5;
Map<String, String>? rowsToMap(
List<NameValueModel>? kvRows, {
@@ -100,3 +101,14 @@ String? getRequestBody(APIType type, HttpRequestModel httpRequestModel) {
APIType.graphql => getGraphQLBody(httpRequestModel),
};
}
// TODO: Expose this function to remove JSON comments
String? removeJsonComments(String? json) {
try {
if (json == null) return null;
var parsed = json5.json5Decode(json);
return kJsonEncoder.convert(parsed);
} catch (e) {
return json;
}
}

View File

@@ -30,9 +30,10 @@ String stripUrlParams(String url) {
return (null, "URL is missing!");
}
if (kLocalhostRegex.hasMatch(url)) {
if (kLocalhostRegex.hasMatch(url) || kIPHostRegex.hasMatch(url)) {
url = '${SupportedUriSchemes.http.name}://$url';
}
Uri? uri = Uri.tryParse(url);
if (uri == null) {
return (null, "Check URL (malformed)");

View File

@@ -19,6 +19,7 @@ dependencies:
http_parser: ^4.1.2
insomnia_collection:
path: ../insomnia_collection
json5: ^0.8.2
postman:
path: ../postman
seed: ^0.0.3

View File

@@ -0,0 +1,74 @@
import 'package:apidash_core/utils/http_request_utils.dart';
import 'package:test/test.dart';
void main() {
group('Testing RemoveJsonComments', () {
test('Removes single-line comments', () {
String input = '''
{
// This is a single-line comment
"key": "value"
}
''';
String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});
test('Removes multi-line comments', () {
String input = '''
{
/*
This is a multi-line comment
*/
"key": "value"
}
''';
String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});
test('Handles valid JSON without comments', () {
String input = '{"key":"value"}';
String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});
test('Returns original string if invalid JSON', () {
String input = '{key: value}';
String expected = '{key: value}';
expect(removeJsonComments(input), expected);
});
test('Removes trailing commas', () {
String input = '''
{
"key1": "value1",
"key2": "value2", // trailing comma
}
''';
String expected = '''{
"key1": "value1",
"key2": "value2"
}''';
expect(removeJsonComments(input), expected);
});
test('Test blank json', () {
String input = '''
{}
''';
String expected = '{}';
expect(removeJsonComments(input), expected);
});
});
}

View File

@@ -62,6 +62,37 @@ void main() {
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', () {
String url1 = "https://api.apidash.dev/country/data";
const kvRow1 = NameValueModel(name: "code", value: "US");