From c7b822223ba01440d5cfe2c8e8d070664569db37 Mon Sep 17 00:00:00 2001 From: Pratham Date: Tue, 26 Nov 2024 01:13:18 +0530 Subject: [PATCH] Split the test file into 3 files --- .../curl_parser/test/curl_model_test.dart | 35 +++ .../curl_parser/test/curl_parser_test.dart | 158 ++----------- .../curl_parser/test/dart_to_curl_test.dart | 218 ++++++++++++++++++ 3 files changed, 272 insertions(+), 139 deletions(-) create mode 100644 packages/curl_parser/test/curl_model_test.dart create mode 100644 packages/curl_parser/test/dart_to_curl_test.dart diff --git a/packages/curl_parser/test/curl_model_test.dart b/packages/curl_parser/test/curl_model_test.dart new file mode 100644 index 00000000..6ccbb5ca --- /dev/null +++ b/packages/curl_parser/test/curl_model_test.dart @@ -0,0 +1,35 @@ +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'; + +void main() { + 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, + ); + }); +} diff --git a/packages/curl_parser/test/curl_parser_test.dart b/packages/curl_parser/test/curl_parser_test.dart index ad85c523..4fa1abcd 100644 --- a/packages/curl_parser/test/curl_parser_test.dart +++ b/packages/curl_parser/test/curl_parser_test.dart @@ -1,5 +1,4 @@ import 'dart:io'; - import 'package:apidash_core/consts.dart'; import 'package:apidash_core/models/form_data_model.dart'; import 'package:curl_parser/curl_parser.dart'; @@ -12,26 +11,13 @@ void main() { test('parse an easy cURL', () async { expect( Curl.parse('curl -X GET https://api.apidash.dev/'), - Curl( - method: 'GET', - uri: exampleDotComUri, - ), - ); - }, timeout: defaultTimeout); - - test('compose an easy cURL', () async { - expect( - Curl.parse('curl -X GET https://api.apidash.dev/'), - Curl( - method: 'GET', - uri: exampleDotComUri, - ), + Curl(method: 'GET', uri: exampleDotComUri), ); }, 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" \\ + const curl = r'''curl -X POST https://api.apidash.dev/upload \ + -F "file=@/path/to/image.jpg" \ -F "username=john" '''; @@ -40,9 +26,7 @@ void main() { Curl( method: 'POST', uri: Uri.parse('https://api.apidash.dev/upload'), - headers: { - "Content-Type": "multipart/form-data", - }, + headers: {"Content-Type": "multipart/form-data"}, form: true, formData: [ FormDataModel( @@ -57,10 +41,10 @@ void main() { }); 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" \\ + 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" '''; @@ -69,9 +53,7 @@ void main() { Curl( method: 'POST', uri: Uri.parse('https://api.apidash.dev/upload'), - headers: { - "Content-Type": "multipart/form-data", - }, + headers: {"Content-Type": "multipart/form-data"}, form: true, formData: [ FormDataModel( @@ -88,52 +70,17 @@ void main() { }); 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" \\ + 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, - ); + expect(() => Curl.parse(curl), throwsException); }); test('Check quotes support for URL string', () async { expect( Curl.parse('curl -X GET "https://api.apidash.dev/"'), - Curl( - method: 'GET', - uri: exampleDotComUri, - ), + Curl(method: 'GET', uri: exampleDotComUri), ); }, timeout: defaultTimeout); @@ -185,17 +132,11 @@ void main() { }, timeout: defaultTimeout); test('throw exception when parses wrong input', () async { - expect( - () => Curl.parse('1f'), - throwsException, - ); + expect(() => Curl.parse('1f'), throwsException); }, timeout: defaultTimeout); test('tryParse return null when parses wrong input', () async { - expect( - Curl.tryParse('1f'), - null, - ); + expect(Curl.tryParse('1f'), null); }, timeout: defaultTimeout); test('tryParse success', () async { @@ -214,56 +155,7 @@ void main() { ); }, timeout: defaultTimeout); - test('GET 1', () async { - expect( - Curl.parse( - r"""curl --url 'https://api.apidash.dev'""", - ), - Curl( - method: 'GET', - uri: Uri.parse('https://api.apidash.dev'), - ), - ); - }, timeout: defaultTimeout); - - test('GET 2', () async { - expect( - Curl.parse( - r"""curl --url 'https://api.apidash.dev/country/data?code=US'""", - ), - Curl( - method: 'GET', - uri: Uri.parse('https://api.apidash.dev/country/data?code=US'), - ), - ); - }, timeout: defaultTimeout); - - test('GET 3', () async { - expect( - Curl.parse( - r"""curl --url 'https://api.apidash.dev/country/data?code=IND'""", - ), - Curl( - method: 'GET', - uri: Uri.parse('https://api.apidash.dev/country/data?code=IND'), - ), - ); - }, timeout: defaultTimeout); - - test('GET 4', () async { - expect( - Curl.parse( - r"""curl --url 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'""", - ), - Curl( - method: 'GET', - uri: Uri.parse( - 'https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true'), - ), - ); - }, timeout: defaultTimeout); - - test('GET 5', () async { + test('GET with GitHub API', () async { expect( Curl.parse( r"""curl --url 'https://api.github.com/repos/foss42/apidash' \ @@ -277,7 +169,7 @@ void main() { ); }, timeout: defaultTimeout); - test('GET 6', () async { + test('GET with GitHub API and raw parameter', () async { expect( Curl.parse( r"""curl --url 'https://api.github.com/repos/foss42/apidash?raw=true' \ @@ -291,19 +183,7 @@ void main() { ); }, timeout: defaultTimeout); - test('HEAD 2', () async { - expect( - Curl.parse( - r"""curl --head --url 'http://api.apidash.dev'""", - ), - Curl( - method: 'HEAD', - uri: Uri.parse('http://api.apidash.dev'), - ), - ); - }, timeout: defaultTimeout); - - test('POST 1', () async { + test('POST with text/plain content', () async { expect( Curl.parse( r"""curl --request POST \ @@ -324,7 +204,7 @@ void main() { ); }, timeout: defaultTimeout); - test('POST 2', () async { + test('POST with application/json content', () async { expect( Curl.parse( r"""curl --request POST \ diff --git a/packages/curl_parser/test/dart_to_curl_test.dart b/packages/curl_parser/test/dart_to_curl_test.dart new file mode 100644 index 00000000..cde85ad0 --- /dev/null +++ b/packages/curl_parser/test/dart_to_curl_test.dart @@ -0,0 +1,218 @@ +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'; + +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(), + 'curl "https://api.apidash.dev" \\\n' + ' -H "Content-Type: application/json" \\\n' + ' -H "Authorization: Bearer token123"', + ); + }); + + test('POST request with data', () { + final curl = Curl( + method: 'POST', + uri: Uri.parse('https://api.apidash.dev/test'), + headers: {'Content-Type': 'application/json'}, + data: '{"key": "value"}', + ); + expect( + curl.toCurlString(), + 'curl -X POST "https://api.apidash.dev/test" \\\n' + ' -H "Content-Type: application/json" \\\n' + ' -d \'{"key": "value"}\'', + ); + }); + }); + + group('Form Data', () { + test('request with form data', () { + final 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/file.txt", + type: FormDataType.file, + ), + FormDataModel( + name: "name", + value: "test", + type: FormDataType.text, + ), + ], + ); + expect( + curl.toCurlString(), + 'curl -X POST "https://api.apidash.dev/upload" \\\n' + ' -H "Content-Type: multipart/form-data" \\\n' + ' -F "file=/path/to/file.txt" \\\n' + ' -F "name=test"', + ); + }); + }); + + 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(), + 'curl "https://api.apidash.dev" \\\n' + ' -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(), + 'curl "https://api.apidash.dev" \\\n' + ' -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(), + 'curl "https://api.apidash.dev" \\\n' + ' -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(), + 'curl "https://api.apidash.dev" \\\n' + ' -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(), + 'curl "https://api.apidash.dev" \\\n' + ' -k', + ); + }); + + test('request with location flag', () { + final curl = Curl( + method: 'GET', + uri: Uri.parse('https://api.apidash.dev'), + location: true, + ); + expect( + curl.toCurlString(), + 'curl "https://api.apidash.dev" \\\n' + ' -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(), + 'curl -X POST "https://api.apidash.dev/test" \\\n' + ' -H "Content-Type: application/json" \\\n' + ' -H "Authorization: Bearer token123" \\\n' + ' -d \'{"key": "value"}\' \\\n' + ' -b \'session=abc123\' \\\n' + ' -u \'username:password\' \\\n' + ' -e \'https://example.com\' \\\n' + ' -A \'MyApp/1.0\' \\\n' + ' -k \\\n' + ' -L', + ); + }); + }); +}