mirror of
https://github.com/foss42/apidash.git
synced 2025-06-18 21:04:37 +08:00
Implemented FormData parsing in curl_parser and added tests for it.
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
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';
|
||||
|
||||
const defaultTimeout = Timeout(Duration(seconds: 3));
|
||||
@ -27,6 +29,104 @@ void main() {
|
||||
);
|
||||
}, 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 {
|
||||
expect(
|
||||
Curl.parse('curl -X GET "https://api.apidash.dev/"'),
|
||||
|
Reference in New Issue
Block a user