Skip to content

Commit

Permalink
Headers.caseInsensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 23, 2023
1 parent fad3fb8 commit 7430df2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
29 changes: 22 additions & 7 deletions dio/lib/src/headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ typedef HeaderForEachCallback = void Function(String name, List<String> values);

/// The headers class for requests and responses.
class Headers {
Headers() : _map = caseInsensitiveKeyMap<List<String>>();
Headers({
this.caseInsensitive = true,
}) : _map = caseInsensitiveKeyMap<List<String>>();

/// Create the [Headers] from a [Map] instance.
Headers.fromMap(Map<String, List<String>> map)
: _map = caseInsensitiveKeyMap<List<String>>(
map.map((k, v) => MapEntry(k.trim().toLowerCase(), v)),
Headers.fromMap(
Map<String, List<String>> map, {
this.caseInsensitive = true,
}) : _map = caseInsensitiveKeyMap<List<String>>(
map.map((k, v) => MapEntry(k.trim(), v)),
);

static const acceptHeader = 'accept';
Expand All @@ -28,14 +32,22 @@ class Headers {

static final jsonMimeType = MediaType.parse(jsonContentType);

/// Whether the header key should be case-insensitive.
///
/// Defaults to true.
final bool caseInsensitive;

final Map<String, List<String>> _map;

Map<String, List<String>> get map => _map;

/// Returns the list of values for the header named [name]. If there
/// is no header with the provided name, [:null:] will be returned.
List<String>? 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
Expand Down Expand Up @@ -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<String>((e) => e.toString()).toList();
_map[name] = value.map<String>((e) => '$e').toList();
} else {
_map[name] = ['$value'.trim()];
}
Expand Down
70 changes: 44 additions & 26 deletions dio/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7430df2

Please sign in to comment.