mirror of
https://github.com/foss42/apidash.git
synced 2025-05-21 08:16:29 +08:00
Update dart_to_curl_test.dart
This commit is contained in:
@ -3,203 +3,241 @@ import 'package:curl_parser/curl_parser.dart';
|
|||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Basic HTTP Methods', () {
|
group(
|
||||||
test('GET request', () {
|
'Basic HTTP Methods',
|
||||||
final curl = Curl(
|
() {
|
||||||
method: 'GET',
|
test(
|
||||||
uri: Uri.parse('https://api.apidash.dev'),
|
'GET request',
|
||||||
);
|
() {
|
||||||
expect(
|
final curl = Curl(
|
||||||
curl.toCurlString(),
|
method: 'GET',
|
||||||
'curl "https://api.apidash.dev"',
|
uri: Uri.parse('https://api.apidash.dev'),
|
||||||
);
|
);
|
||||||
});
|
expect(
|
||||||
|
curl.toCurlString(),
|
||||||
test('POST request', () {
|
'curl "https://api.apidash.dev"',
|
||||||
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(),
|
test(
|
||||||
r'''curl "https://api.apidash.dev" \
|
'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 "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer token123"''',
|
-H "Authorization: Bearer token123"''',
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
test('POST request with data', () {
|
test('POST request with data', () {
|
||||||
final curl = Curl(
|
final curl = Curl(
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
uri: Uri.parse('https://api.apidash.dev/test'),
|
uri: Uri.parse('https://api.apidash.dev/test'),
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
data: '{"key": "value"}',
|
data: '{"key": "value"}',
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
curl.toCurlString(),
|
curl.toCurlString(),
|
||||||
r"""curl -X POST "https://api.apidash.dev/test" \
|
r"""curl -X POST "https://api.apidash.dev/test" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{"key": "value"}'""",
|
-d '{"key": "value"}'""",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
group('Form Data', () {
|
group(
|
||||||
test('request with form data', () {
|
'Post with Form Data',
|
||||||
final curl = Curl(
|
() {
|
||||||
method: 'POST',
|
test('request with form data', () {
|
||||||
uri: Uri.parse('https://api.apidash.dev/upload'),
|
final curl = Curl(
|
||||||
headers: {'Content-Type': 'multipart/form-data'},
|
method: 'POST',
|
||||||
form: true,
|
uri: Uri.parse('https://api.apidash.dev/upload'),
|
||||||
formData: [
|
headers: {'Content-Type': 'multipart/form-data'},
|
||||||
FormDataModel(
|
form: true,
|
||||||
name: "file",
|
formData: [
|
||||||
value: "/path/to/file.txt",
|
FormDataModel(
|
||||||
type: FormDataType.file,
|
name: "file",
|
||||||
),
|
value: "/path/to/file.txt",
|
||||||
FormDataModel(
|
type: FormDataType.file,
|
||||||
name: "name",
|
),
|
||||||
value: "test",
|
FormDataModel(
|
||||||
type: FormDataType.text,
|
name: "name",
|
||||||
),
|
value: "test",
|
||||||
],
|
type: FormDataType.text,
|
||||||
);
|
),
|
||||||
expect(
|
],
|
||||||
curl.toCurlString(),
|
);
|
||||||
r'''curl -X POST "https://api.apidash.dev/upload" \
|
expect(
|
||||||
|
curl.toCurlString(),
|
||||||
|
r'''curl -X POST "https://api.apidash.dev/upload" \
|
||||||
-H "Content-Type: multipart/form-data" \
|
-H "Content-Type: multipart/form-data" \
|
||||||
-F "file=@/path/to/file.txt" \
|
-F "file=@/path/to/file.txt" \
|
||||||
-F "name=test"''',
|
-F "name=test"''',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
group('Special Parameters', () {
|
group(
|
||||||
test('request with cookie', () {
|
'Special Parameters',
|
||||||
final curl = Curl(
|
() {
|
||||||
method: 'GET',
|
test(
|
||||||
uri: Uri.parse('https://api.apidash.dev'),
|
'request with cookie',
|
||||||
cookie: 'session=abc123',
|
() {
|
||||||
);
|
final curl = Curl(
|
||||||
expect(
|
method: 'GET',
|
||||||
curl.toCurlString(),
|
uri: Uri.parse('https://api.apidash.dev'),
|
||||||
r"""curl "https://api.apidash.dev" \
|
cookie: 'session=abc123',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
curl.toCurlString(),
|
||||||
|
r"""curl "https://api.apidash.dev" \
|
||||||
-b 'session=abc123'""",
|
-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(),
|
test(
|
||||||
r"""curl -X POST "https://api.apidash.dev/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 "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer token123" \
|
-H "Authorization: Bearer token123" \
|
||||||
-d '{"key": "value"}' \
|
-d '{"key": "value"}' \
|
||||||
@ -207,7 +245,8 @@ void main() {
|
|||||||
-u 'username:password' \
|
-u 'username:password' \
|
||||||
-e 'https://example.com' \
|
-e 'https://example.com' \
|
||||||
-A 'MyApp/1.0' -k -L""",
|
-A 'MyApp/1.0' -k -L""",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user