mirror of
https://github.com/foss42/apidash.git
synced 2025-05-21 08:16:29 +08:00
Implemented FormData parsing in curl_parser and added tests for it.
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
|
import 'package:apidash_core/consts.dart';
|
||||||
|
import 'package:apidash_core/models/form_data_model.dart';
|
||||||
import 'package:args/args.dart';
|
import 'package:args/args.dart';
|
||||||
|
import 'package:curl_parser/utils/string.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import '../../utils/string.dart';
|
|
||||||
|
|
||||||
/// A representation of a cURL command in Dart.
|
/// A representation of a cURL command in Dart.
|
||||||
///
|
///
|
||||||
@ -10,7 +12,7 @@ class Curl extends Equatable {
|
|||||||
/// Specifies the HTTP request method (e.g., GET, POST, PUT, DELETE).
|
/// Specifies the HTTP request method (e.g., GET, POST, PUT, DELETE).
|
||||||
final String method;
|
final String method;
|
||||||
|
|
||||||
/// Specifies the HTTP request URL
|
/// Specifies the HTTP request URL.
|
||||||
final Uri uri;
|
final Uri uri;
|
||||||
|
|
||||||
/// Adds custom HTTP headers to the request.
|
/// Adds custom HTTP headers to the request.
|
||||||
@ -34,6 +36,10 @@ class Curl extends Equatable {
|
|||||||
/// Sends data as a multipart/form-data request.
|
/// Sends data as a multipart/form-data request.
|
||||||
final bool form;
|
final bool form;
|
||||||
|
|
||||||
|
/// Form data list.
|
||||||
|
/// Currently, it is represented as a list of key-value pairs ([key, value]).
|
||||||
|
final List<FormDataModel>? formData;
|
||||||
|
|
||||||
/// Allows insecure SSL connections.
|
/// Allows insecure SSL connections.
|
||||||
final bool insecure;
|
final bool insecure;
|
||||||
|
|
||||||
@ -42,7 +48,7 @@ class Curl extends Equatable {
|
|||||||
|
|
||||||
/// Constructs a new Curl object with the specified parameters.
|
/// Constructs a new Curl object with the specified parameters.
|
||||||
///
|
///
|
||||||
/// The uri parameter is required, while the remaining parameters are optional.
|
/// The `uri` parameter is required, while the remaining parameters are optional.
|
||||||
Curl({
|
Curl({
|
||||||
required this.uri,
|
required this.uri,
|
||||||
this.method = 'GET',
|
this.method = 'GET',
|
||||||
@ -52,12 +58,18 @@ class Curl extends Equatable {
|
|||||||
this.user,
|
this.user,
|
||||||
this.referer,
|
this.referer,
|
||||||
this.userAgent,
|
this.userAgent,
|
||||||
|
this.formData,
|
||||||
this.form = false,
|
this.form = false,
|
||||||
this.insecure = false,
|
this.insecure = false,
|
||||||
this.location = false,
|
this.location = false,
|
||||||
});
|
}) {
|
||||||
|
assert(
|
||||||
|
['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'].contains(method));
|
||||||
|
assert(['http', 'https'].contains(uri.scheme));
|
||||||
|
assert(form ? formData != null : formData == null);
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse [curlString] as a [Curl] class instance.
|
/// Parses [curlString] into a [Curl] class instance.
|
||||||
///
|
///
|
||||||
/// Like [parse] except that this function returns `null` where a
|
/// Like [parse] except that this function returns `null` where a
|
||||||
/// similar call to [parse] would throw a throwable.
|
/// similar call to [parse] would throw a throwable.
|
||||||
@ -70,8 +82,9 @@ class Curl extends Equatable {
|
|||||||
static Curl? tryParse(String curlString) {
|
static Curl? tryParse(String curlString) {
|
||||||
try {
|
try {
|
||||||
return Curl.parse(curlString);
|
return Curl.parse(curlString);
|
||||||
} catch (_) {}
|
} catch (_) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse [curlString] as a [Curl] class instance.
|
/// Parse [curlString] as a [Curl] class instance.
|
||||||
@ -88,6 +101,8 @@ class Curl extends Equatable {
|
|||||||
|
|
||||||
final parser = ArgParser(allowTrailingOptions: true);
|
final parser = ArgParser(allowTrailingOptions: true);
|
||||||
|
|
||||||
|
// TODO: Add more options
|
||||||
|
// https://gist.github.com/eneko/dc2d8edd9a4b25c5b0725dd123f98b10
|
||||||
// Define the expected options
|
// Define the expected options
|
||||||
parser.addOption('url');
|
parser.addOption('url');
|
||||||
parser.addOption('request', abbr: 'X');
|
parser.addOption('request', abbr: 'X');
|
||||||
@ -98,7 +113,7 @@ class Curl extends Equatable {
|
|||||||
parser.addOption('referer', abbr: 'e');
|
parser.addOption('referer', abbr: 'e');
|
||||||
parser.addOption('user-agent', abbr: 'A');
|
parser.addOption('user-agent', abbr: 'A');
|
||||||
parser.addFlag('head', abbr: 'I');
|
parser.addFlag('head', abbr: 'I');
|
||||||
parser.addFlag('form', abbr: 'F');
|
parser.addMultiOption('form', abbr: 'F');
|
||||||
parser.addFlag('insecure', abbr: 'k');
|
parser.addFlag('insecure', abbr: 'k');
|
||||||
parser.addFlag('location', abbr: 'L');
|
parser.addFlag('location', abbr: 'L');
|
||||||
|
|
||||||
@ -111,7 +126,15 @@ class Curl extends Equatable {
|
|||||||
|
|
||||||
final result = parser.parse(splittedCurlString);
|
final result = parser.parse(splittedCurlString);
|
||||||
|
|
||||||
final method = (result['request'] as String?)?.toUpperCase();
|
// A potential alternative to the above code
|
||||||
|
// final curlString = curlString.replaceAll("\\\n", " ");
|
||||||
|
// final tokens = shlex.split(curlString);
|
||||||
|
// final result = parser.parse(tokens);
|
||||||
|
// if (!result.arguments.contains('curl')) {
|
||||||
|
// throw Exception('Invalid cURL command');
|
||||||
|
// }
|
||||||
|
|
||||||
|
String? method = (result['request'] as String?)?.toUpperCase();
|
||||||
|
|
||||||
// Extract the request headers
|
// Extract the request headers
|
||||||
Map<String, String>? headers;
|
Map<String, String>? headers;
|
||||||
@ -129,26 +152,57 @@ class Curl extends Equatable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String? url = clean(result['url']);
|
// Parse form data
|
||||||
|
List<FormDataModel>? formData;
|
||||||
|
if (result['form'] is List<String> &&
|
||||||
|
(result['form'] as List<String>).isNotEmpty) {
|
||||||
|
formData = <FormDataModel>[];
|
||||||
|
for (final formEntry in result['form']) {
|
||||||
|
final pairs = formEntry.split('=');
|
||||||
|
if (pairs.length != 2) {
|
||||||
|
throw Exception('Form data is not in key=value format');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handling the file or text type
|
||||||
|
FormDataModel formDataModel = pairs[1].startsWith('@')
|
||||||
|
? FormDataModel(
|
||||||
|
name: pairs[0],
|
||||||
|
value: pairs[1].substring(1),
|
||||||
|
type: FormDataType.file,
|
||||||
|
)
|
||||||
|
: FormDataModel(
|
||||||
|
name: pairs[0],
|
||||||
|
value: pairs[1],
|
||||||
|
type: FormDataType.text,
|
||||||
|
);
|
||||||
|
|
||||||
|
formData.add(formDataModel);
|
||||||
|
}
|
||||||
|
headers ??= <String, String>{};
|
||||||
|
headers['Content-Type'] = 'multipart/form-data';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle URL and query parameters
|
||||||
|
String? url = clean(result['url']) ??
|
||||||
|
(result.rest.isNotEmpty ? clean(result.rest.first) : null);
|
||||||
|
if (url == null) {
|
||||||
|
throw Exception('URL is null');
|
||||||
|
}
|
||||||
|
final uri = Uri.parse(url);
|
||||||
|
|
||||||
|
method = result['head'] == true ? 'HEAD' : (method ?? 'GET');
|
||||||
final String? data = result['data'];
|
final String? data = result['data'];
|
||||||
final String? cookie = result['cookie'];
|
final String? cookie = result['cookie'];
|
||||||
final String? user = result['user'];
|
final String? user = result['user'];
|
||||||
final String? referer = result['referer'];
|
final String? referer = result['referer'];
|
||||||
final String? userAgent = result['user-agent'];
|
final String? userAgent = result['user-agent'];
|
||||||
final bool form = result['form'] ?? false;
|
final bool form = formData != null && formData.isNotEmpty;
|
||||||
final bool head = result['head'] ?? false;
|
|
||||||
final bool insecure = result['insecure'] ?? false;
|
final bool insecure = result['insecure'] ?? false;
|
||||||
final bool location = result['location'] ?? false;
|
final bool location = result['location'] ?? false;
|
||||||
|
|
||||||
// Extract the request URL
|
// Extract the request URL
|
||||||
url ??= result.rest.isNotEmpty ? clean(result.rest.first) : null;
|
|
||||||
if (url == null) {
|
|
||||||
throw Exception('url is null');
|
|
||||||
}
|
|
||||||
final uri = Uri.parse(url);
|
|
||||||
|
|
||||||
return Curl(
|
return Curl(
|
||||||
method: head ? "HEAD" : (method ?? 'GET'),
|
method: method,
|
||||||
uri: uri,
|
uri: uri,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
data: data,
|
data: data,
|
||||||
@ -157,12 +211,13 @@ class Curl extends Equatable {
|
|||||||
referer: referer,
|
referer: referer,
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
form: form,
|
form: form,
|
||||||
|
formData: formData,
|
||||||
insecure: insecure,
|
insecure: insecure,
|
||||||
location: location,
|
location: location,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatted cURL command
|
/// Converts the Curl object to a formatted cURL command string.
|
||||||
String toCurlString() {
|
String toCurlString() {
|
||||||
var cmd = 'curl ';
|
var cmd = 'curl ';
|
||||||
|
|
||||||
@ -176,7 +231,6 @@ class Curl extends Equatable {
|
|||||||
|
|
||||||
// Add the URL
|
// Add the URL
|
||||||
cmd += '"${Uri.encodeFull(uri.toString())}" ';
|
cmd += '"${Uri.encodeFull(uri.toString())}" ';
|
||||||
|
|
||||||
// Add the headers
|
// Add the headers
|
||||||
headers?.forEach((key, value) {
|
headers?.forEach((key, value) {
|
||||||
cmd += '\\\n -H "$key: $value" ';
|
cmd += '\\\n -H "$key: $value" ';
|
||||||
@ -204,7 +258,10 @@ class Curl extends Equatable {
|
|||||||
}
|
}
|
||||||
// Add the form flag
|
// Add the form flag
|
||||||
if (form) {
|
if (form) {
|
||||||
cmd += " \\\n -F ";
|
for (final formEntry in formData!) {
|
||||||
|
cmd += '\\\n -F ';
|
||||||
|
cmd += '"${formEntry.name}=${formEntry.value}" ';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Add the insecure flag
|
// Add the insecure flag
|
||||||
if (insecure) {
|
if (insecure) {
|
||||||
@ -229,6 +286,7 @@ class Curl extends Equatable {
|
|||||||
referer,
|
referer,
|
||||||
userAgent,
|
userAgent,
|
||||||
form,
|
form,
|
||||||
|
formData,
|
||||||
insecure,
|
insecure,
|
||||||
location,
|
location,
|
||||||
];
|
];
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
name: curl_parser
|
name: curl_parser
|
||||||
description: Parse cURL command to Dart object and convert Dart object to cURL command.
|
description: Parse cURL command to Dart object and convert Dart object to cURL command.
|
||||||
version: 0.1.0
|
version: 0.1.0
|
||||||
|
publish_to: none
|
||||||
homepage: https://github.com/foss42/apidash/tree/main/packages/curl_parser
|
homepage: https://github.com/foss42/apidash/tree/main/packages/curl_parser
|
||||||
repository: https://github.com/foss42/apidash/tree/main/packages/curl_parser
|
repository: https://github.com/foss42/apidash/tree/main/packages/curl_parser
|
||||||
issue_tracker: https://github.com/foss42/apidash/issues
|
issue_tracker: https://github.com/foss42/apidash/issues
|
||||||
@ -20,6 +21,8 @@ dependencies:
|
|||||||
args: ^2.5.0
|
args: ^2.5.0
|
||||||
equatable: ^2.0.5
|
equatable: ^2.0.5
|
||||||
shlex: ^2.0.2
|
shlex: ^2.0.2
|
||||||
|
apidash_core:
|
||||||
|
path: ../apidash_core
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
lints: ^2.1.0
|
lints: ^2.1.0
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:curl_parser/models/curl.dart';
|
import 'package:apidash_core/consts.dart';
|
||||||
|
import 'package:apidash_core/models/form_data_model.dart';
|
||||||
|
import 'package:curl_parser/curl_parser.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
const defaultTimeout = Timeout(Duration(seconds: 3));
|
const defaultTimeout = Timeout(Duration(seconds: 3));
|
||||||
@ -27,6 +29,104 @@ void main() {
|
|||||||
);
|
);
|
||||||
}, timeout: defaultTimeout);
|
}, timeout: defaultTimeout);
|
||||||
|
|
||||||
|
test('parse POST request with form-data', () {
|
||||||
|
const curl = r'''curl -X POST https://api.apidash.dev/upload \\
|
||||||
|
-F "file=@/path/to/image.jpg" \\
|
||||||
|
-F "username=john"
|
||||||
|
''';
|
||||||
|
|
||||||
|
expect(
|
||||||
|
Curl.parse(curl),
|
||||||
|
Curl(
|
||||||
|
method: 'POST',
|
||||||
|
uri: Uri.parse('https://api.apidash.dev/upload'),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
},
|
||||||
|
form: true,
|
||||||
|
formData: [
|
||||||
|
FormDataModel(
|
||||||
|
name: "file",
|
||||||
|
value: "/path/to/image.jpg",
|
||||||
|
type: FormDataType.file),
|
||||||
|
FormDataModel(
|
||||||
|
name: "username", value: "john", type: FormDataType.text)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parse POST request with form-data including a file and arrays', () {
|
||||||
|
const curl = r'''curl -X POST https://api.apidash.dev/upload \\
|
||||||
|
-F "file=@/path/to/image.jpg" \\
|
||||||
|
-F "username=john" \\
|
||||||
|
-F "tags=tag1" \\
|
||||||
|
-F "tags=tag2"
|
||||||
|
''';
|
||||||
|
|
||||||
|
expect(
|
||||||
|
Curl.parse(curl),
|
||||||
|
Curl(
|
||||||
|
method: 'POST',
|
||||||
|
uri: Uri.parse('https://api.apidash.dev/upload'),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
},
|
||||||
|
form: true,
|
||||||
|
formData: [
|
||||||
|
FormDataModel(
|
||||||
|
name: "file",
|
||||||
|
value: "/path/to/image.jpg",
|
||||||
|
type: FormDataType.file),
|
||||||
|
FormDataModel(
|
||||||
|
name: "username", value: "john", type: FormDataType.text),
|
||||||
|
FormDataModel(name: "tags", value: "tag1", type: FormDataType.text),
|
||||||
|
FormDataModel(name: "tags", value: "tag2", type: FormDataType.text),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw exception when form data is not in key=value format', () {
|
||||||
|
const curl = r'''curl -X POST https://api.apidash.dev/upload \\
|
||||||
|
-F "invalid_format" \\
|
||||||
|
-F "username=john"
|
||||||
|
''';
|
||||||
|
expect(
|
||||||
|
() => Curl.parse(curl),
|
||||||
|
throwsException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not throw when form data entries are valid key-value pairs', () {
|
||||||
|
expect(
|
||||||
|
() => Curl(
|
||||||
|
uri: Uri.parse('https://api.apidash.dev/upload'),
|
||||||
|
method: 'POST',
|
||||||
|
form: true,
|
||||||
|
formData: [
|
||||||
|
FormDataModel(
|
||||||
|
name: "username", value: "john", type: FormDataType.text),
|
||||||
|
FormDataModel(
|
||||||
|
name: "password", value: "password", type: FormDataType.text),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
returnsNormally,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not throw when form data is null', () {
|
||||||
|
expect(
|
||||||
|
() => Curl(
|
||||||
|
uri: Uri.parse('https://api.apidash.dev/upload'),
|
||||||
|
method: 'POST',
|
||||||
|
form: false,
|
||||||
|
formData: null,
|
||||||
|
),
|
||||||
|
returnsNormally,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test('Check quotes support for URL string', () async {
|
test('Check quotes support for URL string', () async {
|
||||||
expect(
|
expect(
|
||||||
Curl.parse('curl -X GET "https://api.apidash.dev/"'),
|
Curl.parse('curl -X GET "https://api.apidash.dev/"'),
|
||||||
|
Reference in New Issue
Block a user