From f52aa45d86f8c2279b8ce9bfb1b710677ff67a2b Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 5 Dec 2024 14:56:34 +0530 Subject: [PATCH] added requested tests for curl file import --- test/importer/curl_test.dart | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/importer/curl_test.dart diff --git a/test/importer/curl_test.dart b/test/importer/curl_test.dart new file mode 100644 index 00000000..ac2b3df5 --- /dev/null +++ b/test/importer/curl_test.dart @@ -0,0 +1,94 @@ +import 'package:apidash/importer/curl/curl.dart'; +import 'package:test/test.dart'; +import 'package:apidash_core/apidash_core.dart'; + +void main() { + group('CurlFileImport Tests', () { + late CurlFileImport curlImport; + + setUp(() { + curlImport = CurlFileImport(); + }); + + test('should parse simple GET request', () { + const curl = 'curl https://api.apidash.dev/users'; + final result = curlImport.getHttpRequestModel(curl); + + expect( + result, + const HttpRequestModel( + method: HTTPVerb.get, + url: 'https://api.apidash.dev/users', + headers: null, + params: [], + body: null, + bodyContentType: ContentType.text, + formData: null)); + }); + + test('should parse POST request with JSON body and headers', () { + const curl = ''' + curl -X POST https://api.apidash.dev/users + -H "Content-Type: application/json" + -H "Authorization: Bearer token123" + -d '{"name": "John", "age": 30}' + '''; + + final result = curlImport.getHttpRequestModel(curl); + + expect( + result, + const HttpRequestModel( + method: HTTPVerb.post, + url: 'https://api.apidash.dev/users', + headers: [ + NameValueModel(name: 'Content-Type', value: 'application/json'), + NameValueModel(name: 'Authorization', value: 'Bearer token123'), + ], + params: [], + body: '{"name": "John", "age": 30}', + bodyContentType: ContentType.json, + formData: null, + ), + ); + }); + + test('should parse form data request', () { + const curl = ''' + curl -X POST https://api.apidash.dev/upload + -F "file=@photo.jpg" + -F "description=My Photo" + '''; + + final result = curlImport.getHttpRequestModel(curl); + + expect( + result, + const HttpRequestModel( + method: HTTPVerb.post, + url: 'https://api.apidash.dev/upload', + headers: [ + NameValueModel(name: "Content-Type", value: "multipart/form-data") + ], + params: [], + body: null, + bodyContentType: ContentType.formdata, + formData: [ + FormDataModel( + name: 'file', value: 'photo.jpg', type: FormDataType.file), + FormDataModel( + name: 'description', + value: 'My Photo', + type: FormDataType.text), + ], + )); + }); + + test('should return null for invalid curl command', () { + const curl = 'invalid curl command'; + final result = curlImport.getHttpRequestModel(curl); + + expect(result, isNull); + }); + }); +}