2021-03-18 23:10:19 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
class MockHttpHeaders extends HttpHeaders {
|
|
|
|
final Map<String, List<String>> _data = {};
|
|
|
|
final List<String> _noFolding = [];
|
2021-03-20 02:33:34 +00:00
|
|
|
Uri? _host;
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
List<String> get doNotFold => List<String>.unmodifiable(_noFolding);
|
|
|
|
|
|
|
|
@override
|
|
|
|
ContentType get contentType {
|
|
|
|
if (_data.containsKey(HttpHeaders.contentTypeHeader)) {
|
2021-03-20 02:33:34 +00:00
|
|
|
return ContentType.parse(_data[HttpHeaders.contentTypeHeader]!.join(','));
|
2021-03-18 23:10:19 +00:00
|
|
|
} else {
|
2021-03-20 02:33:34 +00:00
|
|
|
return ContentType.html;
|
2021-03-18 23:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
set contentType(ContentType? value) =>
|
|
|
|
set(HttpHeaders.contentTypeHeader, value?.value ?? ContentType.html);
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
DateTime get date => _data.containsKey(HttpHeaders.dateHeader)
|
2021-03-20 02:33:34 +00:00
|
|
|
? HttpDate.parse(_data[HttpHeaders.dateHeader]!.join(','))
|
|
|
|
: DateTime.now();
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
set date(DateTime? value) =>
|
|
|
|
set(HttpHeaders.dateHeader, HttpDate.format(value ?? DateTime.now()));
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
DateTime get expires => _data.containsKey(HttpHeaders.expiresHeader)
|
2021-03-20 02:33:34 +00:00
|
|
|
? HttpDate.parse(_data[HttpHeaders.expiresHeader]!.join(','))
|
|
|
|
: DateTime.now();
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
set expires(DateTime? value) =>
|
|
|
|
set(HttpHeaders.expiresHeader, HttpDate.format(value ?? DateTime.now()));
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
DateTime get ifModifiedSince =>
|
|
|
|
_data.containsKey(HttpHeaders.ifModifiedSinceHeader)
|
2021-03-20 02:33:34 +00:00
|
|
|
? HttpDate.parse(_data[HttpHeaders.ifModifiedSinceHeader]!.join(','))
|
|
|
|
: DateTime.now();
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
set ifModifiedSince(DateTime? value) => set(HttpHeaders.ifModifiedSinceHeader,
|
|
|
|
HttpDate.format(value ?? DateTime.now()));
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
String? get host {
|
2021-03-18 23:10:19 +00:00
|
|
|
if (_host != null) {
|
2021-03-20 02:33:34 +00:00
|
|
|
return _host!.host;
|
2021-03-18 23:10:19 +00:00
|
|
|
} else if (_data.containsKey(HttpHeaders.hostHeader)) {
|
2021-03-20 02:33:34 +00:00
|
|
|
_host = Uri.parse(_data[HttpHeaders.hostHeader]!.join(','));
|
|
|
|
return _host!.host;
|
2021-03-18 23:10:19 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
int? get port {
|
2021-03-18 23:10:19 +00:00
|
|
|
host; // Parse it
|
|
|
|
return _host?.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
List<String>? operator [](String name) => _data[name.toLowerCase()];
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void add(String name, Object value, {bool preserveHeaderCase = false}) {
|
|
|
|
var lower = preserveHeaderCase ? name : name.toLowerCase();
|
|
|
|
|
|
|
|
if (_data.containsKey(lower)) {
|
|
|
|
if (value is Iterable) {
|
2021-03-20 02:33:34 +00:00
|
|
|
_data[lower]!.addAll(value.map((x) => x.toString()).toList());
|
2021-03-18 23:10:19 +00:00
|
|
|
} else {
|
2021-03-20 02:33:34 +00:00
|
|
|
_data[lower]!.add(value.toString());
|
2021-03-18 23:10:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (value is Iterable) {
|
|
|
|
_data[lower] = value.map((x) => x.toString()).toList();
|
|
|
|
} else {
|
|
|
|
_data[lower] = [value.toString()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
_data.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void forEach(void Function(String name, List<String> values) f) {
|
|
|
|
_data.forEach(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void noFolding(String name) {
|
|
|
|
_noFolding.add(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void remove(String name, Object value) {
|
|
|
|
var lower = name.toLowerCase();
|
|
|
|
|
|
|
|
if (_data.containsKey(lower)) {
|
|
|
|
if (value is Iterable) {
|
|
|
|
for (var x in value) {
|
2021-03-20 02:33:34 +00:00
|
|
|
_data[lower]!.remove(x.toString());
|
2021-03-18 23:10:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-20 02:33:34 +00:00
|
|
|
_data[lower]!.remove(value.toString());
|
2021-03-18 23:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void removeAll(String name) {
|
|
|
|
_data.remove(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void set(String name, Object value, {bool preserveHeaderCase = false}) {
|
|
|
|
var lower = preserveHeaderCase ? name : name.toLowerCase();
|
|
|
|
_data.remove(lower);
|
|
|
|
|
|
|
|
if (value is Iterable) {
|
|
|
|
_data[lower] = value.map((x) => x.toString()).toList();
|
|
|
|
} else {
|
|
|
|
_data[lower] = [value.toString()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-20 02:33:34 +00:00
|
|
|
String? value(String name) => _data[name.toLowerCase()]?.join(',');
|
2021-03-18 23:10:19 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
var b = StringBuffer();
|
|
|
|
_data.forEach((k, v) {
|
|
|
|
b.write('$k: ');
|
|
|
|
b.write(v.join(','));
|
|
|
|
b.writeln();
|
|
|
|
});
|
|
|
|
return b.toString();
|
|
|
|
}
|
|
|
|
}
|