mirror of
https://github.com/foss42/apidash.git
synced 2025-05-30 13:27:09 +08:00
Merge pull request #504 from WrathOP/add-feature-parsing-form-data
Implemented FormData parsing in curl_parser and added tests for it.
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
## 0.1.1
|
||||
|
||||
- Add formdata support and new test cases.
|
||||
|
||||
## 0.1.0
|
||||
|
||||
- Added new test cases.
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:args/args.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../utils/string.dart';
|
||||
import '../utils/string.dart';
|
||||
|
||||
/// A representation of a cURL command in Dart.
|
||||
///
|
||||
@ -10,7 +11,7 @@ class Curl extends Equatable {
|
||||
/// Specifies the HTTP request method (e.g., GET, POST, PUT, DELETE).
|
||||
final String method;
|
||||
|
||||
/// Specifies the HTTP request URL
|
||||
/// Specifies the HTTP request URL.
|
||||
final Uri uri;
|
||||
|
||||
/// Adds custom HTTP headers to the request.
|
||||
@ -34,6 +35,9 @@ class Curl extends Equatable {
|
||||
/// Sends data as a multipart/form-data request.
|
||||
final bool form;
|
||||
|
||||
/// Form data list.
|
||||
final List<FormDataModel>? formData;
|
||||
|
||||
/// Allows insecure SSL connections.
|
||||
final bool insecure;
|
||||
|
||||
@ -42,7 +46,7 @@ class Curl extends Equatable {
|
||||
|
||||
/// 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({
|
||||
required this.uri,
|
||||
this.method = 'GET',
|
||||
@ -52,12 +56,18 @@ class Curl extends Equatable {
|
||||
this.user,
|
||||
this.referer,
|
||||
this.userAgent,
|
||||
this.formData,
|
||||
this.form = false,
|
||||
this.insecure = 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
|
||||
/// similar call to [parse] would throw a throwable.
|
||||
@ -70,8 +80,9 @@ class Curl extends Equatable {
|
||||
static Curl? tryParse(String curlString) {
|
||||
try {
|
||||
return Curl.parse(curlString);
|
||||
} catch (_) {}
|
||||
return null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse [curlString] as a [Curl] class instance.
|
||||
@ -82,12 +93,10 @@ class Curl extends Equatable {
|
||||
/// print(Curl.parse('1f')); // [Exception] is thrown
|
||||
/// ```
|
||||
static Curl parse(String curlString) {
|
||||
String? clean(String? url) {
|
||||
return url?.replaceAll('"', '').replaceAll("'", '');
|
||||
}
|
||||
|
||||
final parser = ArgParser(allowTrailingOptions: true);
|
||||
|
||||
// TODO: Add more options
|
||||
// https://gist.github.com/eneko/dc2d8edd9a4b25c5b0725dd123f98b10
|
||||
// Define the expected options
|
||||
parser.addOption('url');
|
||||
parser.addOption('request', abbr: 'X');
|
||||
@ -98,7 +107,7 @@ class Curl extends Equatable {
|
||||
parser.addOption('referer', abbr: 'e');
|
||||
parser.addOption('user-agent', abbr: 'A');
|
||||
parser.addFlag('head', abbr: 'I');
|
||||
parser.addFlag('form', abbr: 'F');
|
||||
parser.addMultiOption('form', abbr: 'F');
|
||||
parser.addFlag('insecure', abbr: 'k');
|
||||
parser.addFlag('location', abbr: 'L');
|
||||
|
||||
@ -111,8 +120,6 @@ class Curl extends Equatable {
|
||||
|
||||
final result = parser.parse(splittedCurlString);
|
||||
|
||||
final method = (result['request'] as String?)?.toUpperCase();
|
||||
|
||||
// Extract the request headers
|
||||
Map<String, String>? headers;
|
||||
if (result['header'] != null) {
|
||||
@ -129,26 +136,59 @@ 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 entry $formEntry is not in key=value format');
|
||||
}
|
||||
|
||||
// Handling the file or text type
|
||||
var 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[kHeaderContentType] = ContentType.formdata.header;
|
||||
}
|
||||
|
||||
// Handle URL and query parameters
|
||||
final url = clean(result['url']) ?? clean(result.rest.firstOrNull);
|
||||
if (url == null) {
|
||||
throw Exception('URL is null');
|
||||
}
|
||||
final uri = Uri.parse(url);
|
||||
|
||||
final method = result['head']
|
||||
? 'HEAD'
|
||||
: ((result['request'] as String?)?.toUpperCase() ?? 'GET');
|
||||
final String? data = result['data'];
|
||||
final String? cookie = result['cookie'];
|
||||
final String? user = result['user'];
|
||||
final String? referer = result['referer'];
|
||||
final String? userAgent = result['user-agent'];
|
||||
final bool form = result['form'] ?? false;
|
||||
final bool head = result['head'] ?? false;
|
||||
final bool form = formData != null && formData.isNotEmpty;
|
||||
final bool insecure = result['insecure'] ?? false;
|
||||
final bool location = result['location'] ?? false;
|
||||
|
||||
// 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(
|
||||
method: head ? "HEAD" : (method ?? 'GET'),
|
||||
method: method,
|
||||
uri: uri,
|
||||
headers: headers,
|
||||
data: data,
|
||||
@ -157,12 +197,13 @@ class Curl extends Equatable {
|
||||
referer: referer,
|
||||
userAgent: userAgent,
|
||||
form: form,
|
||||
formData: formData,
|
||||
insecure: insecure,
|
||||
location: location,
|
||||
);
|
||||
}
|
||||
|
||||
// Formatted cURL command
|
||||
/// Converts the Curl object to a formatted cURL command string.
|
||||
String toCurlString() {
|
||||
var cmd = 'curl ';
|
||||
|
||||
@ -176,7 +217,6 @@ class Curl extends Equatable {
|
||||
|
||||
// Add the URL
|
||||
cmd += '"${Uri.encodeFull(uri.toString())}" ';
|
||||
|
||||
// Add the headers
|
||||
headers?.forEach((key, value) {
|
||||
cmd += '\\\n -H "$key: $value" ';
|
||||
@ -204,15 +244,22 @@ class Curl extends Equatable {
|
||||
}
|
||||
// Add the form flag
|
||||
if (form) {
|
||||
cmd += " \\\n -F ";
|
||||
for (final formEntry in formData!) {
|
||||
cmd += "\\\n -F ";
|
||||
if (formEntry.type == FormDataType.file) {
|
||||
cmd += '"${formEntry.name}=@${formEntry.value}" ';
|
||||
} else {
|
||||
cmd += '"${formEntry.name}=${formEntry.value}" ';
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the insecure flag
|
||||
if (insecure) {
|
||||
cmd += " \\\n -k ";
|
||||
cmd += "-k ";
|
||||
}
|
||||
// Add the location flag
|
||||
if (location) {
|
||||
cmd += " \\\n -L ";
|
||||
cmd += "-L ";
|
||||
}
|
||||
|
||||
return cmd.trim();
|
||||
@ -229,6 +276,7 @@ class Curl extends Equatable {
|
||||
referer,
|
||||
userAgent,
|
||||
form,
|
||||
formData,
|
||||
insecure,
|
||||
location,
|
||||
];
|
||||
|
@ -3,3 +3,7 @@ import 'package:shlex/shlex.dart' as shlex;
|
||||
List<String> splitAsCommandLineArgs(String command) {
|
||||
return shlex.split(command);
|
||||
}
|
||||
|
||||
String? clean(String? url) {
|
||||
return url?.replaceAll('"', '').replaceAll("'", '');
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: curl_parser
|
||||
description: Parse cURL command to Dart object and convert Dart object to cURL command.
|
||||
version: 0.1.0
|
||||
version: 0.1.1
|
||||
homepage: 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
|
||||
@ -20,6 +20,8 @@ dependencies:
|
||||
args: ^2.5.0
|
||||
equatable: ^2.0.5
|
||||
shlex: ^2.0.2
|
||||
apidash_core:
|
||||
path: ../apidash_core
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^2.1.0
|
||||
|
34
packages/curl_parser/test/curl_model_test.dart
Normal file
34
packages/curl_parser/test/curl_model_test.dart
Normal file
@ -0,0 +1,34 @@
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:curl_parser/curl_parser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('should not throw when form data entries are provided', () {
|
||||
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,
|
||||
);
|
||||
});
|
||||
}
|
@ -1,233 +1,328 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:curl_parser/models/curl.dart';
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:curl_parser/curl_parser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
const defaultTimeout = Timeout(Duration(seconds: 3));
|
||||
final exampleDotComUri = Uri.parse('https://api.apidash.dev/');
|
||||
final apiUri = Uri.parse('https://api.apidash.dev');
|
||||
|
||||
void main() {
|
||||
test('parse an easy cURL', () async {
|
||||
test(
|
||||
'parse an easy cURL',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse('curl -X GET https://api.apidash.dev'),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: apiUri,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('parse POST request with multipart/form-data', () {
|
||||
const curl = r'''curl -X POST 'https://api.apidash.dev/io/img' \
|
||||
-H 'Content-Type: multipart/form-data' \
|
||||
-F "imfile=@/path/to/image.jpg" \
|
||||
-F "token=john"
|
||||
''';
|
||||
|
||||
expect(
|
||||
Curl.parse('curl -X GET https://api.apidash.dev/'),
|
||||
Curl.parse(curl),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: exampleDotComUri,
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/io/img'),
|
||||
headers: {"Content-Type": "multipart/form-data"},
|
||||
form: true,
|
||||
formData: [
|
||||
FormDataModel(
|
||||
name: "imfile",
|
||||
value: "/path/to/image.jpg",
|
||||
type: FormDataType.file,
|
||||
),
|
||||
FormDataModel(
|
||||
name: "token",
|
||||
value: "john",
|
||||
type: FormDataType.text,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
});
|
||||
|
||||
test('parse POST request with x-www-form-urlencoded', () {
|
||||
const curl = r'''curl -X 'POST' \
|
||||
'https://api.apidash.dev/io/form' \
|
||||
-H 'Content-Type: application/x-www-form-urlencoded' \
|
||||
-d 'text=apidash&sep=%7C×=3'
|
||||
''';
|
||||
|
||||
test('compose an easy cURL', () async {
|
||||
expect(
|
||||
Curl.parse('curl -X GET https://api.apidash.dev/'),
|
||||
Curl.parse(curl),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: exampleDotComUri,
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/io/form'),
|
||||
headers: {"Content-Type": "application/x-www-form-urlencoded"},
|
||||
data: 'text=apidash&sep=%7C×=3',
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
});
|
||||
|
||||
test('Check quotes support for URL string', () async {
|
||||
expect(
|
||||
Curl.parse('curl -X GET "https://api.apidash.dev/"'),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: exampleDotComUri,
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test('should throw exception when multipart/form-data is not valid', () {
|
||||
const curl = r'''curl -X POST https://api.apidash.dev/upload \
|
||||
-F "invalid_format" \
|
||||
-F "username=john"
|
||||
''';
|
||||
expect(() => Curl.parse(curl), throwsException);
|
||||
});
|
||||
|
||||
test('parses two headers', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
'curl -X GET https://api.apidash.dev/ -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: exampleDotComUri,
|
||||
headers: {
|
||||
HttpHeaders.contentTypeHeader: ContentType.text.toString(),
|
||||
HttpHeaders.authorizationHeader: 'Bearer %token%',
|
||||
},
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'Check quotes support for URL string',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse('curl -X GET "https://api.apidash.dev"'),
|
||||
Curl(method: 'GET', uri: apiUri),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('parses a lot of headers', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r'curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" -H "Upgrade-Insecure-Requests: 1" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7" -H "Cache-Control: max-age=0" -H "Dnt: 1" -H "Sec-Ch-Ua: \"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"" -H "Sec-Ch-Ua-Mobile: ?0" -H "Sec-Ch-Ua-Platform: \"macOS\"" -H "Sec-Fetch-Dest: document" -H "Sec-Fetch-Mode: navigate" -H "Sec-Fetch-Site: same-origin" -H "Sec-Fetch-User: ?1" https://apidash.dev',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://apidash.dev'),
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
'Accept':
|
||||
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Accept-Language': 'en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7',
|
||||
'Cache-Control': 'max-age=0',
|
||||
'Dnt': '1',
|
||||
'Sec-Ch-Ua':
|
||||
'"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
|
||||
'Sec-Ch-Ua-Mobile': '?0',
|
||||
'Sec-Ch-Ua-Platform': '"macOS"',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Sec-Fetch-User': '?1'
|
||||
},
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'parses two headers',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
'curl -X GET https://api.apidash.dev -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: apiUri,
|
||||
headers: {
|
||||
HttpHeaders.contentTypeHeader: ContentType.text.toString(),
|
||||
HttpHeaders.authorizationHeader: 'Bearer %token%',
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('throw exception when parses wrong input', () async {
|
||||
expect(
|
||||
() => Curl.parse('1f'),
|
||||
throwsException,
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'parses a lot of headers',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r'''curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" \
|
||||
-H "Upgrade-Insecure-Requests: 1" \
|
||||
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7" \
|
||||
-H "Accept-Encoding: gzip, deflate, br" \
|
||||
-H "Accept-Language: en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7" \
|
||||
-H "Cache-Control: max-age=0" \
|
||||
-H "Dnt: 1" \
|
||||
-H "Sec-Ch-Ua: \"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"" \
|
||||
-H "Sec-Ch-Ua-Mobile: ?0" \
|
||||
-H "Sec-Ch-Ua-Platform: \"macOS\"" \
|
||||
-H "Sec-Fetch-Dest: document" \
|
||||
-H "Sec-Fetch-Mode: navigate" \
|
||||
-H "Sec-Fetch-Site: same-origin" \
|
||||
-H "Sec-Fetch-User: ?1" https://apidash.dev''',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://apidash.dev'),
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
'Accept':
|
||||
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Accept-Language': 'en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7',
|
||||
'Cache-Control': 'max-age=0',
|
||||
'Dnt': '1',
|
||||
'Sec-Ch-Ua':
|
||||
'"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
|
||||
'Sec-Ch-Ua-Mobile': '?0',
|
||||
'Sec-Ch-Ua-Platform': '"macOS"',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Sec-Fetch-User': '?1'
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('tryParse return null when parses wrong input', () async {
|
||||
expect(
|
||||
Curl.tryParse('1f'),
|
||||
null,
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'throw exception when parses wrong input',
|
||||
() async {
|
||||
expect(() => Curl.parse('1f'), throwsException);
|
||||
},
|
||||
);
|
||||
|
||||
test('tryParse success', () async {
|
||||
expect(
|
||||
Curl.tryParse(
|
||||
'curl -X GET https://api.apidash.dev/ -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: exampleDotComUri,
|
||||
headers: {
|
||||
HttpHeaders.contentTypeHeader: ContentType.text.toString(),
|
||||
HttpHeaders.authorizationHeader: 'Bearer %token%',
|
||||
},
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'tryParse return null when parses wrong input',
|
||||
() async {
|
||||
expect(Curl.tryParse('1f'), null);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 1', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'tryParse success',
|
||||
() async {
|
||||
expect(
|
||||
Curl.tryParse(
|
||||
'curl -X GET https://api.apidash.dev -H "test: Agent"',
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: apiUri,
|
||||
headers: {
|
||||
'test': 'Agent',
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 2', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev/country/data?code=US'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev/country/data?code=US'),
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'HEAD 1',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl -I 'https://api.apidash.dev'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'HEAD',
|
||||
uri: apiUri,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 3', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev/country/data?code=IND'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev/country/data?code=IND'),
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'HEAD 2',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --head 'https://api.apidash.dev'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'HEAD',
|
||||
uri: apiUri,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 4', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse(
|
||||
'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'),
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
test(
|
||||
'GET 1',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: apiUri,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 5', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.github.com/repos/foss42/apidash' \
|
||||
test(
|
||||
'GET 2',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.apidash.dev/country/data?code=US'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev/country/data?code=US'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'GET 3',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl 'https://api.apidash.dev/country/data?code=IND'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev/country/data?code=IND'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'GET with GitHub API',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.github.com/repos/foss42/apidash' \
|
||||
--header 'User-Agent: Test Agent'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.github.com/repos/foss42/apidash'),
|
||||
headers: {"User-Agent": "Test Agent"},
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.github.com/repos/foss42/apidash'),
|
||||
headers: {"User-Agent": "Test Agent"},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('GET 6', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.github.com/repos/foss42/apidash?raw=true' \
|
||||
test(
|
||||
'GET with GitHub API and raw parameter',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --url 'https://api.github.com/repos/foss42/apidash?raw=true' \
|
||||
--header 'User-Agent: Test Agent'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.github.com/repos/foss42/apidash?raw=true'),
|
||||
headers: {"User-Agent": "Test Agent"},
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
),
|
||||
Curl(
|
||||
method: 'GET',
|
||||
uri:
|
||||
Uri.parse('https://api.github.com/repos/foss42/apidash?raw=true'),
|
||||
headers: {"User-Agent": "Test Agent"},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('HEAD 2', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --head --url 'http://api.apidash.dev'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'HEAD',
|
||||
uri: Uri.parse('http://api.apidash.dev'),
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
|
||||
test('POST 1', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --request POST \
|
||||
test(
|
||||
'POST with text/plain content',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --request POST \
|
||||
--url 'https://api.apidash.dev/case/lower' \
|
||||
--header 'Content-Type: text/plain' \
|
||||
--data '{
|
||||
"text": "I LOVE Flutter"
|
||||
}'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/case/lower'),
|
||||
headers: {"Content-Type": "text/plain"},
|
||||
data: r"""{
|
||||
),
|
||||
Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/case/lower'),
|
||||
headers: {"Content-Type": "text/plain"},
|
||||
data: r"""{
|
||||
"text": "I LOVE Flutter"
|
||||
}""",
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('POST 2', () async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --request POST \
|
||||
test(
|
||||
'POST with application/json content',
|
||||
() async {
|
||||
expect(
|
||||
Curl.parse(
|
||||
r"""curl --request POST \
|
||||
--url 'https://api.apidash.dev/case/lower' \
|
||||
--header 'Content-Type: application/json' \
|
||||
--data '{
|
||||
@ -238,12 +333,12 @@ void main() {
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}'""",
|
||||
),
|
||||
Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/case/lower'),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
data: r"""{
|
||||
),
|
||||
Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/case/lower'),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
data: r"""{
|
||||
"text": "I LOVE Flutter",
|
||||
"flag": null,
|
||||
"male": true,
|
||||
@ -251,7 +346,8 @@ void main() {
|
||||
"no": 1.2,
|
||||
"arr": ["null", "true", "false", null]
|
||||
}""",
|
||||
),
|
||||
);
|
||||
}, timeout: defaultTimeout);
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
252
packages/curl_parser/test/dart_to_curl_test.dart
Normal file
252
packages/curl_parser/test/dart_to_curl_test.dart
Normal file
@ -0,0 +1,252 @@
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:curl_parser/curl_parser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group(
|
||||
'Basic HTTP Methods',
|
||||
() {
|
||||
test(
|
||||
'GET request',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
'curl "https://api.apidash.dev"',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'POST request',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/test'),
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
'curl -X POST "https://api.apidash.dev/test"',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('HEAD request', () {
|
||||
final curl = Curl(
|
||||
method: 'HEAD',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
'curl -I "https://api.apidash.dev"',
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group(
|
||||
'Headers and Data',
|
||||
() {
|
||||
test(
|
||||
'request with headers',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer token123'
|
||||
},
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r'''curl "https://api.apidash.dev" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer token123"''',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('POST request with data', () {
|
||||
final curl = Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/case/lower'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
data: '{"text": "Grass Is Green"}',
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl -X POST "https://api.apidash.dev/case/lower" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"text": "Grass Is Green"}'""",
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group(
|
||||
'Post with Form Data',
|
||||
() {
|
||||
test('request with form data', () {
|
||||
final curl = Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/io/img'),
|
||||
headers: {'Content-Type': 'multipart/form-data'},
|
||||
form: true,
|
||||
formData: [
|
||||
FormDataModel(
|
||||
name: "imfile",
|
||||
value: "/path/to/file.png",
|
||||
type: FormDataType.file,
|
||||
),
|
||||
FormDataModel(
|
||||
name: "token",
|
||||
value: "123",
|
||||
type: FormDataType.text,
|
||||
),
|
||||
],
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r'''curl -X POST "https://api.apidash.dev/io/img" \
|
||||
-H "Content-Type: multipart/form-data" \
|
||||
-F "imfile=@/path/to/file.png" \
|
||||
-F "token=123"''',
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group(
|
||||
'Special Parameters',
|
||||
() {
|
||||
test(
|
||||
'request with cookie',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
cookie: 'session=abc123',
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" \
|
||||
-b 'session=abc123'""",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request with user credentials',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
user: 'username:password',
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" \
|
||||
-u 'username:password'""",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request with referer',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
referer: 'https://example.com',
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" \
|
||||
-e 'https://example.com'""",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request with user agent',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
userAgent: 'MyApp/1.0',
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" \
|
||||
-A 'MyApp/1.0'""",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request with insecure flag',
|
||||
() {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
insecure: true,
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" -k""",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('request with location flag', () {
|
||||
final curl = Curl(
|
||||
method: 'GET',
|
||||
uri: Uri.parse('https://api.apidash.dev'),
|
||||
location: true,
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl "https://api.apidash.dev" -L""",
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group(
|
||||
'Complex Requests',
|
||||
() {
|
||||
test('request with all parameters', () {
|
||||
final curl = Curl(
|
||||
method: 'POST',
|
||||
uri: Uri.parse('https://api.apidash.dev/test'),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer token123'
|
||||
},
|
||||
data: '{"key": "value"}',
|
||||
cookie: 'session=abc123',
|
||||
user: 'username:password',
|
||||
referer: 'https://example.com',
|
||||
userAgent: 'MyApp/1.0',
|
||||
insecure: true,
|
||||
location: true,
|
||||
);
|
||||
expect(
|
||||
curl.toCurlString(),
|
||||
r"""curl -X POST "https://api.apidash.dev/test" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer token123" \
|
||||
-d '{"key": "value"}' \
|
||||
-b 'session=abc123' \
|
||||
-u 'username:password' \
|
||||
-e 'https://example.com' \
|
||||
-A 'MyApp/1.0' -k -L""",
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user