From 7ab3f9009316e9767f49cdfa6470a89cd6d1cd9d Mon Sep 17 00:00:00 2001 From: Nishant Kumar Date: Tue, 19 Mar 2024 15:24:49 +0530 Subject: [PATCH] feature C libcurl --- lib/codegen/C/curl.dart | 176 +++++ lib/codegen/codegen.dart | 3 + lib/consts.dart | 3 +- test/codegen/cCurl_codegen_test.dart | 1030 ++++++++++++++++++++++++++ 4 files changed, 1211 insertions(+), 1 deletion(-) create mode 100644 lib/codegen/C/curl.dart create mode 100644 test/codegen/cCurl_codegen_test.dart diff --git a/lib/codegen/C/curl.dart b/lib/codegen/C/curl.dart new file mode 100644 index 00000000..8950a58e --- /dev/null +++ b/lib/codegen/C/curl.dart @@ -0,0 +1,176 @@ +import 'package:apidash/consts.dart'; +import 'package:apidash/utils/header_utils.dart'; +import 'package:jinja/jinja.dart' as jj; +import 'package:apidash/utils/utils.dart' + show getValidRequestUri, requestModelToHARJsonRequest; +import 'package:apidash/models/models.dart' show RequestModel; + +class cCurlCodeGen { + final String kTemplateStart = """#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { +"""; + + String kTemplateUrl = """ + + curl_easy_setopt(curl, CURLOPT_URL, "{{url}}"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; +"""; + + String kTemplateBody = """ + {% if body %} + const char *data = "{{body}}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + {% endif %} + +"""; + + String kTemplateFormData = """ + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + {% for field in fields %}{% if field.type == "file" %} + part = curl_mime_addpart(mime); + curl_mime_name(part, "{{field.name}}"); + curl_mime_filedata(part, "{{field.value}}"); + {% else %} + part = curl_mime_addpart(mime); + curl_mime_name(part, "{{field.name}}"); + curl_mime_data(part, "{{field.value}}", CURL_ZERO_TERMINATED); + {% endif %} + {% endfor %} +"""; + + String kTemplateHeader = """ + {% if headers %}{% for header, value in headers %} + headers = curl_slist_append(headers,"{{header}}: {{value}}") {% endfor %} + {% endif %} +"""; + String kTemplateHeaderEnd = """ + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); +"""; + String kStringFormDataHeader = """"""; + + String kTemplateQueryParam = """ + +"""; + + String kTemplateRequest = """ + + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "{{method}}"); +"""; + + final String kTemplateEnd = """ + + {% if formdata %}curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);{% endif %} + res = curl_easy_perform(curl); + {% if formdata %}curl_mime_free(mime);{% endif %} + {% if headers %}curl_slist_free_all(headers);{% endif %} + } + curl_easy_cleanup(curl); + return 0; +}"""; + + String? getCode( + RequestModel requestModel, + ) { + try { + String result = ""; + var hasBody = false; + var requestBody = requestModel.requestBody; + + String url = requestModel.url; + + var templateStart = jj.Template(kTemplateStart); + result += templateStart.render({ + "hasFormData": requestModel.hasFormData, + "hasFileInFormData": requestModel.hasFileInFormData, + }); + + var method = requestModel.method.name.toUpperCase(); + var templateRequest = jj.Template(kTemplateRequest); + result += templateRequest.render({ + "method": method, + "hasBody": hasBody, + }); + + var harJson = + requestModelToHARJsonRequest(requestModel, useEnabled: true); + var templateUrl = jj.Template(kTemplateUrl); + String correctUrl = harJson["url"]; + result += templateUrl.render({"url": correctUrl}); + + var rec = getValidRequestUri( + url, + requestModel.enabledRequestParams, + ); + + var headersList = requestModel.enabledRequestHeaders; + if (headersList != null || requestModel.hasBody) { + var headers = requestModel.enabledHeadersMap; + if (requestModel.hasJsonData || requestModel.hasTextData) { + headers.putIfAbsent(kHeaderContentType, + () => requestModel.requestBodyContentType.header); + } + if (headers.isNotEmpty) { + var templateHeader = jj.Template(kTemplateHeader); + result += templateHeader.render({ + "headers": headers, + }); + } + } + result += kTemplateHeaderEnd; + + Uri? uri = rec.$1; + + if (uri != null) { + if (requestModel.hasTextData || requestModel.hasJsonData) { + hasBody = true; + var templateRawBody = jj.Template(kTemplateBody); + result += templateRawBody.render({"body": requestBody}); + } else if (requestModel.hasFormData) { + hasBody = true; + var templateFormData = jj.Template(kTemplateFormData); + result += templateFormData.render({ + "hasFileInFormData": requestModel.hasFileInFormData, + "fields": requestModel.formDataMapList, + }); + } + if(requestModel.hasTextData){ + + } + if (uri.hasQuery) { + var params = uri.queryParameters; + if (params.isNotEmpty) { + var templateQueryParam = jj.Template(kTemplateQueryParam); + result += templateQueryParam.render({"params": params}); + } + } + var headers = requestModel.enabledHeadersMap; + bool allow = headers.isNotEmpty || + requestModel.hasJsonData || + requestModel.hasTextData; + var templateEnd = jj.Template(kTemplateEnd); + result += templateEnd.render({ + "formdata": requestModel.hasFormData, + "headers": allow, + }); + } + + return result; + } catch (e) { + return null; + } + } +} diff --git a/lib/codegen/codegen.dart b/lib/codegen/codegen.dart index 7c66b88f..cf121288 100644 --- a/lib/codegen/codegen.dart +++ b/lib/codegen/codegen.dart @@ -1,3 +1,4 @@ +import 'package:apidash/codegen/C/curl.dart'; import 'package:apidash/models/models.dart' show RequestModel; import 'package:apidash/consts.dart'; import 'package:apidash/utils/utils.dart' show getNewUuid; @@ -79,6 +80,8 @@ class Codegen { return RustUreqCodeGen().getCode(rM, boundary: boundary); case CodegenLanguage.phpGuzzle: return PhpGuzzleCodeGen().getCode(rM); + case CodegenLanguage.cCurlCodeGen: + return cCurlCodeGen().getCode(rM); } } } diff --git a/lib/consts.dart b/lib/consts.dart index e3d2dc6b..5e23e4f0 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -281,7 +281,8 @@ enum CodegenLanguage { javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"), javaHttpClient("Java (HttpClient)", "java", "java"), juliaHttp("Julia (HTTP)", "julia", "jl"), - phpGuzzle("PHP (guzzle)", "php", "php"); + phpGuzzle("PHP (guzzle)", "php", "php"), + cCurlCodeGen("C (Curl)", "C", "c"); const CodegenLanguage(this.label, this.codeHighlightLang, this.ext); final String label; diff --git a/test/codegen/cCurl_codegen_test.dart b/test/codegen/cCurl_codegen_test.dart new file mode 100644 index 00000000..626ffd89 --- /dev/null +++ b/test/codegen/cCurl_codegen_test.dart @@ -0,0 +1,1030 @@ +import 'package:apidash/codegen/codegen.dart'; +import 'package:apidash/consts.dart'; +import 'package:test/test.dart'; +import '../request_models.dart'; + +void main() { + final codeGen = Codegen(); + + group('GET Request', () { + test('GET 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet1, "https"), + expectedCode); + }); + + test('GET 2', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/country/data?code=US"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet2, "https"), + expectedCode); + }); + + test('GET 3', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/country/data?code=IND"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet3, "https"), + expectedCode); + }); + + test('GET 4', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet4, "https"), + expectedCode); + }); + + test('GET 5', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/foss42/apidash"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet5, "https"), + expectedCode); + }); + + test('GET 6', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/foss42/apidash?raw=true"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet6, "https"), + expectedCode); + }); + + test('GET 7', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet7, "https"), + expectedCode); + }); + + test('GET 8', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/foss42/apidash?raw=true"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet8, "https"), + expectedCode); + }); + + test('GET 9', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/humanize/social?num=8700000&add_space=true"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet9, "https"), + expectedCode); + }); + + test('GET 10', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/humanize/social"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect( + codeGen.getCode( + CodegenLanguage.cCurlCodeGen, + requestModelGet10, + "https", + ), + expectedCode); + }); + + test('GET 11', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/humanize/social?num=8700000&digits=3"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet11, "https"), + expectedCode); + }); + + test('GET 12', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/humanize/social"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelGet12, "https"), + expectedCode); + }); + }); + + group('HEAD Request', () { + test('HEAD 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelHead1, "https"), + expectedCode); + }); + + test('HEAD 2', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD"); + curl_easy_setopt(curl, CURLOPT_URL, "http://api.apidash.dev"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelHead2, "http"), + expectedCode); + }); + }); + + group('POST Request', () { + test('POST 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/case/lower"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"Content-Type: text/plain") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"text": "I LOVE Flutter" +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost1, "https"), + expectedCode); + }); + + test('POST 2', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/case/lower"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"Content-Type: application/json") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"text": "I LOVE Flutter", +"flag": null, +"male": true, +"female": false, +"no": 1.2, +"arr": ["null", "true", "false", null] +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost2, "https"), + expectedCode); + }); + + test('POST 3', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/case/lower"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + headers = curl_slist_append(headers,"Content-Type: application/json") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"text": "I LOVE Flutter" +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost3, "https"), + expectedCode); + }); + + test('POST 4', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/form"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "text"); + curl_mime_data(part, "API", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "sep"); + curl_mime_data(part, "|", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "times"); + curl_mime_data(part, "3", CURL_ZERO_TERMINATED); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost4, "https"), + expectedCode); + }); + + test('POST 5', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/form"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "text"); + curl_mime_data(part, "API", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "sep"); + curl_mime_data(part, "|", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "times"); + curl_mime_data(part, "3", CURL_ZERO_TERMINATED); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost5, "https"), + expectedCode); + }); + + test('POST 6', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/img"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "token"); + curl_mime_data(part, "xyz", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "imfile"); + curl_mime_filedata(part, "/Documents/up/1.png"); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost6, "https"), + expectedCode); + }); + + test('POST 7', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/img"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "token"); + curl_mime_data(part, "xyz", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "imfile"); + curl_mime_filedata(part, "/Documents/up/1.png"); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost7, "https"), + expectedCode); + }); + + test('POST 8', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/form?size=2&len=3"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "text"); + curl_mime_data(part, "API", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "sep"); + curl_mime_data(part, "|", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "times"); + curl_mime_data(part, "3", CURL_ZERO_TERMINATED); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost8, "https"), + expectedCode); + }); + + test('POST 9', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.apidash.dev/io/img?size=2&len=3"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"User-Agent: Test Agent") + headers = curl_slist_append(headers,"Keep-Alive: true") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_mime *mime; + curl_mimepart *part; + mime = curl_mime_init(curl); + + part = curl_mime_addpart(mime); + curl_mime_name(part, "token"); + curl_mime_data(part, "xyz", CURL_ZERO_TERMINATED); + + + part = curl_mime_addpart(mime); + curl_mime_name(part, "imfile"); + curl_mime_filedata(part, "/Documents/up/1.png"); + + + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + res = curl_easy_perform(curl); + curl_mime_free(mime); + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPost9, "https"), + expectedCode); + }); + }); + + group('PUT Request', () { + test('PUT 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users/2"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"Content-Type: application/json") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"name": "morpheus", +"job": "zion resident" +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect(codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPut1, "https"), + expectedCode); + }); + }); + + group('PATCH Request', () { + test('PATCH 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); + curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users/2"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"Content-Type: application/json") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"name": "marfeus", +"job": "accountant" +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect( + codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelPatch1, "https"), + expectedCode); + }); + }); + + group('DELETE Request', () { + test('DELETE 1', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users/2"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + + res = curl_easy_perform(curl); + + + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect( + codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelDelete1, "https"), + expectedCode); + }); + + test('DELETE 2', () { + const expectedCode = r"""#include +#include +#include +#include + + +int main() { + CURL *curl; + CURLcode res; + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users/2"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https"); + struct curl_slist *headers = NULL; + headers = curl_slist_append(headers,"Content-Type: application/json") + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + const char *data = "{ +"name": "marfeus", +"job": "accountant" +}"; + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + + + res = curl_easy_perform(curl); + + curl_slist_free_all(headers); + } + curl_easy_cleanup(curl); + return 0; +}"""; + expect( + codeGen.getCode(CodegenLanguage.cCurlCodeGen, requestModelDelete2, "https"), + expectedCode); + }); + }); +}