platform/packages/range_header/test/all_test.dart

96 lines
2.7 KiB
Dart
Raw Normal View History

2021-05-18 11:58:51 +00:00
import 'package:angel3_range_header/angel3_range_header.dart';
2021-05-01 02:48:36 +00:00
import 'package:test/test.dart';
final Matcher throwsRangeParseException =
throwsA(const TypeMatcher<RangeHeaderParseException>());
2021-05-01 03:39:09 +00:00
final Matcher throwsInvalidRangeHeaderException =
throwsA(const TypeMatcher<InvalidRangeHeaderException>());
2021-05-15 11:58:09 +00:00
void main() {
2021-05-01 02:48:36 +00:00
group('one item', () {
test('start and end', () {
2021-05-01 03:39:09 +00:00
var r = RangeHeader.parse('bytes 1-200');
2021-05-01 02:48:36 +00:00
expect(r.items, hasLength(1));
expect(r.items.first.start, 1);
expect(r.items.first.end, 200);
});
test('start only', () {
2021-05-01 03:39:09 +00:00
var r = RangeHeader.parse('bytes 1-');
2021-05-01 02:48:36 +00:00
expect(r.items, hasLength(1));
expect(r.items.first.start, 1);
expect(r.items.first.end, -1);
});
test('end only', () {
2021-05-01 03:39:09 +00:00
var r = RangeHeader.parse('bytes -200');
2021-05-01 02:48:36 +00:00
print(r.items);
expect(r.items, hasLength(1));
expect(r.items.first.start, -1);
expect(r.items.first.end, 200);
});
});
group('multiple items', () {
test('three items', () {
2021-05-01 03:39:09 +00:00
var r = RangeHeader.parse('bytes 1-20, 21-40, 41-60');
2021-05-01 02:48:36 +00:00
print(r.items);
expect(r.items, hasLength(3));
expect(r.items[0].start, 1);
expect(r.items[0].end, 20);
expect(r.items[1].start, 21);
expect(r.items[1].end, 40);
expect(r.items[2].start, 41);
expect(r.items[2].end, 60);
});
test('one item without end', () {
2021-05-01 03:39:09 +00:00
var r = RangeHeader.parse('bytes 1-20, 21-');
2021-05-01 02:48:36 +00:00
print(r.items);
expect(r.items, hasLength(2));
expect(r.items[0].start, 1);
expect(r.items[0].end, 20);
expect(r.items[1].start, 21);
expect(r.items[1].end, -1);
});
});
group('failures', () {
test('no start with no end', () {
2021-05-01 03:39:09 +00:00
expect(() => RangeHeader.parse('-'), throwsInvalidRangeHeaderException);
2021-05-01 02:48:36 +00:00
});
});
group('exceptions', () {
test('invalid character', () {
2021-05-01 03:39:09 +00:00
expect(() => RangeHeader.parse('!!!'), throwsRangeParseException);
2021-05-01 02:48:36 +00:00
});
test('no ranges', () {
2021-05-01 03:39:09 +00:00
expect(() => RangeHeader.parse('bytes'), throwsRangeParseException);
2021-05-01 02:48:36 +00:00
});
test('no dash after int', () {
2021-05-01 03:39:09 +00:00
expect(() => RangeHeader.parse('bytes 3'), throwsRangeParseException);
expect(() => RangeHeader.parse('bytes 3,'), throwsRangeParseException);
expect(() => RangeHeader.parse('bytes 3 24'), throwsRangeParseException);
2021-05-01 02:48:36 +00:00
});
test('no int after dash', () {
2021-05-01 03:39:09 +00:00
expect(() => RangeHeader.parse('bytes -,'), throwsRangeParseException);
2021-05-01 02:48:36 +00:00
});
});
group('complete coverage', () {
test('exception toString()', () {
2021-05-01 03:39:09 +00:00
var m = RangeHeaderParseException('hey');
2021-05-01 02:48:36 +00:00
expect(m.toString(), contains('hey'));
});
});
test('content-range', () {
2021-05-01 03:39:09 +00:00
expect(RangeHeader.parse('bytes 1-2').items[0].toContentRange(3), '1-2/3');
2021-05-01 02:48:36 +00:00
});
}