mirror of
https://github.com/foss42/apidash.git
synced 2025-05-17 22:36:16 +08:00
Merge pull request #818 from WannaCry016/patch-5
Fix Issue #630 Unable to override Content-Type header charset
This commit is contained in:
@ -23,4 +23,10 @@ extension MapExtension on Map {
|
|||||||
String? getValueContentType() {
|
String? getValueContentType() {
|
||||||
return this[getKeyContentType()];
|
return this[getKeyContentType()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map removeKeyContentType() {
|
||||||
|
removeWhere(
|
||||||
|
(key, value) => key.toLowerCase() == HttpHeaders.contentTypeHeader);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import 'dart:io';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:seed/seed.dart';
|
import 'package:seed/seed.dart';
|
||||||
import '../consts.dart';
|
import '../consts.dart';
|
||||||
|
import '../extensions/extensions.dart';
|
||||||
import '../models/models.dart';
|
import '../models/models.dart';
|
||||||
import '../utils/utils.dart';
|
import '../utils/utils.dart';
|
||||||
import 'http_client_manager.dart';
|
import 'http_client_manager.dart';
|
||||||
@ -33,6 +34,7 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
|||||||
if (uriRec.$1 != null) {
|
if (uriRec.$1 != null) {
|
||||||
Uri requestUrl = uriRec.$1!;
|
Uri requestUrl = uriRec.$1!;
|
||||||
Map<String, String> headers = requestModel.enabledHeadersMap;
|
Map<String, String> headers = requestModel.enabledHeadersMap;
|
||||||
|
bool overrideContentType = false;
|
||||||
HttpResponse? response;
|
HttpResponse? response;
|
||||||
String? body;
|
String? body;
|
||||||
try {
|
try {
|
||||||
@ -43,16 +45,15 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
|||||||
|
|
||||||
if (kMethodsWithBody.contains(requestModel.method)) {
|
if (kMethodsWithBody.contains(requestModel.method)) {
|
||||||
var requestBody = requestModel.body;
|
var requestBody = requestModel.body;
|
||||||
if (requestBody != null && !isMultiPartRequest) {
|
if (requestBody != null &&
|
||||||
var contentLength = utf8.encode(requestBody).length;
|
!isMultiPartRequest &&
|
||||||
if (contentLength > 0) {
|
requestBody.isNotEmpty) {
|
||||||
body = requestBody;
|
body = requestBody;
|
||||||
headers[HttpHeaders.contentLengthHeader] =
|
if (requestModel.hasContentTypeHeader) {
|
||||||
contentLength.toString();
|
overrideContentType = true;
|
||||||
if (!requestModel.hasContentTypeHeader) {
|
} else {
|
||||||
headers[HttpHeaders.contentTypeHeader] =
|
headers[HttpHeaders.contentTypeHeader] =
|
||||||
requestModel.bodyContentType.header;
|
requestModel.bodyContentType.header;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isMultiPartRequest) {
|
if (isMultiPartRequest) {
|
||||||
@ -82,18 +83,28 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
|||||||
return (convertedMultiPartResponse, stopwatch.elapsed, null);
|
return (convertedMultiPartResponse, stopwatch.elapsed, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response = switch (requestModel.method) {
|
switch (requestModel.method) {
|
||||||
HTTPVerb.get => await client.get(requestUrl, headers: headers),
|
case HTTPVerb.get:
|
||||||
HTTPVerb.head => await client.head(requestUrl, headers: headers),
|
response = await client.get(requestUrl, headers: headers);
|
||||||
HTTPVerb.post =>
|
break;
|
||||||
await client.post(requestUrl, headers: headers, body: body),
|
case HTTPVerb.head:
|
||||||
HTTPVerb.put =>
|
response = await client.head(requestUrl, headers: headers);
|
||||||
await client.put(requestUrl, headers: headers, body: body),
|
break;
|
||||||
HTTPVerb.patch =>
|
case HTTPVerb.post:
|
||||||
await client.patch(requestUrl, headers: headers, body: body),
|
case HTTPVerb.put:
|
||||||
HTTPVerb.delete =>
|
case HTTPVerb.patch:
|
||||||
await client.delete(requestUrl, headers: headers, body: body),
|
case HTTPVerb.delete:
|
||||||
};
|
final request = prepareHttpRequest(
|
||||||
|
url: requestUrl,
|
||||||
|
method: requestModel.method.name.toUpperCase(),
|
||||||
|
headers: headers,
|
||||||
|
body: body,
|
||||||
|
overrideContentType: overrideContentType,
|
||||||
|
);
|
||||||
|
final streamed = await client.send(request);
|
||||||
|
response = await http.Response.fromStream(streamed);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (apiType == APIType.graphql) {
|
if (apiType == APIType.graphql) {
|
||||||
var requestBody = getGraphQLBody(requestModel);
|
var requestBody = getGraphQLBody(requestModel);
|
||||||
@ -131,3 +142,27 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
|||||||
void cancelHttpRequest(String? requestId) {
|
void cancelHttpRequest(String? requestId) {
|
||||||
httpClientManager.cancelRequest(requestId);
|
httpClientManager.cancelRequest(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
http.Request prepareHttpRequest({
|
||||||
|
required Uri url,
|
||||||
|
required String method,
|
||||||
|
required Map<String, String> headers,
|
||||||
|
required String? body,
|
||||||
|
bool overrideContentType = false,
|
||||||
|
}) {
|
||||||
|
var request = http.Request(method, url);
|
||||||
|
if (headers.getValueContentType() != null) {
|
||||||
|
request.headers[HttpHeaders.contentTypeHeader] =
|
||||||
|
headers.getValueContentType()!;
|
||||||
|
if (!overrideContentType) {
|
||||||
|
headers.removeKeyContentType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (body != null) {
|
||||||
|
request.body = body;
|
||||||
|
headers[HttpHeaders.contentLengthHeader] =
|
||||||
|
request.bodyBytes.length.toString();
|
||||||
|
}
|
||||||
|
request.headers.addAll(headers);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
@ -441,3 +441,28 @@ const httpRequestModelPost11 = HttpRequestModel(
|
|||||||
"text": "I LOVE Flutter"
|
"text": "I LOVE Flutter"
|
||||||
}""",
|
}""",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// POST request model with default (utf-8) content type charset
|
||||||
|
const httpRequestModelPost12 = HttpRequestModel(
|
||||||
|
method: HTTPVerb.post,
|
||||||
|
url: 'https://api.apidash.dev/case/lower',
|
||||||
|
bodyContentType: ContentType.json,
|
||||||
|
body: r"""{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""",
|
||||||
|
);
|
||||||
|
|
||||||
|
/// POST request model with charset override (latin1)
|
||||||
|
const httpRequestModelPost13 = HttpRequestModel(
|
||||||
|
method: HTTPVerb.post,
|
||||||
|
url: 'https://api.apidash.dev/case/lower',
|
||||||
|
headers: [
|
||||||
|
NameValueModel(
|
||||||
|
name: 'Content-Type', value: 'application/json; charset=latin1'),
|
||||||
|
],
|
||||||
|
isHeaderEnabledList: [true],
|
||||||
|
bodyContentType: ContentType.json,
|
||||||
|
body: r"""{
|
||||||
|
"text": "I LOVE Flutter"
|
||||||
|
}""",
|
||||||
|
);
|
||||||
|
@ -233,8 +233,23 @@ const requestModelGetBadSSL = RequestModel(
|
|||||||
httpRequestModel: httpRequestModelGetBadSSL,
|
httpRequestModel: httpRequestModelGetBadSSL,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// POST request model with content type override having no charset
|
||||||
const requestModelPost11 = RequestModel(
|
const requestModelPost11 = RequestModel(
|
||||||
id: 'post11',
|
id: 'post11',
|
||||||
apiType: APIType.rest,
|
apiType: APIType.rest,
|
||||||
httpRequestModel: httpRequestModelPost11,
|
httpRequestModel: httpRequestModelPost11,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// POST request model with default (utf-8) content type charset
|
||||||
|
const requestModelPost12 = RequestModel(
|
||||||
|
id: 'post12',
|
||||||
|
apiType: APIType.rest,
|
||||||
|
httpRequestModel: httpRequestModelPost12,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// POST request model with charset override (latin1)
|
||||||
|
const requestModelPost13 = RequestModel(
|
||||||
|
id: 'post13',
|
||||||
|
apiType: APIType.rest,
|
||||||
|
httpRequestModel: httpRequestModelPost13,
|
||||||
|
);
|
||||||
|
@ -58,10 +58,36 @@ void main() {
|
|||||||
final responseData = responseModel.fromResponse(response: responseRec.$1!);
|
final responseData = responseModel.fromResponse(response: responseRec.$1!);
|
||||||
expect(responseData.statusCode, 200);
|
expect(responseData.statusCode, 200);
|
||||||
expect(responseData.body, '{"data":"i love flutter"}');
|
expect(responseData.body, '{"data":"i love flutter"}');
|
||||||
expect(responseData.contentType, 'application/json; charset=utf-8');
|
expect(responseData.contentType, 'application/json');
|
||||||
expect(responseData.requestHeaders?['content-type'], 'application/json');
|
expect(responseData.requestHeaders?['content-type'], 'application/json');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Testing default contentType charset added by dart', () async {
|
||||||
|
var responseRec = await sendHttpRequest(
|
||||||
|
requestModelPost12.id,
|
||||||
|
requestModelPost12.apiType,
|
||||||
|
requestModelPost12.httpRequestModel!,
|
||||||
|
);
|
||||||
|
|
||||||
|
final responseData = responseModel.fromResponse(response: responseRec.$1!);
|
||||||
|
expect(responseData.statusCode, 200);
|
||||||
|
expect(responseData.requestHeaders?['content-type'],
|
||||||
|
'application/json; charset=utf-8');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing latin1 charset added by user', () async {
|
||||||
|
var responseRec = await sendHttpRequest(
|
||||||
|
requestModelPost13.id,
|
||||||
|
requestModelPost13.apiType,
|
||||||
|
requestModelPost13.httpRequestModel!,
|
||||||
|
);
|
||||||
|
|
||||||
|
final responseData = responseModel.fromResponse(response: responseRec.$1!);
|
||||||
|
expect(responseData.statusCode, 200);
|
||||||
|
expect(responseData.requestHeaders?['content-type'],
|
||||||
|
'application/json; charset=latin1');
|
||||||
|
});
|
||||||
|
|
||||||
test('Testing fromResponse for Bad SSL with certificate check', () async {
|
test('Testing fromResponse for Bad SSL with certificate check', () async {
|
||||||
var responseRec = await sendHttpRequest(
|
var responseRec = await sendHttpRequest(
|
||||||
requestModelGetBadSSL.id,
|
requestModelGetBadSSL.id,
|
||||||
|
Reference in New Issue
Block a user