From 671b7811209de66544cca48a1c1ca062fd7a6b49 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Mon, 25 Mar 2024 16:20:14 -0700 Subject: [PATCH] Set content-length request header --- .../integration_test/client_profile_test.dart | 15 +++++++-------- pkgs/cupertino_http/lib/src/cupertino_client.dart | 12 ++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pkgs/cupertino_http/example/integration_test/client_profile_test.dart b/pkgs/cupertino_http/example/integration_test/client_profile_test.dart index 4c64241dcd..a03a49efae 100644 --- a/pkgs/cupertino_http/example/integration_test/client_profile_test.dart +++ b/pkgs/cupertino_http/example/integration_test/client_profile_test.dart @@ -54,8 +54,8 @@ void main() { expect(profile.requestData.contentLength, 2); expect(profile.requestData.endTime, isNotNull); expect(profile.requestData.error, isNull); - // XXX Should 'content-length' also be in the headers? It is not for the - // request but it is for the response. + expect( + profile.requestData.headers, containsPair('Content-Length', ['2'])); expect(profile.requestData.headers, containsPair('Content-Type', ['text/plain; charset=utf-8'])); expect(profile.requestData.persistentConnection, isNull); @@ -66,8 +66,6 @@ void main() { test('response attributes', () { expect(profile.responseData.bodyBytes, 'Hello World'.codeUnits); expect(profile.responseData.compressionState, isNull); - // XXX how are request and response connectionInfo supposed to be - // different? expect(profile.responseData.connectionInfo, isNull); expect(profile.responseData.contentLength, 11); expect(profile.responseData.endTime, isNotNull); @@ -128,6 +126,7 @@ void main() { expect(profile.requestData.contentLength, isNull); expect(profile.requestData.endTime, isNotNull); expect(profile.requestData.startTime, isNotNull); + expect(profile.requestData.headers, isNot(contains('Content-Length'))); }); }); @@ -159,8 +158,8 @@ void main() { expect(profile.requestData.contentLength, 2); expect(profile.requestData.endTime, isNotNull); expect(profile.requestData.error, startsWith('ClientException:')); - // XXX Should 'content-length' also be in the headers? It is not for the - // request but it is for the response. + expect( + profile.requestData.headers, containsPair('Content-Length', ['2'])); expect(profile.requestData.headers, containsPair('Content-Type', ['text/plain; charset=utf-8'])); expect(profile.requestData.persistentConnection, isNull); @@ -232,8 +231,8 @@ void main() { expect(profile.requestData.contentLength, 2); expect(profile.requestData.endTime, isNotNull); expect(profile.requestData.error, isNull); - // XXX Should 'content-length' also be in the headers? It is not for the - // request but it is for the response. + expect( + profile.requestData.headers, containsPair('Content-Length', ['2'])); expect(profile.requestData.headers, containsPair('Content-Type', ['text/plain; charset=utf-8'])); expect(profile.requestData.persistentConnection, isNull); diff --git a/pkgs/cupertino_http/lib/src/cupertino_client.dart b/pkgs/cupertino_http/lib/src/cupertino_client.dart index e70f9b5ecc..bd88820e5b 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_client.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_client.dart @@ -315,6 +315,12 @@ class CupertinoClient extends BaseClient { // `httpBodyStream` requires a lot of expensive setup and data passing. urlRequest.httpBody = Data.fromList(request.bodyBytes); profile?.requestData.bodySink.add(request.bodyBytes); + if (profile != null) { + final headers = + Map>.from(profile.requestData.headers!); + headers['Content-Length'] = ['${request.contentLength}']; + profile.requestData.headersListValues = headers; + } } else if (await _hasData(stream) case (true, final s)) { // If the request is supposed to be bodyless (e.g. GET requests) // then setting `httpBodyStream` will cause the request to fail - @@ -325,6 +331,12 @@ class CupertinoClient extends BaseClient { final splitter = StreamSplitter(s); urlRequest.httpBodyStream = splitter.split(); unawaited(profile.requestData.bodySink.addStream(splitter.split())); + if (request.contentLength != null) { + final headers = + Map>.from(profile.requestData.headers!); + headers['Content-Length'] = ['${request.contentLength}']; + profile.requestData.headersListValues = headers; + } } }