From fa5e9b3bfe047a3e2f9b85b4cb6d12693b3d00b4 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sat, 19 Apr 2025 21:21:51 +0530 Subject: [PATCH 1/2] Adding test case for #630 --- test/models/http_request_models.dart | 14 ++++++++++ test/models/request_models.dart | 38 ++++++++++++++++++++++++++++ test/models/response_model_test.dart | 21 ++++++++++++--- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/test/models/http_request_models.dart b/test/models/http_request_models.dart index e3448c30..a56b8e39 100644 --- a/test/models/http_request_models.dart +++ b/test/models/http_request_models.dart @@ -427,3 +427,17 @@ const httpRequestModelGetBadSSL = HttpRequestModel( method: HTTPVerb.get, url: 'https://expired.badssl.com/', ); + +/// POST request model with content type override having no charset +const httpRequestModelPost11 = HttpRequestModel( + method: HTTPVerb.post, + url: 'https://api.apidash.dev/case/lower', + headers: [ + NameValueModel(name: 'Content-Type', value: 'application/json'), + ], + isHeaderEnabledList: [true], + bodyContentType: ContentType.json, + body: r"""{ +"text": "I LOVE Flutter" +}""", +); diff --git a/test/models/request_models.dart b/test/models/request_models.dart index 0e67a02c..fc1447e8 100644 --- a/test/models/request_models.dart +++ b/test/models/request_models.dart @@ -1,177 +1,207 @@ import 'package:apidash/models/models.dart'; +import 'package:apidash_core/apidash_core.dart'; import 'http_request_models.dart'; import 'http_response_models.dart'; /// Basic GET request model const requestModelGet1 = RequestModel( id: 'get1', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet1, ); /// GET request model with query params const requestModelGet2 = RequestModel( id: 'get2', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet2, ); /// GET request model with override query params const requestModelGet3 = RequestModel( id: 'get3', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet3, ); /// GET request model with different types of query params const requestModelGet4 = RequestModel( id: 'get4', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet4, ); /// GET request model with headers const requestModelGet5 = RequestModel( id: 'get5', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet5, ); /// GET request model with headers & query params const requestModelGet6 = RequestModel( id: 'get6', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet6, ); /// GET request model with body const requestModelGet7 = RequestModel( id: 'get7', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet7, ); /// GET request model with empty header & query param name const requestModelGet8 = RequestModel( id: 'get8', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet8, ); /// GET request model with some params enabled const requestModelGet9 = RequestModel( id: 'get9', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet9, ); /// GET Request model with some headers enabled const requestModelGet10 = RequestModel( id: 'get10', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet10, ); /// GET Request model with some headers & URL parameters enabled const requestModelGet11 = RequestModel( id: 'get11', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet11, ); /// Request model with all headers & URL parameters disabled const requestModelGet12 = RequestModel( id: 'get12', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet12, ); /// Basic HEAD request model const requestModelHead1 = RequestModel( id: 'head1', + apiType: APIType.rest, httpRequestModel: httpRequestModelHead1, ); /// Without URI Scheme (pass default as http) const requestModelHead2 = RequestModel( id: 'head2', + apiType: APIType.rest, httpRequestModel: httpRequestModelHead2, ); /// Basic POST request model (txt body) const requestModelPost1 = RequestModel( id: 'post1', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost1, ); /// POST request model with JSON body const requestModelPost2 = RequestModel( id: 'post2', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost2, ); /// POST request model with headers const requestModelPost3 = RequestModel( id: 'post3', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost3, ); /// POST request model with multipart body(text) const requestModelPost4 = RequestModel( id: 'post4', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost4, ); /// POST request model with multipart body and headers const requestModelPost5 = RequestModel( id: 'post5', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost5, ); /// POST request model with multipart body(text, file) const requestModelPost6 = RequestModel( id: 'post6', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost6, ); /// POST request model with multipart body and requestBody (the requestBody shouldn't be in codegen) const requestModelPost7 = RequestModel( id: 'post7', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost7, ); /// POST request model with multipart body and requestParams const requestModelPost8 = RequestModel( id: 'post8', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost8, ); /// POST request model with multipart body(file and text), requestParams, requestHeaders and requestBody const requestModelPost9 = RequestModel( id: 'post9', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost9, ); const requestModelPost10 = RequestModel( id: 'post9', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost10, ); /// PUT request model const requestModelPut1 = RequestModel( id: 'put1', + apiType: APIType.rest, httpRequestModel: httpRequestModelPut1, ); /// PATCH request model const requestModelPatch1 = RequestModel( id: 'patch1', + apiType: APIType.rest, httpRequestModel: httpRequestModelPatch1, ); /// Basic DELETE request model const requestModelDelete1 = RequestModel( id: 'delete1', + apiType: APIType.rest, httpRequestModel: httpRequestModelDelete1, ); /// Basic DELETE with body const requestModelDelete2 = RequestModel( id: 'delete2', + apiType: APIType.rest, httpRequestModel: httpRequestModelDelete2, ); // full request model RequestModel testRequestModel = RequestModel( id: '1', + apiType: APIType.rest, httpRequestModel: httpRequestModelPost10, responseStatus: 200, httpResponseModel: responseModel, @@ -192,11 +222,19 @@ Map requestModelJson = { /// Basic GET request model for apidash.dev const requestModelGet13 = RequestModel( id: 'get13', + apiType: APIType.rest, httpRequestModel: httpRequestModelGet13, ); /// Basic GET request model for badSSL const requestModelGetBadSSL = RequestModel( id: 'badSSL', + apiType: APIType.rest, httpRequestModel: httpRequestModelGetBadSSL, ); + +const requestModelPost11 = RequestModel( + id: 'post11', + apiType: APIType.rest, + httpRequestModel: httpRequestModelPost11, +); diff --git a/test/models/response_model_test.dart b/test/models/response_model_test.dart index 5438f108..fc0011bf 100644 --- a/test/models/response_model_test.dart +++ b/test/models/response_model_test.dart @@ -34,7 +34,7 @@ void main() { test('Testing fromResponse for contentType not Json', () async { var responseRec = await sendHttpRequest( requestModelGet13.id, - requestModelGet1.apiType, + requestModelGet13.apiType, requestModelGet13.httpRequestModel!, defaultUriScheme: kDefaultUriScheme, noSSL: false, @@ -47,10 +47,25 @@ void main() { expect(responseData.mediaType!.mimeType, 'text/html'); }); + test('Testing contentType override by the user having no charset (#630)', + () async { + var responseRec = await sendHttpRequest( + requestModelPost11.id, + requestModelPost11.apiType, + requestModelPost11.httpRequestModel!, + ); + + final responseData = responseModel.fromResponse(response: responseRec.$1!); + expect(responseData.statusCode, 200); + expect(responseData.body, '{"data":"i love flutter"}'); + expect(responseData.contentType, 'application/json; charset=utf-8'); + expect(responseData.requestHeaders?['content-type'], 'application/json'); + }); + test('Testing fromResponse for Bad SSL with certificate check', () async { var responseRec = await sendHttpRequest( requestModelGetBadSSL.id, - requestModelGet1.apiType, + requestModelGetBadSSL.apiType, requestModelGetBadSSL.httpRequestModel!, defaultUriScheme: kDefaultUriScheme, noSSL: false, @@ -62,7 +77,7 @@ void main() { test('Testing fromResponse for Bad SSL with no certificate check', () async { var responseRec = await sendHttpRequest( requestModelGetBadSSL.id, - requestModelGet1.apiType, + requestModelGetBadSSL.apiType, requestModelGetBadSSL.httpRequestModel!, defaultUriScheme: kDefaultUriScheme, noSSL: true, From 5337d5aa4f66b47d0829cddbfb9524324ab1f885 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sun, 20 Apr 2025 00:25:09 +0530 Subject: [PATCH 2/2] Update http_service.dart --- .../lib/services/http_service.dart | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/apidash_core/lib/services/http_service.dart b/packages/apidash_core/lib/services/http_service.dart index 73f82d9a..31cfcd9a 100644 --- a/packages/apidash_core/lib/services/http_service.dart +++ b/packages/apidash_core/lib/services/http_service.dart @@ -84,16 +84,15 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest( } response = switch (requestModel.method) { HTTPVerb.get => await client.get(requestUrl, headers: headers), - HTTPVerb.head => response = - await client.head(requestUrl, headers: headers), - HTTPVerb.post => response = - await client.post(requestUrl, headers: headers, body: body), - HTTPVerb.put => response = - await client.put(requestUrl, headers: headers, body: body), - HTTPVerb.patch => response = - await client.patch(requestUrl, headers: headers, body: body), - HTTPVerb.delete => response = - await client.delete(requestUrl, headers: headers, body: body), + HTTPVerb.head => await client.head(requestUrl, headers: headers), + HTTPVerb.post => + await client.post(requestUrl, headers: headers, body: body), + HTTPVerb.put => + await client.put(requestUrl, headers: headers, body: body), + HTTPVerb.patch => + await client.patch(requestUrl, headers: headers, body: body), + HTTPVerb.delete => + await client.delete(requestUrl, headers: headers, body: body), }; } if (apiType == APIType.graphql) {