From 7430df2e10f22600857724c3a49387c6575d3372 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 23 Oct 2023 17:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`Headers.caseInsensitive`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/headers.dart | 29 +++++++++++++---- dio/test/basic_test.dart | 70 +++++++++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/dio/lib/src/headers.dart b/dio/lib/src/headers.dart index 42854c3e2..2b1873731 100644 --- a/dio/lib/src/headers.dart +++ b/dio/lib/src/headers.dart @@ -7,12 +7,16 @@ typedef HeaderForEachCallback = void Function(String name, List values); /// The headers class for requests and responses. class Headers { - Headers() : _map = caseInsensitiveKeyMap>(); + Headers({ + this.caseInsensitive = true, + }) : _map = caseInsensitiveKeyMap>(); /// Create the [Headers] from a [Map] instance. - Headers.fromMap(Map> map) - : _map = caseInsensitiveKeyMap>( - map.map((k, v) => MapEntry(k.trim().toLowerCase(), v)), + Headers.fromMap( + Map> map, { + this.caseInsensitive = true, + }) : _map = caseInsensitiveKeyMap>( + map.map((k, v) => MapEntry(k.trim(), v)), ); static const acceptHeader = 'accept'; @@ -28,6 +32,11 @@ class Headers { static final jsonMimeType = MediaType.parse(jsonContentType); + /// Whether the header key should be case-insensitive. + /// + /// Defaults to true. + final bool caseInsensitive; + final Map> _map; Map> get map => _map; @@ -35,7 +44,10 @@ class Headers { /// Returns the list of values for the header named [name]. If there /// is no header with the provided name, [:null:] will be returned. List? operator [](String name) { - return _map[name.trim().toLowerCase()]; + if (caseInsensitive) { + name = name.toLowerCase(); + } + return _map[name.trim()]; } /// Convenience method for the value for a single valued header. If @@ -63,9 +75,12 @@ class Headers { /// cleared before the value [value] is added as its value. void set(String name, dynamic value) { if (value == null) return; - name = name.trim().toLowerCase(); + if (caseInsensitive) { + name = name.toLowerCase(); + } + name = name.trim(); if (value is List) { - _map[name] = value.map((e) => e.toString()).toList(); + _map[name] = value.map((e) => '$e').toList(); } else { _map[name] = ['$value'.trim()]; } diff --git a/dio/test/basic_test.dart b/dio/test/basic_test.dart index e52faeae6..e21506dcb 100644 --- a/dio/test/basic_test.dart +++ b/dio/test/basic_test.dart @@ -8,34 +8,52 @@ import 'mock/adapters.dart'; import 'utils.dart'; void main() { - test('test headers', () { - final headers = Headers.fromMap({ - 'set-cookie': ['k=v', 'k1=v1'], - 'content-length': ['200'], - 'test': ['1', '2'], + group(Headers, () { + test('set', () { + final headers = Headers.fromMap({ + 'set-cookie': ['k=v', 'k1=v1'], + 'content-length': ['200'], + 'test': ['1', '2'], + }); + headers.add('SET-COOKIE', 'k2=v2'); + expect(headers.value('content-length'), '200'); + expect(Future(() => headers.value('test')), throwsException); + expect(headers['set-cookie']?.length, 3); + headers.remove('set-cookie', 'k=v'); + expect(headers['set-cookie']?.length, 2); + headers.removeAll('set-cookie'); + expect(headers['set-cookie'], isNull); + final ls = []; + headers.forEach((k, list) => ls.addAll(list)); + expect(ls.length, 3); + expect(headers.toString(), 'content-length: 200\ntest: 1\ntest: 2\n'); + headers.set('content-length', '300'); + expect(headers.value('content-length'), '300'); + headers.set('content-length', ['400']); + expect(headers.value('content-length'), '400'); }); - headers.add('SET-COOKIE', 'k2=v2'); - expect(headers.value('content-length'), '200'); - expect(Future(() => headers.value('test')), throwsException); - expect(headers['set-cookie']?.length, 3); - headers.remove('set-cookie', 'k=v'); - expect(headers['set-cookie']?.length, 2); - headers.removeAll('set-cookie'); - expect(headers['set-cookie'], isNull); - final ls = []; - headers.forEach((k, list) => ls.addAll(list)); - expect(ls.length, 3); - expect(headers.toString(), 'content-length: 200\ntest: 1\ntest: 2\n'); - headers.set('content-length', '300'); - expect(headers.value('content-length'), '300'); - headers.set('content-length', ['400']); - expect(headers.value('content-length'), '400'); - final headers1 = Headers(); - headers1.set('xx', 'v'); - expect(headers1.value('xx'), 'v'); - headers1.clear(); - expect(headers1.map.isEmpty, isTrue); + test('clear', () { + final headers1 = Headers(); + headers1.set('xx', 'v'); + expect(headers1.value('xx'), 'v'); + headers1.clear(); + expect(headers1.map.isEmpty, isTrue); + }); + + test('case-sensitive', () { + final headers = Headers.fromMap( + { + 'SET-COOKIE': ['k=v', 'k1=v1'], + 'content-length': ['200'], + 'Test': ['1', '2'], + }, + caseInsensitive: false, + ); + expect(headers['SET-COOKIE']?.length, 2); + expect(headers['content-length']?.length, 1); + expect(headers['Test']?.length, 2); + }); }); test('send with an invalid URL', () async {