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.basic_file_test; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'package:http_parser/http_parser.dart'; |
| 9 import 'package:path/path.dart' as p; |
| 10 import 'package:scheduled_test/descriptor.dart' as d; |
| 11 import 'package:scheduled_test/scheduled_test.dart'; |
| 12 |
| 13 import 'package:shelf_static/shelf_static.dart'; |
| 14 import 'test_util.dart'; |
| 15 |
| 16 void main() { |
| 17 setUp(() { |
| 18 var tempDir; |
| 19 schedule(() { |
| 20 return Directory.systemTemp.createTemp('shelf_static-test-').then((dir) { |
| 21 tempDir = dir; |
| 22 d.defaultRoot = tempDir.path; |
| 23 }); |
| 24 }); |
| 25 |
| 26 d.file('index.html', '<html></html>').create(); |
| 27 d.file('root.txt', 'root txt').create(); |
| 28 d.file('random.unknown', 'no clue').create(); |
| 29 d |
| 30 .dir('files', [ |
| 31 d.file('test.txt', 'test txt content'), |
| 32 d.file('with space.txt', 'with space content') |
| 33 ]) |
| 34 .create(); |
| 35 |
| 36 currentSchedule.onComplete.schedule(() { |
| 37 d.defaultRoot = null; |
| 38 return tempDir.delete(recursive: true); |
| 39 }); |
| 40 }); |
| 41 |
| 42 test('access root file', () { |
| 43 schedule(() { |
| 44 var handler = createStaticHandler(d.defaultRoot); |
| 45 |
| 46 return makeRequest(handler, '/root.txt').then((response) { |
| 47 expect(response.statusCode, HttpStatus.OK); |
| 48 expect(response.contentLength, 8); |
| 49 expect(response.readAsString(), completion('root txt')); |
| 50 }); |
| 51 }); |
| 52 }); |
| 53 |
| 54 test('access root file with space', () { |
| 55 schedule(() { |
| 56 var handler = createStaticHandler(d.defaultRoot); |
| 57 |
| 58 return makeRequest(handler, '/files/with%20space.txt').then((response) { |
| 59 expect(response.statusCode, HttpStatus.OK); |
| 60 expect(response.contentLength, 18); |
| 61 expect(response.readAsString(), completion('with space content')); |
| 62 }); |
| 63 }); |
| 64 }); |
| 65 |
| 66 test('access root file with unencoded space', () { |
| 67 schedule(() { |
| 68 var handler = createStaticHandler(d.defaultRoot); |
| 69 |
| 70 return makeRequest(handler, '/files/with%20space.txt').then((response) { |
| 71 expect(response.statusCode, HttpStatus.OK); |
| 72 expect(response.contentLength, 18); |
| 73 expect(response.readAsString(), completion('with space content')); |
| 74 }); |
| 75 }); |
| 76 }); |
| 77 |
| 78 test('access file under directory', () { |
| 79 schedule(() { |
| 80 var handler = createStaticHandler(d.defaultRoot); |
| 81 |
| 82 return makeRequest(handler, '/files/test.txt').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, '/not_here.txt').then((response) { |
| 95 expect(response.statusCode, HttpStatus.NOT_FOUND); |
| 96 }); |
| 97 }); |
| 98 }); |
| 99 |
| 100 test('last modified', () { |
| 101 schedule(() { |
| 102 var handler = createStaticHandler(d.defaultRoot); |
| 103 |
| 104 var rootPath = p.join(d.defaultRoot, 'root.txt'); |
| 105 var modified = new File(rootPath).statSync().changed.toUtc(); |
| 106 |
| 107 return makeRequest(handler, '/root.txt').then((response) { |
| 108 expect(response.lastModified, atSameTimeToSecond(modified)); |
| 109 }); |
| 110 }); |
| 111 }); |
| 112 |
| 113 group('if modified since', () { |
| 114 test('same as last modified', () { |
| 115 schedule(() { |
| 116 var handler = createStaticHandler(d.defaultRoot); |
| 117 |
| 118 var rootPath = p.join(d.defaultRoot, 'root.txt'); |
| 119 var modified = new File(rootPath).statSync().changed.toUtc(); |
| 120 |
| 121 var headers = {HttpHeaders.IF_MODIFIED_SINCE: formatHttpDate(modified)}; |
| 122 |
| 123 return makeRequest(handler, '/root.txt', headers: headers) |
| 124 .then((response) { |
| 125 expect(response.statusCode, HttpStatus.NOT_MODIFIED); |
| 126 expect(response.contentLength, isNull); |
| 127 }); |
| 128 }); |
| 129 }); |
| 130 |
| 131 test('before last modified', () { |
| 132 schedule(() { |
| 133 var handler = createStaticHandler(d.defaultRoot); |
| 134 |
| 135 var rootPath = p.join(d.defaultRoot, 'root.txt'); |
| 136 var modified = new File(rootPath).statSync().changed.toUtc(); |
| 137 |
| 138 var headers = { |
| 139 HttpHeaders.IF_MODIFIED_SINCE: |
| 140 formatHttpDate(modified.subtract(const Duration(seconds: 1))) |
| 141 }; |
| 142 |
| 143 return makeRequest(handler, '/root.txt', headers: headers) |
| 144 .then((response) { |
| 145 expect(response.statusCode, HttpStatus.OK); |
| 146 expect(response.lastModified, atSameTimeToSecond(modified)); |
| 147 }); |
| 148 }); |
| 149 }); |
| 150 |
| 151 test('after last modified', () { |
| 152 schedule(() { |
| 153 var handler = createStaticHandler(d.defaultRoot); |
| 154 |
| 155 var rootPath = p.join(d.defaultRoot, 'root.txt'); |
| 156 var modified = new File(rootPath).statSync().changed.toUtc(); |
| 157 |
| 158 var headers = { |
| 159 HttpHeaders.IF_MODIFIED_SINCE: |
| 160 formatHttpDate(modified.add(const Duration(seconds: 1))) |
| 161 }; |
| 162 |
| 163 return makeRequest(handler, '/root.txt', headers: headers) |
| 164 .then((response) { |
| 165 expect(response.statusCode, HttpStatus.NOT_MODIFIED); |
| 166 expect(response.contentLength, isNull); |
| 167 }); |
| 168 }); |
| 169 }); |
| 170 }); |
| 171 |
| 172 group('content type', () { |
| 173 test('root.txt should be text/plain', () { |
| 174 schedule(() { |
| 175 var handler = createStaticHandler(d.defaultRoot); |
| 176 |
| 177 return makeRequest(handler, '/root.txt').then((response) { |
| 178 expect(response.mimeType, 'text/plain'); |
| 179 }); |
| 180 }); |
| 181 }); |
| 182 |
| 183 test('index.html should be text/html', () { |
| 184 schedule(() { |
| 185 var handler = createStaticHandler(d.defaultRoot); |
| 186 |
| 187 return makeRequest(handler, '/index.html').then((response) { |
| 188 expect(response.mimeType, 'text/html'); |
| 189 }); |
| 190 }); |
| 191 }); |
| 192 |
| 193 test('random.unknown should be null', () { |
| 194 schedule(() { |
| 195 var handler = createStaticHandler(d.defaultRoot); |
| 196 |
| 197 return makeRequest(handler, '/random.unknown').then((response) { |
| 198 expect(response.mimeType, isNull); |
| 199 }); |
| 200 }); |
| 201 }); |
| 202 }); |
| 203 } |
OLD | NEW |