OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import "package:http_server/http_server.dart"; | 8 import "package:http_server/http_server.dart"; |
9 import 'package:path/path.dart' as pathos; | 9 import 'package:path/path.dart' as pathos; |
10 import "package:unittest/unittest.dart"; | 10 import "package:unittest/unittest.dart"; |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 | 518 |
519 return getHeaders(virDir, '/file.jpg') | 519 return getHeaders(virDir, '/file.jpg') |
520 .then((headers) { | 520 .then((headers) { |
521 var contentType = headers.contentType.toString(); | 521 var contentType = headers.contentType.toString(); |
522 expect(contentType, 'image/png'); | 522 expect(contentType, 'image/png'); |
523 }); | 523 }); |
524 }); | 524 }); |
525 }); | 525 }); |
526 }); | 526 }); |
527 | 527 |
528 solo_group('range', () { | |
kustermann
2014/11/14 13:25:27
Remove the solo_ here, otherwise we loose all othe
Søren Gjesse
2014/11/14 15:27:30
Done.
| |
529 testVirtualDir('range', (dir) { | |
530 var fileContent = [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9]; | |
531 new File('${dir.path}/file') | |
532 ..writeAsBytesSync(fileContent); | |
533 var virDir = new VirtualDirectory(dir.path); | |
534 | |
535 Future test( | |
536 int from, int to, [List<int> expected, String contentRange]) { | |
537 if (expected == null) { | |
538 expected = fileContent.sublist(from, to + 1); | |
539 } | |
540 if (contentRange == null) { | |
541 contentRange = 'bytes $from-$to/${fileContent.length}'; | |
542 } | |
543 return getAsBytes(virDir, '/file', from: from, to: to) | |
544 .then(expectAsync((result) { | |
545 expect(result, expected); | |
546 return getHeaders(virDir, '/file', from: from, to: to) | |
547 .then(expectAsync((headers) { | |
548 expect(headers[HttpHeaders.CONTENT_RANGE][0], | |
549 contentRange); | |
550 return getStatusCodeForVirtDir( | |
551 virDir, '/file', from: from, to: to) | |
552 .then(expectAsync((statusCode) { | |
553 expect(statusCode, HttpStatus.PARTIAL_CONTENT); | |
554 })); | |
555 })); | |
556 })); | |
557 } | |
558 | |
559 return Future.forEach([ | |
560 () => test(0, 0), | |
561 () => test(0, 1), | |
562 () => test(1, 2), | |
563 () => test(1, 9), | |
564 () => test(0, 9), | |
565 () => test(8, 9), | |
566 () => test(9, 9), | |
kustermann
2014/11/14 13:25:27
Add a case for 10 here, for the off-by one issue.
Søren Gjesse
2014/11/14 15:27:30
Done.
| |
567 () => test(0, 1000, fileContent, 'bytes 0-9/10'), | |
568 ], (f) => f().then(expectAsync((_) {}))); | |
569 }); | |
570 | |
571 testVirtualDir('suffix-range', (dir) { | |
kustermann
2014/11/14 13:25:27
There is a test missing for 'prefix-range', right?
Søren Gjesse
2014/11/14 15:27:30
Yes, added.
| |
572 var fileContent = [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9]; | |
573 new File('${dir.path}/file') | |
574 ..writeAsBytesSync(fileContent); | |
kustermann
2014/11/14 13:25:27
No actual need for cascasde (same above and below)
Søren Gjesse
2014/11/14 15:27:30
Done.
| |
575 var virDir = new VirtualDirectory(dir.path); | |
576 | |
577 Future test(int to, [List<int> expected, String contentRange]) { | |
578 if (expected == null) { | |
579 expected = fileContent.sublist(fileContent.length - to, | |
580 fileContent.length); | |
581 } | |
582 if (contentRange == null) { | |
583 contentRange = 'bytes ${fileContent.length - to}-' | |
584 '${fileContent.length - 1}/' | |
585 '${fileContent.length}'; | |
586 } | |
587 return getAsBytes(virDir, '/file', to: to) | |
588 .then(expectAsync((result) { | |
589 expect(result, expected); | |
590 return getHeaders(virDir, '/file', to: to) | |
591 .then(expectAsync((headers) { | |
592 expect(headers[HttpHeaders.CONTENT_RANGE][0], | |
593 contentRange); | |
594 return getStatusCodeForVirtDir( | |
595 virDir, '/file', to: to) | |
596 .then(expectAsync((statusCode) { | |
597 expect(statusCode, HttpStatus.PARTIAL_CONTENT); | |
598 })); | |
599 })); | |
600 })); | |
601 } | |
602 | |
603 return Future.forEach([ | |
604 () => test(1), | |
605 () => test(2), | |
606 () => test(9), | |
607 () => test(10), | |
608 () => test(11, fileContent, 'bytes 0-9/10'), | |
609 () => test(1000, fileContent, 'bytes 0-9/10') | |
610 ], (f) => f().then(expectAsync((_) {}))); | |
611 }); | |
612 | |
613 testVirtualDir('unsatisfiable-range', (dir) { | |
614 var fileContent = [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9]; | |
615 new File('${dir.path}/file') | |
616 ..writeAsBytesSync(fileContent); | |
617 var virDir = new VirtualDirectory(dir.path); | |
618 | |
619 Future test(int from, int to) { | |
620 return getAsBytes(virDir, '/file', from: from, to: to) | |
621 .then(expectAsync((result) { | |
622 expect(result.length, 0); | |
623 return getHeaders(virDir, '/file', from: from, to: to) | |
624 .then(expectAsync((headers) { | |
625 expect(headers[HttpHeaders.CONTENT_RANGE], isNull); | |
626 return getStatusCodeForVirtDir( | |
627 virDir, '/file', from: from, to: to) | |
628 .then(expectAsync((statusCode) { | |
629 var expectedStatusCode = | |
630 HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE; | |
631 expect(statusCode, expectedStatusCode); | |
632 })); | |
633 })); | |
634 })); | |
635 } | |
636 | |
637 return Future.forEach([ | |
638 () => test(10, 11), | |
639 () => test(10, 1000), | |
640 () => test(1000, 1000) | |
641 ], (f) => f().then(expectAsync((_) {}))); | |
642 }); | |
643 | |
644 testVirtualDir('invalid-range', (dir) { | |
645 var fileContent = [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9]; | |
646 new File('${dir.path}/file') | |
647 ..writeAsBytesSync(fileContent); | |
648 var virDir = new VirtualDirectory(dir.path); | |
kustermann
2014/11/14 13:25:27
Maybe just move these repeated lines into a setUp(
Søren Gjesse
2014/11/14 15:27:30
Added a prepare method. Cannot use unittest setUp
| |
649 | |
650 Future test(int from, int to) { | |
651 return getAsBytes(virDir, '/file', from: from, to: to) | |
652 .then(expectAsync((result) { | |
653 expect(result, fileContent); | |
654 return getHeaders(virDir, '/file', from: from, to: to) | |
655 .then(expectAsync((headers) { | |
656 expect(headers[HttpHeaders.CONTENT_RANGE], isNull); | |
657 return getStatusCodeForVirtDir( | |
658 virDir, '/file', from: from, to: to) | |
659 .then(expectAsync((statusCode) { | |
660 expect(statusCode, HttpStatus.OK); | |
661 })); | |
662 })); | |
663 })); | |
664 } | |
kustermann
2014/11/14 13:25:27
Maybe extract these redundant lines for getting th
Søren Gjesse
2014/11/14 15:27:30
Done.
| |
665 | |
666 return Future.forEach([ | |
667 () => test(1, 0), | |
668 () => test(10, 0), | |
669 () => test(1000, 999), | |
670 () => test(null, 0), // This is effectively range 10-9. | |
671 ], (f) => f().then(expectAsync((_) {}))); | |
672 }); | |
673 }); | |
674 | |
528 group('error-page', () { | 675 group('error-page', () { |
529 testVirtualDir('default', (dir) { | 676 testVirtualDir('default', (dir) { |
530 var virDir = new VirtualDirectory(pathos.join(dir.path, 'foo')); | 677 var virDir = new VirtualDirectory(pathos.join(dir.path, 'foo')); |
531 | 678 |
532 return getAsString(virDir, '/') | 679 return getAsString(virDir, '/') |
533 .then((result) { | 680 .then((result) { |
534 expect(result, matches(new RegExp('404.*Not Found'))); | 681 expect(result, matches(new RegExp('404.*Not Found'))); |
535 }); | 682 }); |
536 }); | 683 }); |
537 | 684 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 return virDir.serveFile(new File('${d.path}/file'), request); | 783 return virDir.serveFile(new File('${d.path}/file'), request); |
637 }; | 784 }; |
638 | 785 |
639 return getAsString(virDir, '/') | 786 return getAsString(virDir, '/') |
640 .then((result) { | 787 .then((result) { |
641 expect(result, 'file contents'); | 788 expect(result, 'file contents'); |
642 }); | 789 }); |
643 }); | 790 }); |
644 }); | 791 }); |
645 } | 792 } |
OLD | NEW |