mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
- Pre-filter unknown flags before ArgParser; keep positional args. - Tolerate non-request flags: -v/--verbose, --connect-timeout, --retry, --output, --compressed, -i/--include, --globoff. - Auth: support --oauth2-bearer; map to - Authorization only if absent. - Cookies: parse -b/--cookie; accept -c/--cookie-jar (ignored for request). - URL: prefer first http(s) positional when --url missing; quote cleaning. - Data: merge data-urlencode → data-raw → data-binary → data; default POST when body/form present; HEAD remains HEAD. - Forms: parse -F entries; auto-set multipart Content-Type if missing. - Headers: robust -H parsing for multi-colon values. - toCurlString: deterministic order; fix continuation spacing; emit -d right after headers/form; place -k/-L at end. - Utils: normalize backslash-newlines/CRLF; remove stray '+'; shlex split. - Tests: add unknown flags, oauth2-bearer (and non-override), cookie-jar, verbose/timeout/retry/output tolerance, data merging order, HEAD+data, -A user-agent, -b filename. - Docs: add Dartdoc for utils; class docs present.
105 lines
2.6 KiB
Dart
105 lines
2.6 KiB
Dart
import 'package:curl_parser/utils/string.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
const defaultTimeout = Timeout(Duration(seconds: 3));
|
|
|
|
void main() {
|
|
test('parse cURL with single quotes', () async {
|
|
expect(
|
|
splitAsCommandLineArgs(
|
|
r"""--url 'https://api.github.com/repos/foss42/apidash?raw=true' \
|
|
--header 'User-Agent: Test Agent'"""),
|
|
[
|
|
"--url",
|
|
"https://api.github.com/repos/foss42/apidash?raw=true",
|
|
"--header",
|
|
"User-Agent: Test Agent",
|
|
],
|
|
);
|
|
}, timeout: defaultTimeout);
|
|
|
|
test('parse cURL with double quotes', () async {
|
|
expect(
|
|
splitAsCommandLineArgs(
|
|
r'''--url "https://api.github.com/repos/foss42/apidash?raw=true" \
|
|
--header "User-Agent: Test Agent"'''),
|
|
[
|
|
"--url",
|
|
"https://api.github.com/repos/foss42/apidash?raw=true",
|
|
"--header",
|
|
"User-Agent: Test Agent",
|
|
],
|
|
);
|
|
}, timeout: defaultTimeout);
|
|
|
|
test('parse cURL DevTools', () async {
|
|
expect(
|
|
splitAsCommandLineArgs(
|
|
r"""--request GET 'https://dummyimage.com/150/92c952' \
|
|
--header 'user-agent: Dart/3.8 (dart:io)' \
|
|
--header 'accept-encoding: gzip' \
|
|
--header 'content-length: 0' \
|
|
--header 'host: dummyimage.com'"""),
|
|
[
|
|
'--request',
|
|
'GET',
|
|
'https://dummyimage.com/150/92c952',
|
|
'--header',
|
|
'user-agent: Dart/3.8 (dart:io)',
|
|
'--header',
|
|
'accept-encoding: gzip',
|
|
'--header',
|
|
'content-length: 0',
|
|
'--header',
|
|
'host: dummyimage.com'
|
|
],
|
|
);
|
|
}, timeout: defaultTimeout);
|
|
|
|
test('parse cURL with body', () async {
|
|
expect(
|
|
splitAsCommandLineArgs(r"""--request POST \
|
|
--url 'https://api.apidash.dev/case/lower' \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"text": "I LOVE Flutter",
|
|
"flag": null,
|
|
"male": true,
|
|
"female": false,
|
|
"no": 1.2,
|
|
"arr": ["null", "true", "false", null]
|
|
}'"""),
|
|
[
|
|
"--request",
|
|
"POST",
|
|
"--url",
|
|
"https://api.apidash.dev/case/lower",
|
|
"--header",
|
|
"Content-Type: application/json",
|
|
"--data",
|
|
r'''{
|
|
"text": "I LOVE Flutter",
|
|
"flag": null,
|
|
"male": true,
|
|
"female": false,
|
|
"no": 1.2,
|
|
"arr": ["null", "true", "false", null]
|
|
}''',
|
|
],
|
|
);
|
|
}, timeout: defaultTimeout);
|
|
|
|
test('split handles CRLF and backslash-newline', () async {
|
|
final args = splitAsCommandLineArgs(
|
|
"--request GET \\\r\n --url 'https://api.apidash.dev/echo' \\\n+ \n --header 'A: 1' ");
|
|
expect(args, [
|
|
'--request',
|
|
'GET',
|
|
'--url',
|
|
'https://api.apidash.dev/echo',
|
|
'--header',
|
|
'A: 1'
|
|
]);
|
|
}, timeout: defaultTimeout);
|
|
}
|