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.
290 lines
7.2 KiB
Dart
290 lines
7.2 KiB
Dart
import 'package:curl_parser/curl_parser.dart';
|
|
import 'package:seed/seed.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"''',
|
|
);
|
|
});
|
|
|
|
test('form defaults header when absent', () {
|
|
final curl = Curl(
|
|
method: 'POST',
|
|
uri: Uri.parse('https://api.apidash.dev/io/img'),
|
|
form: true,
|
|
formData: [
|
|
FormDataModel(
|
|
name: 'file',
|
|
value: '/tmp/a.png',
|
|
type: FormDataType.file,
|
|
),
|
|
],
|
|
);
|
|
// parse back to ensure header gets defaulted in parser
|
|
final parsed = Curl.parse(curl.toCurlString());
|
|
expect(parsed.form, isTrue);
|
|
expect(
|
|
parsed.headers?[kHeaderContentType] ??
|
|
parsed.headers?['content-type'],
|
|
'multipart/form-data');
|
|
});
|
|
},
|
|
);
|
|
|
|
group('Roundtrip with body', () {
|
|
test('toCurlString includes body when present', () {
|
|
final curl = Curl(
|
|
method: 'POST',
|
|
uri: Uri.parse('https://api.apidash.dev/submit'),
|
|
data: 'a=1&b=2',
|
|
);
|
|
final s = curl.toCurlString();
|
|
final back = Curl.parse(s);
|
|
expect(back.method, 'POST');
|
|
expect(back.data, 'a=1&b=2');
|
|
expect(back.uri, Uri.parse('https://api.apidash.dev/submit'));
|
|
});
|
|
});
|
|
|
|
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""",
|
|
);
|
|
});
|
|
},
|
|
);
|
|
}
|