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 group('range', () { | |
529 var fileContent = [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9]; | |
530 var virDir; | |
531 | |
532 prepare(dir) { | |
533 new File('${dir.path}/file').writeAsBytesSync(fileContent); | |
534 virDir = new VirtualDirectory(dir.path); | |
535 } | |
kustermann
2014/11/17 11:15:30
Are we leaking these files/directories or will the
Søren Gjesse
2014/11/17 13:13:23
The 'testVirtualDir' used below does the cleanup.
| |
536 | |
537 testVirtualDir('range', (dir) { | |
538 prepare(dir); | |
539 Future test( | |
540 int from, int to, [List<int> expected, String contentRange]) { | |
541 if (expected == null) { | |
542 expected = fileContent.sublist(from, to + 1); | |
543 } | |
544 if (contentRange == null) { | |
545 contentRange = 'bytes $from-$to/${fileContent.length}'; | |
546 } | |
547 return getContentAndResponse(virDir, '/file', from: from, to: to) | |
548 .then(expectAsync((result) { | |
549 var content = result[0]; | |
550 var response = result[1]; | |
551 expect(content, expected); | |
552 expect(response.headers[HttpHeaders.CONTENT_RANGE][0], | |
553 contentRange); | |
554 expect(expected.length, response.headers.contentLength); | |
555 expect(response.statusCode, HttpStatus.PARTIAL_CONTENT); | |
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), | |
567 () => test(0, 10, fileContent, 'bytes 0-9/10'), | |
568 () => test(9, 10, [9], 'bytes 9-9/10'), | |
569 () => test(0, 1000, fileContent, 'bytes 0-9/10'), | |
570 ], (f) => f().then(expectAsync((_) {}))); | |
571 }); | |
572 | |
573 testVirtualDir('prefix-range', (dir) { | |
574 prepare(dir); | |
575 Future test(int from, | |
576 [List<int> expected, | |
577 String contentRange, | |
578 bool expectContentRange = true, | |
579 int expectedStatusCode = HttpStatus.PARTIAL_CONTENT]) { | |
580 if (expected == null) { | |
581 expected = fileContent.sublist(from, fileContent.length); | |
582 } | |
583 if (contentRange == null && expectContentRange) { | |
584 contentRange = 'bytes ${from}-' | |
585 '${fileContent.length - 1}/' | |
586 '${fileContent.length}'; | |
587 } | |
588 return getContentAndResponse(virDir, '/file', from: from) | |
589 .then(expectAsync((result) { | |
590 var content = result[0]; | |
591 var response = result[1]; | |
592 expect(content, expected); | |
593 if (expectContentRange) { | |
594 expect(response.headers[HttpHeaders.CONTENT_RANGE][0], | |
595 contentRange); | |
596 } else { | |
597 expect(response.headers[HttpHeaders.CONTENT_RANGE], null); | |
598 } | |
599 expect(response.statusCode, expectedStatusCode); | |
600 })); | |
601 } | |
602 | |
603 return Future.forEach([ | |
604 () => test(0), | |
605 () => test(1), | |
606 () => test(9), | |
607 () => test(10, fileContent, null, false, HttpStatus.OK), | |
608 () => test(11, fileContent, null, false, HttpStatus.OK), | |
609 () => test(1000, fileContent, null, false, HttpStatus.OK), | |
610 ], (f) => f().then(expectAsync((_) {}))); | |
611 }); | |
612 | |
613 testVirtualDir('suffix-range', (dir) { | |
614 prepare(dir); | |
615 Future test(int to, [List<int> expected, String contentRange]) { | |
616 if (expected == null) { | |
617 expected = fileContent.sublist(fileContent.length - to, | |
618 fileContent.length); | |
619 } | |
620 if (contentRange == null) { | |
621 contentRange = 'bytes ${fileContent.length - to}-' | |
622 '${fileContent.length - 1}/' | |
623 '${fileContent.length}'; | |
624 } | |
625 return getContentAndResponse(virDir, '/file', to: to) | |
626 .then(expectAsync((result) { | |
627 var content = result[0]; | |
628 var response = result[1]; | |
629 expect(content, expected); | |
630 expect(response.headers[HttpHeaders.CONTENT_RANGE][0], | |
631 contentRange); | |
632 expect(response.statusCode, HttpStatus.PARTIAL_CONTENT); | |
633 })); | |
634 } | |
635 | |
636 return Future.forEach([ | |
637 () => test(1), | |
638 () => test(2), | |
639 () => test(9), | |
640 () => test(10), | |
641 () => test(11, fileContent, 'bytes 0-9/10'), | |
642 () => test(1000, fileContent, 'bytes 0-9/10') | |
643 ], (f) => f().then(expectAsync((_) {}))); | |
644 }); | |
645 | |
646 testVirtualDir('unsatisfiable-range', (dir) { | |
647 prepare(dir); | |
648 Future test(int from, int to) { | |
649 return getContentAndResponse(virDir, '/file', from: from, to: to) | |
650 .then(expectAsync((result) { | |
651 var content = result[0]; | |
652 var response = result[1]; | |
653 expect(content.length, 0); | |
654 expect(response.headers[HttpHeaders.CONTENT_RANGE], isNull); | |
655 expect(response.statusCode, | |
656 HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE); | |
657 })); | |
658 } | |
659 | |
660 return Future.forEach([ | |
661 () => test(10, 11), | |
662 () => test(10, 1000), | |
663 () => test(1000, 1000) | |
664 ], (f) => f().then(expectAsync((_) {}))); | |
665 }); | |
666 | |
667 testVirtualDir('invalid-range', (dir) { | |
668 prepare(dir); | |
669 Future test(int from, int to) { | |
670 return getContentAndResponse(virDir, '/file', from: from, to: to) | |
671 .then(expectAsync((result) { | |
672 var content = result[0]; | |
673 var response = result[1]; | |
674 expect(content, fileContent); | |
675 expect(response.headers[HttpHeaders.CONTENT_RANGE], isNull); | |
676 expect(response.statusCode, HttpStatus.OK); | |
677 })); | |
678 } | |
679 | |
680 return Future.forEach([ | |
681 () => test(1, 0), | |
682 () => test(10, 0), | |
683 () => test(1000, 999), | |
684 () => test(null, 0), // This is effectively range 10-9. | |
685 ], (f) => f().then(expectAsync((_) {}))); | |
686 }); | |
687 }); | |
688 | |
528 group('error-page', () { | 689 group('error-page', () { |
529 testVirtualDir('default', (dir) { | 690 testVirtualDir('default', (dir) { |
530 var virDir = new VirtualDirectory(pathos.join(dir.path, 'foo')); | 691 var virDir = new VirtualDirectory(pathos.join(dir.path, 'foo')); |
531 | 692 |
532 return getAsString(virDir, '/') | 693 return getAsString(virDir, '/') |
533 .then((result) { | 694 .then((result) { |
534 expect(result, matches(new RegExp('404.*Not Found'))); | 695 expect(result, matches(new RegExp('404.*Not Found'))); |
535 }); | 696 }); |
536 }); | 697 }); |
537 | 698 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
630 testVirtualDir('from-dir-handler', (dir) { | 791 testVirtualDir('from-dir-handler', (dir) { |
631 new File('${dir.path}/file')..writeAsStringSync('file contents'); | 792 new File('${dir.path}/file')..writeAsStringSync('file contents'); |
632 var virDir = new VirtualDirectory(dir.path); | 793 var virDir = new VirtualDirectory(dir.path); |
633 virDir.allowDirectoryListing = true; | 794 virDir.allowDirectoryListing = true; |
634 virDir.directoryHandler = (d, request) { | 795 virDir.directoryHandler = (d, request) { |
635 expect(FileSystemEntity.identicalSync(dir.path, d.path), isTrue); | 796 expect(FileSystemEntity.identicalSync(dir.path, d.path), isTrue); |
636 return virDir.serveFile(new File('${d.path}/file'), request); | 797 return virDir.serveFile(new File('${d.path}/file'), request); |
637 }; | 798 }; |
638 | 799 |
639 return getAsString(virDir, '/') | 800 return getAsString(virDir, '/') |
640 .then((result) { | 801 .then((result) { |
641 expect(result, 'file contents'); | 802 expect(result, 'file contents'); |
803 return getHeaders(virDir, '/') | |
804 .then(expectAsync((headers) { | |
805 expect('file contents'.length, headers.contentLength); | |
806 })); | |
642 }); | 807 }); |
643 }); | 808 }); |
644 }); | 809 }); |
645 } | 810 } |
OLD | NEW |