Update curl_parser_test.dart

This commit is contained in:
Ashita Prasad
2024-11-30 14:24:02 +05:30
parent 835b1df03d
commit bbcca7ee40

View File

@ -3,72 +3,71 @@ import 'package:apidash_core/apidash_core.dart';
import 'package:curl_parser/curl_parser.dart'; import 'package:curl_parser/curl_parser.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
const defaultTimeout = Timeout(Duration(seconds: 3)); final apiUri = Uri.parse('https://api.apidash.dev');
final exampleDotComUri = Uri.parse('https://api.apidash.dev/');
void main() { void main() {
test('parse an easy cURL', () async { test(
expect( 'parse an easy cURL',
Curl.parse('curl -X GET https://api.apidash.dev/'), () async {
Curl(method: 'GET', uri: exampleDotComUri), expect(
); Curl.parse('curl -X GET https://api.apidash.dev'),
}, timeout: defaultTimeout); Curl(
method: 'GET',
uri: apiUri,
),
);
},
);
test('parse POST request with form-data', () { test('parse POST request with multipart/form-data', () {
const curl = r'''curl -X POST https://api.apidash.dev/upload \ const curl = r'''curl -X POST 'https://api.apidash.dev/io/img' \
-F "file=@/path/to/image.jpg" \ -H 'Content-Type: multipart/form-data' \
-F "username=john" -F "imfile=@/path/to/image.jpg" \
-F "token=john"
'''; ''';
expect( expect(
Curl.parse(curl), Curl.parse(curl),
Curl( Curl(
method: 'POST', method: 'POST',
uri: Uri.parse('https://api.apidash.dev/upload'), uri: Uri.parse('https://api.apidash.dev/io/img'),
headers: {"Content-Type": "multipart/form-data"}, headers: {"Content-Type": "multipart/form-data"},
form: true, form: true,
formData: [ formData: [
FormDataModel( FormDataModel(
name: "file", name: "imfile",
value: "/path/to/image.jpg", value: "/path/to/image.jpg",
type: FormDataType.file), type: FormDataType.file,
),
FormDataModel( FormDataModel(
name: "username", value: "john", type: FormDataType.text) name: "token",
value: "john",
type: FormDataType.text,
)
], ],
), ),
); );
}); });
test('parse POST request with form-data including a file and arrays', () { test('parse POST request with x-www-form-urlencoded', () {
const curl = r'''curl -X POST https://api.apidash.dev/upload \ const curl = r'''curl -X 'POST' \
-F "file=@/path/to/image.jpg" \ 'https://api.apidash.dev/io/form' \
-F "username=john" \ -H 'Content-Type: application/x-www-form-urlencoded' \
-F "tags=tag1" \ -d 'text=apidash&sep=%7C&times=3'
-F "tags=tag2"
'''; ''';
expect( expect(
Curl.parse(curl), Curl.parse(curl),
Curl( Curl(
method: 'POST', method: 'POST',
uri: Uri.parse('https://api.apidash.dev/upload'), uri: Uri.parse('https://api.apidash.dev/io/form'),
headers: {"Content-Type": "multipart/form-data"}, headers: {"Content-Type": "application/x-www-form-urlencoded"},
form: true, data: 'text=apidash&sep=%7C&times=3',
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', () { test('should throw exception when multipart/form-data is not valid', () {
const curl = r'''curl -X POST https://api.apidash.dev/upload \ const curl = r'''curl -X POST https://api.apidash.dev/upload \
-F "invalid_format" \ -F "invalid_format" \
-F "username=john" -F "username=john"
@ -76,186 +75,254 @@ void main() {
expect(() => Curl.parse(curl), throwsException); expect(() => Curl.parse(curl), throwsException);
}); });
test('Check quotes support for URL string', () async { test(
expect( 'Check quotes support for URL string',
Curl.parse('curl -X GET "https://api.apidash.dev/"'), () async {
Curl(method: 'GET', uri: exampleDotComUri), expect(
); Curl.parse('curl -X GET "https://api.apidash.dev"'),
}, timeout: defaultTimeout); Curl(method: 'GET', uri: apiUri),
);
},
);
test('parses two headers', () async { test(
expect( 'parses two headers',
Curl.parse( () async {
'curl -X GET https://api.apidash.dev/ -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"', expect(
), Curl.parse(
Curl( 'curl -X GET https://api.apidash.dev -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"',
method: 'GET', ),
uri: exampleDotComUri, Curl(
headers: { method: 'GET',
HttpHeaders.contentTypeHeader: ContentType.text.toString(), uri: apiUri,
HttpHeaders.authorizationHeader: 'Bearer %token%', headers: {
}, HttpHeaders.contentTypeHeader: ContentType.text.toString(),
), HttpHeaders.authorizationHeader: 'Bearer %token%',
); },
}, timeout: defaultTimeout); ),
);
},
);
test('parses a lot of headers', () async { test(
expect( 'parses a lot of headers',
Curl.parse( () async {
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', expect(
), Curl.parse(
Curl( 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" \
method: 'GET', -H "Upgrade-Insecure-Requests: 1" \
uri: Uri.parse('https://apidash.dev'), -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" \
headers: { -H "Accept-Encoding: gzip, deflate, br" \
'User-Agent': -H "Accept-Language: en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7" \
'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 "Cache-Control: max-age=0" \
'Upgrade-Insecure-Requests': '1', -H "Dnt: 1" \
'Accept': -H "Sec-Ch-Ua: \"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"" \
'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 "Sec-Ch-Ua-Mobile: ?0" \
'Accept-Encoding': 'gzip, deflate, br', -H "Sec-Ch-Ua-Platform: \"macOS\"" \
'Accept-Language': 'en,en-GB;q=0.9,en-US;q=0.8,ru;q=0.7', -H "Sec-Fetch-Dest: document" \
'Cache-Control': 'max-age=0', -H "Sec-Fetch-Mode: navigate" \
'Dnt': '1', -H "Sec-Fetch-Site: same-origin" \
'Sec-Ch-Ua': -H "Sec-Fetch-User: ?1" https://apidash.dev''',
'"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"', ),
'Sec-Ch-Ua-Mobile': '?0', Curl(
'Sec-Ch-Ua-Platform': '"macOS"', method: 'GET',
'Sec-Fetch-Dest': 'document', uri: Uri.parse('https://apidash.dev'),
'Sec-Fetch-Mode': 'navigate', headers: {
'Sec-Fetch-Site': 'same-origin', 'User-Agent':
'Sec-Fetch-User': '?1' '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',
}, timeout: defaultTimeout); '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('throw exception when parses wrong input', () async { test(
expect(() => Curl.parse('1f'), throwsException); 'throw exception when parses wrong input',
}, timeout: defaultTimeout); () async {
expect(() => Curl.parse('1f'), throwsException);
},
);
test('tryParse return null when parses wrong input', () async { test(
expect(Curl.tryParse('1f'), null); 'tryParse return null when parses wrong input',
}, timeout: defaultTimeout); () async {
expect(Curl.tryParse('1f'), null);
},
);
test('tryParse success', () async { test(
expect( 'tryParse success',
Curl.tryParse( () async {
'curl -X GET https://api.apidash.dev/ -H "${HttpHeaders.contentTypeHeader}: ${ContentType.text}" -H "${HttpHeaders.authorizationHeader}: Bearer %token%"', expect(
), Curl.tryParse(
Curl( 'curl -X GET https://api.apidash.dev -H "test: Agent"',
method: 'GET', ),
uri: exampleDotComUri, Curl(
headers: { method: 'GET',
HttpHeaders.contentTypeHeader: ContentType.text.toString(), uri: apiUri,
HttpHeaders.authorizationHeader: 'Bearer %token%', headers: {
}, 'test': 'Agent',
), },
); ),
}, timeout: defaultTimeout); );
},
);
test('GET 1', () async { test(
expect( 'HEAD 1',
Curl.parse( () async {
r"""curl --url 'https://api.apidash.dev'""", expect(
), Curl.parse(
Curl( r"""curl -I 'https://api.apidash.dev'""",
method: 'GET', ),
uri: Uri.parse('https://api.apidash.dev'), Curl(
), method: 'HEAD',
); uri: apiUri,
}, timeout: defaultTimeout); ),
);
},
);
test('GET 2', () async { test(
expect( 'HEAD 2',
Curl.parse( () async {
r"""curl --url 'https://api.apidash.dev/country/data?code=US'""", expect(
), Curl.parse(
Curl( r"""curl --head 'https://api.apidash.dev'""",
method: 'GET', ),
uri: Uri.parse('https://api.apidash.dev/country/data?code=US'), Curl(
), method: 'HEAD',
); uri: apiUri,
}, timeout: defaultTimeout); ),
);
},
);
test('GET 3', () async { test(
expect( 'GET 1',
Curl.parse( () async {
r"""curl --url 'https://api.apidash.dev/country/data?code=IND'""", expect(
), Curl.parse(
Curl( r"""curl --url 'https://api.apidash.dev'""",
method: 'GET', ),
uri: Uri.parse('https://api.apidash.dev/country/data?code=IND'), Curl(
), method: 'GET',
); uri: apiUri,
}, timeout: defaultTimeout); ),
);
},
);
test('GET 4', () async { test(
expect( 'GET 2',
Curl.parse( () async {
r"""curl --url 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'""", expect(
), Curl.parse(
Curl( r"""curl --url 'https://api.apidash.dev/country/data?code=US'""",
method: 'GET', ),
uri: Uri.parse( Curl(
'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'), method: 'GET',
), uri: Uri.parse('https://api.apidash.dev/country/data?code=US'),
); ),
}, timeout: defaultTimeout); );
},
);
test('GET with GitHub API', () async { test(
expect( 'GET 3',
Curl.parse( () async {
r"""curl --url 'https://api.github.com/repos/foss42/apidash' \ 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'""", --header 'User-Agent: Test Agent'""",
), ),
Curl( Curl(
method: 'GET', method: 'GET',
uri: Uri.parse('https://api.github.com/repos/foss42/apidash'), uri: Uri.parse('https://api.github.com/repos/foss42/apidash'),
headers: {"User-Agent": "Test Agent"}, headers: {"User-Agent": "Test Agent"},
), ),
); );
}, timeout: defaultTimeout); },
);
test('GET with GitHub API and raw parameter', () async { test(
expect( 'GET with GitHub API and raw parameter',
Curl.parse( () async {
r"""curl --url 'https://api.github.com/repos/foss42/apidash?raw=true' \ expect(
Curl.parse(
r"""curl --url 'https://api.github.com/repos/foss42/apidash?raw=true' \
--header 'User-Agent: Test Agent'""", --header 'User-Agent: Test Agent'""",
), ),
Curl( Curl(
method: 'GET', method: 'GET',
uri: Uri.parse('https://api.github.com/repos/foss42/apidash?raw=true'), uri:
headers: {"User-Agent": "Test Agent"}, Uri.parse('https://api.github.com/repos/foss42/apidash?raw=true'),
), headers: {"User-Agent": "Test Agent"},
); ),
}, timeout: defaultTimeout); );
},
);
test('POST with text/plain content', () async { test(
expect( 'POST with text/plain content',
Curl.parse( () async {
r"""curl --request POST \ expect(
Curl.parse(
r"""curl --request POST \
--url 'https://api.apidash.dev/case/lower' \ --url 'https://api.apidash.dev/case/lower' \
--header 'Content-Type: text/plain' \ --header 'Content-Type: text/plain' \
--data '{ --data '{
"text": "I LOVE Flutter" "text": "I LOVE Flutter"
}'""", }'""",
), ),
Curl( Curl(
method: 'POST', method: 'POST',
uri: Uri.parse('https://api.apidash.dev/case/lower'), uri: Uri.parse('https://api.apidash.dev/case/lower'),
headers: {"Content-Type": "text/plain"}, headers: {"Content-Type": "text/plain"},
data: r"""{ data: r"""{
"text": "I LOVE Flutter" "text": "I LOVE Flutter"
}""", }""",
), ),
); );
}, timeout: defaultTimeout); },
);
test('POST with application/json content', () async { test(
expect( 'POST with application/json content',
Curl.parse( () async {
r"""curl --request POST \ expect(
Curl.parse(
r"""curl --request POST \
--url 'https://api.apidash.dev/case/lower' \ --url 'https://api.apidash.dev/case/lower' \
--header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \
--data '{ --data '{
@ -266,12 +333,12 @@ void main() {
"no": 1.2, "no": 1.2,
"arr": ["null", "true", "false", null] "arr": ["null", "true", "false", null]
}'""", }'""",
), ),
Curl( Curl(
method: 'POST', method: 'POST',
uri: Uri.parse('https://api.apidash.dev/case/lower'), uri: Uri.parse('https://api.apidash.dev/case/lower'),
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
data: r"""{ data: r"""{
"text": "I LOVE Flutter", "text": "I LOVE Flutter",
"flag": null, "flag": null,
"male": true, "male": true,
@ -279,7 +346,8 @@ void main() {
"no": 1.2, "no": 1.2,
"arr": ["null", "true", "false", null] "arr": ["null", "true", "false", null]
}""", }""",
), ),
); );
}, timeout: defaultTimeout); },
);
} }