OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 import "package:unittest/unittest.dart"; |
| 8 import "package:http_server/http_server.dart"; |
| 9 |
| 10 |
| 11 void main() { |
| 12 group('serve-root', () { |
| 13 test('dir-exists', () { |
| 14 expect(HttpServer.bind('localhost', 0).then((server) { |
| 15 var dir = new Directory('').createTempSync(); |
| 16 var virDir = new VirtualDirectory(dir.path); |
| 17 |
| 18 virDir.serve(server); |
| 19 |
| 20 return new HttpClient().get('localhost', server.port, '/') |
| 21 .then((request) => request.close()) |
| 22 .then((response) => response.statusCode) |
| 23 .whenComplete(() { |
| 24 server.close(); |
| 25 dir.deleteSync(); |
| 26 }); |
| 27 }), completion(equals(HttpStatus.NOT_FOUND))); |
| 28 }); |
| 29 |
| 30 test('dir-not-exists', () { |
| 31 expect(HttpServer.bind('localhost', 0).then((server) { |
| 32 var dir = new Directory('').createTempSync(); |
| 33 dir.deleteSync(); |
| 34 var virDir = new VirtualDirectory(dir.path); |
| 35 |
| 36 virDir.serve(server); |
| 37 |
| 38 return new HttpClient().get('localhost', server.port, '/') |
| 39 .then((request) => request.close()) |
| 40 .then((response) => response.statusCode) |
| 41 .whenComplete(() { |
| 42 server.close(); |
| 43 }); |
| 44 }), completion(equals(HttpStatus.NOT_FOUND))); |
| 45 }); |
| 46 }); |
| 47 |
| 48 group('serve-file', () { |
| 49 group('top-level', () { |
| 50 test('file-exists', () { |
| 51 expect(HttpServer.bind('localhost', 0).then((server) { |
| 52 var dir = new Directory('').createTempSync(); |
| 53 var file = new File('${dir.path}/file')..createSync(); |
| 54 var virDir = new VirtualDirectory(dir.path); |
| 55 |
| 56 virDir.serve(server); |
| 57 |
| 58 return new HttpClient().get('localhost', server.port, '/file') |
| 59 .then((request) => request.close()) |
| 60 .then((response) => response.statusCode) |
| 61 .whenComplete(() { |
| 62 server.close(); |
| 63 file.deleteSync(); |
| 64 dir.deleteSync(); |
| 65 }); |
| 66 }), completion(equals(HttpStatus.OK))); |
| 67 }); |
| 68 |
| 69 test('file-not-exists', () { |
| 70 expect(HttpServer.bind('localhost', 0).then((server) { |
| 71 var dir = new Directory('').createTempSync(); |
| 72 var virDir = new VirtualDirectory(dir.path); |
| 73 |
| 74 virDir.serve(server); |
| 75 |
| 76 return new HttpClient().get('localhost', server.port, '/file') |
| 77 .then((request) => request.close()) |
| 78 .then((response) => response.statusCode) |
| 79 .whenComplete(() { |
| 80 server.close(); |
| 81 dir.deleteSync(); |
| 82 }); |
| 83 }), completion(equals(HttpStatus.NOT_FOUND))); |
| 84 }); |
| 85 }); |
| 86 |
| 87 group('in-dir', () { |
| 88 test('file-exists', () { |
| 89 expect(HttpServer.bind('localhost', 0).then((server) { |
| 90 var dir = new Directory('').createTempSync(); |
| 91 var dir2 = new Directory('${dir.path}/dir')..createSync(); |
| 92 var file = new File('${dir2.path}/file')..createSync(); |
| 93 var virDir = new VirtualDirectory(dir.path); |
| 94 |
| 95 virDir.serve(server); |
| 96 |
| 97 return new HttpClient().get('localhost', server.port, '/dir/file') |
| 98 .then((request) => request.close()) |
| 99 .then((response) => response.statusCode) |
| 100 .whenComplete(() { |
| 101 server.close(); |
| 102 file.deleteSync(); |
| 103 dir2.deleteSync(); |
| 104 dir.deleteSync(); |
| 105 }); |
| 106 }), completion(equals(HttpStatus.OK))); |
| 107 }); |
| 108 |
| 109 test('file-not-exists', () { |
| 110 expect(HttpServer.bind('localhost', 0).then((server) { |
| 111 var dir = new Directory('').createTempSync(); |
| 112 var dir2 = new Directory('${dir.path}/dir')..createSync(); |
| 113 var file = new File('${dir.path}/file')..createSync(); |
| 114 var virDir = new VirtualDirectory(dir.path); |
| 115 |
| 116 virDir.serve(server); |
| 117 |
| 118 return new HttpClient().get('localhost', server.port, '/dir/file') |
| 119 .then((request) => request.close()) |
| 120 .then((response) => response.statusCode) |
| 121 .whenComplete(() { |
| 122 server.close(); |
| 123 file.deleteSync(); |
| 124 dir2.deleteSync(); |
| 125 dir.deleteSync(); |
| 126 }); |
| 127 }), completion(equals(HttpStatus.NOT_FOUND))); |
| 128 }); |
| 129 }); |
| 130 }); |
| 131 } |
| 132 |
OLD | NEW |