Skip to content

Commit

Permalink
✅ ++
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 24, 2023
1 parent 0704b98 commit 42b8656
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 50 deletions.
50 changes: 0 additions & 50 deletions dio/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,6 @@ import 'mock/adapters.dart';
import 'utils.dart';

void main() {
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');
});

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'],
},
caseSensitive: true,
);
expect(headers['SET-COOKIE']?.length, 2);
// Although it's case-sensitive, we still use case-insensitive map.
expect(headers['set-cookie']?.length, 2);
expect(headers['content-length']?.length, 1);
expect(headers['Test']?.length, 2);
});
});

test('send with an invalid URL', () async {
await expectLater(
Dio().get('http://http.invalid'),
Expand Down
56 changes: 56 additions & 0 deletions dio/test/headers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:async';

import 'package:dio/dio.dart';
import 'package:test/test.dart';

void main() {
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');
});

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'],
},
caseSensitive: true,
);
expect(headers['SET-COOKIE']?.length, 2);
// Although it's case-sensitive, we still use case-insensitive map.
expect(headers['set-cookie']?.length, 2);
expect(headers['content-length']?.length, 1);
expect(headers['Test']?.length, 2);
});
});
}
34 changes: 34 additions & 0 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
@TestOn('vm')
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';
import 'mock/http_mock.dart';
import 'mock/http_mock.mocks.dart';
import 'utils.dart';

void main() {
Expand Down Expand Up @@ -477,4 +483,32 @@ void main() {
expect(response.data, 'test');
}
});

test('Headers can be case-sensitive', () async {
await HttpOverrides.runWithHttpOverrides(() async {
final client = MockHttpClient();
final dio = Dio();
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () => client,
);

late MockHttpClientRequest request;
when(client.openUrl(any, any)).thenAnswer((_) async {
request = MockHttpClientRequest();
return request;
});
await dio.get(
'https://pub.dev',
options: Options(
caseSensitiveHeaders: true,
headers: {
'Sensitive': 'foo-1',
'insensitive': 'foo-2',
},
),
);
expect(request.headers.value('Sensitive'), 'foo-1');
expect(request.headers.value('insensitive'), 'foo-2');
}, MockHttpOverrides());
});
}

0 comments on commit 42b8656

Please sign in to comment.