OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library shelf_static.alternative_root_test; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'package:scheduled_test/descriptor.dart' as d; |
| 9 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 |
| 11 import 'package:shelf_static/shelf_static.dart'; |
| 12 import 'test_util.dart'; |
| 13 |
| 14 void main() { |
| 15 setUp(() { |
| 16 var tempDir; |
| 17 schedule(() { |
| 18 return Directory.systemTemp.createTemp('shelf_static-test-').then((dir) { |
| 19 tempDir = dir; |
| 20 d.defaultRoot = tempDir.path; |
| 21 }); |
| 22 }); |
| 23 |
| 24 d.file('root.txt', 'root txt').create(); |
| 25 d |
| 26 .dir('files', [ |
| 27 d.file('test.txt', 'test txt content'), |
| 28 d.file('with space.txt', 'with space content') |
| 29 ]) |
| 30 .create(); |
| 31 |
| 32 currentSchedule.onComplete.schedule(() { |
| 33 d.defaultRoot = null; |
| 34 return tempDir.delete(recursive: true); |
| 35 }); |
| 36 }); |
| 37 |
| 38 test('access root file', () { |
| 39 schedule(() { |
| 40 var handler = createStaticHandler(d.defaultRoot); |
| 41 |
| 42 return makeRequest(handler, '/static/root.txt', handlerPath: 'static') |
| 43 .then((response) { |
| 44 expect(response.statusCode, HttpStatus.OK); |
| 45 expect(response.contentLength, 8); |
| 46 expect(response.readAsString(), completion('root txt')); |
| 47 }); |
| 48 }); |
| 49 }); |
| 50 |
| 51 test('access root file with space', () { |
| 52 schedule(() { |
| 53 var handler = createStaticHandler(d.defaultRoot); |
| 54 |
| 55 return makeRequest(handler, '/static/files/with%20space.txt', |
| 56 handlerPath: 'static').then((response) { |
| 57 expect(response.statusCode, HttpStatus.OK); |
| 58 expect(response.contentLength, 18); |
| 59 expect(response.readAsString(), completion('with space content')); |
| 60 }); |
| 61 }); |
| 62 }); |
| 63 |
| 64 test('access root file with unencoded space', () { |
| 65 schedule(() { |
| 66 var handler = createStaticHandler(d.defaultRoot); |
| 67 |
| 68 return makeRequest(handler, '/static/files/with%20space.txt', |
| 69 handlerPath: 'static').then((response) { |
| 70 expect(response.statusCode, HttpStatus.OK); |
| 71 expect(response.contentLength, 18); |
| 72 expect(response.readAsString(), completion('with space content')); |
| 73 }); |
| 74 }); |
| 75 }); |
| 76 |
| 77 test('access file under directory', () { |
| 78 schedule(() { |
| 79 var handler = createStaticHandler(d.defaultRoot); |
| 80 |
| 81 return makeRequest(handler, '/static/files/test.txt', |
| 82 handlerPath: 'static').then((response) { |
| 83 expect(response.statusCode, HttpStatus.OK); |
| 84 expect(response.contentLength, 16); |
| 85 expect(response.readAsString(), completion('test txt content')); |
| 86 }); |
| 87 }); |
| 88 }); |
| 89 |
| 90 test('file not found', () { |
| 91 schedule(() { |
| 92 var handler = createStaticHandler(d.defaultRoot); |
| 93 |
| 94 return makeRequest(handler, '/static/not_here.txt', handlerPath: 'static') |
| 95 .then((response) { |
| 96 expect(response.statusCode, HttpStatus.NOT_FOUND); |
| 97 }); |
| 98 }); |
| 99 }); |
| 100 } |
OLD | NEW |