From b532044981d12397522f1e42a7d062df7bddab38 Mon Sep 17 00:00:00 2001 From: Vidya Sagar <59490902+vidya-hub@users.noreply.github.com> Date: Sun, 7 Jan 2024 19:32:09 +0530 Subject: [PATCH] chore: fixed code generation test cases --- lib/codegen/js/axios.dart | 22 ++++++++++++--------- lib/codegen/js/fetch.dart | 1 - lib/codegen/python/http_client.dart | 3 +-- test/codegen/js_fetch_codegen_test.dart | 18 ++++++----------- test/codegen/nodejs_fetch_codegen_test.dart | 18 ++++++----------- test/models/request_model_test.dart | 2 ++ 6 files changed, 28 insertions(+), 36 deletions(-) diff --git a/lib/codegen/js/axios.dart b/lib/codegen/js/axios.dart index fc65905b..ef73f646 100644 --- a/lib/codegen/js/axios.dart +++ b/lib/codegen/js/axios.dart @@ -10,10 +10,10 @@ class AxiosCodeGen { final bool isNodeJs; - String kStringImportNode = """import axios from 'axios'; -{% if isFormDataRequest and isNodeJs %}const fs = require('fs'); -{% endif %} + String kStringImportNode = """{% if isNodeJs %}import axios from 'axios'; +{% endif %}{% if isFormDataRequest and isNodeJs %} +const fs = require('fs');{% endif %} """; String kTemplateStart = """let config = { @@ -82,11 +82,14 @@ async function buildFormData(fields) { }); String result = importsData; - var templateMultiPartBody = jj.Template(kMultiPartBodyTemplate); - var renderedMultiPartBody = templateMultiPartBody.render({ - "isNodeJs": isNodeJs, - }); - result += renderedMultiPartBody; + if (requestModel.isFormDataRequest && + requestModel.formDataMapList.isNotEmpty) { + var templateMultiPartBody = jj.Template(kMultiPartBodyTemplate); + var renderedMultiPartBody = templateMultiPartBody.render({ + "isNodeJs": isNodeJs, + }); + result += renderedMultiPartBody; + } String url = requestModel.url; if (!url.contains("://") && url.isNotEmpty) { @@ -128,7 +131,8 @@ async function buildFormData(fields) { } var templateBody = jj.Template(kTemplateBody); - if (requestModel.isFormDataRequest) { + if (requestModel.isFormDataRequest && + requestModel.formDataMapList.isNotEmpty) { var getFieldDataTemplate = jj.Template(kGetFormDataTemplate); result += templateBody.render({ diff --git a/lib/codegen/js/fetch.dart b/lib/codegen/js/fetch.dart index 31f7a211..fb2b4e36 100644 --- a/lib/codegen/js/fetch.dart +++ b/lib/codegen/js/fetch.dart @@ -14,7 +14,6 @@ class FetchCodeGen { import fetch from 'node-fetch'; {% if isFormDataRequest %}const fs = require('fs');{% endif %} - """; String kTemplateStart = """let url = '{{url}}'; diff --git a/lib/codegen/python/http_client.dart b/lib/codegen/python/http_client.dart index cc34cb70..8efb368a 100644 --- a/lib/codegen/python/http_client.dart +++ b/lib/codegen/python/http_client.dart @@ -7,8 +7,7 @@ import 'package:apidash/models/models.dart' show RequestModel; import 'package:apidash/consts.dart'; class PythonHttpClientCodeGen { - final String kTemplateStart = """import http.client -{% if isFormDataRequest %} + final String kTemplateStart = """import http.client{% if isFormDataRequest %} import mimetypes from codecs import encode {% endif %} diff --git a/test/codegen/js_fetch_codegen_test.dart b/test/codegen/js_fetch_codegen_test.dart index 43d053f9..e7426bf1 100644 --- a/test/codegen/js_fetch_codegen_test.dart +++ b/test/codegen/js_fetch_codegen_test.dart @@ -398,8 +398,7 @@ let options = { headers: { "Content-Type": "text/plain" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -428,8 +427,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -459,8 +457,7 @@ let options = { "Content-Type": "application/json", "User-Agent": "Test Agent" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -491,8 +488,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}" + body: "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}" }; let status; @@ -523,8 +519,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" + body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" }; let status; @@ -580,8 +575,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" + body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" }; let status; diff --git a/test/codegen/nodejs_fetch_codegen_test.dart b/test/codegen/nodejs_fetch_codegen_test.dart index c06ceee5..feca786d 100644 --- a/test/codegen/nodejs_fetch_codegen_test.dart +++ b/test/codegen/nodejs_fetch_codegen_test.dart @@ -418,8 +418,7 @@ let options = { headers: { "Content-Type": "text/plain" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -450,8 +449,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -483,8 +481,7 @@ let options = { "Content-Type": "application/json", "User-Agent": "Test Agent" }, - body: -"{\n\"text\": \"I LOVE Flutter\"\n}" + body: "{\n\"text\": \"I LOVE Flutter\"\n}" }; let status; @@ -517,8 +514,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}" + body: "{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}" }; let status; @@ -551,8 +547,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" + body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" }; let status; @@ -612,8 +607,7 @@ let options = { headers: { "Content-Type": "application/json" }, - body: -"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" + body: "{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}" }; let status; diff --git a/test/models/request_model_test.dart b/test/models/request_model_test.dart index b1b56bfd..66064d31 100644 --- a/test/models/request_model_test.dart +++ b/test/models/request_model_test.dart @@ -113,6 +113,7 @@ void main() { "requestBody": '''{ "text":"WORLD" }''', + 'requestFormDataList': null, 'responseStatus': null, 'message': null, 'responseModel': null @@ -147,6 +148,7 @@ void main() { "Enabled Params: null", "Request Body Content Type: ContentType.json", 'Request Body: {\n"text":"WORLD"\n}', + 'Request FormData: null', "Response Status: null", "Response Message: null", "Response: null"