OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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.request_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:shelf/shelf.dart'; |
| 11 import 'package:test/test.dart'; |
| 12 |
| 13 import 'test_util.dart'; |
| 14 |
| 15 Request _request({Map<String, String> headers, body, Encoding encoding}) { |
| 16 return new Request("GET", LOCALHOST_URI, |
| 17 headers: headers, body: body, encoding: encoding); |
| 18 } |
| 19 |
| 20 void main() { |
| 21 group('constructor', () { |
| 22 test('protocolVersion defaults to "1.1"', () { |
| 23 var request = new Request('GET', LOCALHOST_URI); |
| 24 expect(request.protocolVersion, '1.1'); |
| 25 }); |
| 26 |
| 27 test('provide non-default protocolVersion', () { |
| 28 var request = new Request('GET', LOCALHOST_URI, protocolVersion: '1.0'); |
| 29 expect(request.protocolVersion, '1.0'); |
| 30 }); |
| 31 |
| 32 group("url", () { |
| 33 test("defaults to the requestedUri's relativized path and query", () { |
| 34 var request = |
| 35 new Request('GET', Uri.parse("http://localhost/foo/bar?q=1")); |
| 36 expect(request.url, equals(Uri.parse("foo/bar?q=1"))); |
| 37 }); |
| 38 |
| 39 test("is inferred from handlerPath if possible", () { |
| 40 var request = new Request( |
| 41 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 42 handlerPath: '/foo/'); |
| 43 expect(request.url, equals(Uri.parse("bar?q=1"))); |
| 44 }); |
| 45 |
| 46 test("uses the given value if passed", () { |
| 47 var request = new Request( |
| 48 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 49 url: Uri.parse("bar?q=1")); |
| 50 expect(request.url, equals(Uri.parse("bar?q=1"))); |
| 51 }); |
| 52 |
| 53 test("may be empty", () { |
| 54 var request = new Request( |
| 55 'GET', Uri.parse("http://localhost/foo/bar"), |
| 56 url: Uri.parse("")); |
| 57 expect(request.url, equals(Uri.parse(""))); |
| 58 }); |
| 59 }); |
| 60 |
| 61 group("handlerPath", () { |
| 62 test("defaults to '/'", () { |
| 63 var request = new Request('GET', Uri.parse("http://localhost/foo/bar")); |
| 64 expect(request.handlerPath, equals('/')); |
| 65 }); |
| 66 |
| 67 test("is inferred from url if possible", () { |
| 68 var request = new Request( |
| 69 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 70 url: Uri.parse("bar?q=1")); |
| 71 expect(request.handlerPath, equals("/foo/")); |
| 72 }); |
| 73 |
| 74 test("uses the given value if passed", () { |
| 75 var request = new Request( |
| 76 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 77 handlerPath: '/foo/'); |
| 78 expect(request.handlerPath, equals("/foo/")); |
| 79 }); |
| 80 |
| 81 test("adds a trailing slash to the given value if necessary", () { |
| 82 var request = new Request( |
| 83 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 84 handlerPath: '/foo'); |
| 85 expect(request.handlerPath, equals("/foo/")); |
| 86 expect(request.url, equals(Uri.parse("bar?q=1"))); |
| 87 }); |
| 88 |
| 89 test("may be a single slash", () { |
| 90 var request = new Request( |
| 91 'GET', Uri.parse("http://localhost/foo/bar?q=1"), |
| 92 handlerPath: '/'); |
| 93 expect(request.handlerPath, equals("/")); |
| 94 expect(request.url, equals(Uri.parse("foo/bar?q=1"))); |
| 95 }); |
| 96 }); |
| 97 |
| 98 group("errors", () { |
| 99 group('requestedUri', () { |
| 100 test('must be absolute', () { |
| 101 expect(() => new Request('GET', Uri.parse('/path')), |
| 102 throwsArgumentError); |
| 103 }); |
| 104 |
| 105 test('may not have a fragment', () { |
| 106 expect(() { |
| 107 new Request('GET', Uri.parse('http://localhost/#fragment')); |
| 108 }, throwsArgumentError); |
| 109 }); |
| 110 }); |
| 111 |
| 112 group('url', () { |
| 113 test('must be relative', () { |
| 114 expect(() { |
| 115 new Request('GET', Uri.parse('http://localhost/test'), |
| 116 url: Uri.parse('http://localhost/test')); |
| 117 }, throwsArgumentError); |
| 118 }); |
| 119 |
| 120 test('may not be root-relative', () { |
| 121 expect(() { |
| 122 new Request('GET', Uri.parse('http://localhost/test'), |
| 123 url: Uri.parse('/test')); |
| 124 }, throwsArgumentError); |
| 125 }); |
| 126 |
| 127 test('may not have a fragment', () { |
| 128 expect(() { |
| 129 new Request('GET', Uri.parse('http://localhost/test'), |
| 130 url: Uri.parse('test#fragment')); |
| 131 }, throwsArgumentError); |
| 132 }); |
| 133 |
| 134 test('must be a suffix of requestedUri', () { |
| 135 expect(() { |
| 136 new Request('GET', Uri.parse('http://localhost/dir/test'), |
| 137 url: Uri.parse('dir')); |
| 138 }, throwsArgumentError); |
| 139 }); |
| 140 |
| 141 test('must have the same query parameters as requestedUri', () { |
| 142 expect(() { |
| 143 new Request('GET', Uri.parse('http://localhost/test?q=1&r=2'), |
| 144 url: Uri.parse('test?q=2&r=1')); |
| 145 }, throwsArgumentError); |
| 146 |
| 147 // Order matters for query parameters. |
| 148 expect(() { |
| 149 new Request('GET', Uri.parse('http://localhost/test?q=1&r=2'), |
| 150 url: Uri.parse('test?r=2&q=1')); |
| 151 }, throwsArgumentError); |
| 152 }); |
| 153 }); |
| 154 |
| 155 group('handlerPath', () { |
| 156 test('must be a prefix of requestedUri', () { |
| 157 expect(() { |
| 158 new Request('GET', Uri.parse('http://localhost/dir/test'), |
| 159 handlerPath: '/test'); |
| 160 }, throwsArgumentError); |
| 161 }); |
| 162 |
| 163 test('must start with "/"', () { |
| 164 expect(() { |
| 165 new Request('GET', Uri.parse('http://localhost/test'), |
| 166 handlerPath: 'test'); |
| 167 }, throwsArgumentError); |
| 168 }); |
| 169 |
| 170 test('must be the requestedUri path if url is empty', () { |
| 171 expect(() { |
| 172 new Request('GET', Uri.parse('http://localhost/test'), |
| 173 handlerPath: '/', |
| 174 url: Uri.parse('')); |
| 175 }, throwsArgumentError); |
| 176 }); |
| 177 }); |
| 178 |
| 179 group('handlerPath + url must', () { |
| 180 test('be requestedUrl path', () { |
| 181 expect(() { |
| 182 new Request('GET', Uri.parse('http://localhost/foo/bar/baz'), |
| 183 handlerPath: '/foo/', url: Uri.parse('baz')); |
| 184 }, throwsArgumentError); |
| 185 }); |
| 186 |
| 187 test('be on a path boundary', () { |
| 188 expect(() { |
| 189 new Request('GET', Uri.parse('http://localhost/foo/bar/baz'), |
| 190 handlerPath: '/foo/ba', url: Uri.parse('r/baz')); |
| 191 }, throwsArgumentError); |
| 192 }); |
| 193 }); |
| 194 }); |
| 195 }); |
| 196 |
| 197 group("ifModifiedSince", () { |
| 198 test("is null without an If-Modified-Since header", () { |
| 199 var request = _request(); |
| 200 expect(request.ifModifiedSince, isNull); |
| 201 }); |
| 202 |
| 203 test("comes from the Last-Modified header", () { |
| 204 var request = _request( |
| 205 headers: {'if-modified-since': 'Sun, 06 Nov 1994 08:49:37 GMT'}); |
| 206 expect(request.ifModifiedSince, |
| 207 equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 208 }); |
| 209 }); |
| 210 |
| 211 group('change', () { |
| 212 test('with no arguments returns instance with equal values', () { |
| 213 var controller = new StreamController(); |
| 214 |
| 215 var uri = Uri.parse('https://test.example.com/static/file.html'); |
| 216 |
| 217 var request = new Request('GET', uri, |
| 218 protocolVersion: '2.0', |
| 219 headers: {'header1': 'header value 1'}, |
| 220 url: Uri.parse('file.html'), |
| 221 handlerPath: '/static/', |
| 222 body: controller.stream, |
| 223 context: {'context1': 'context value 1'}); |
| 224 |
| 225 var copy = request.change(); |
| 226 |
| 227 expect(copy.method, request.method); |
| 228 expect(copy.requestedUri, request.requestedUri); |
| 229 expect(copy.protocolVersion, request.protocolVersion); |
| 230 expect(copy.headers, same(request.headers)); |
| 231 expect(copy.url, request.url); |
| 232 expect(copy.handlerPath, request.handlerPath); |
| 233 expect(copy.context, same(request.context)); |
| 234 expect(copy.readAsString(), completion('hello, world')); |
| 235 |
| 236 controller.add(HELLO_BYTES); |
| 237 return new Future(() { |
| 238 controller |
| 239 ..add(WORLD_BYTES) |
| 240 ..close(); |
| 241 }); |
| 242 }); |
| 243 |
| 244 group('with path', () { |
| 245 test('updates handlerPath and url', () { |
| 246 var uri = Uri.parse('https://test.example.com/static/dir/file.html'); |
| 247 var request = new Request('GET', uri, |
| 248 handlerPath: '/static/', url: Uri.parse('dir/file.html')); |
| 249 var copy = request.change(path: 'dir'); |
| 250 |
| 251 expect(copy.handlerPath, '/static/dir/'); |
| 252 expect(copy.url, Uri.parse('file.html')); |
| 253 }); |
| 254 |
| 255 test('allows a trailing slash', () { |
| 256 var uri = Uri.parse('https://test.example.com/static/dir/file.html'); |
| 257 var request = new Request('GET', uri, |
| 258 handlerPath: '/static/', url: Uri.parse('dir/file.html')); |
| 259 var copy = request.change(path: 'dir/'); |
| 260 |
| 261 expect(copy.handlerPath, '/static/dir/'); |
| 262 expect(copy.url, Uri.parse('file.html')); |
| 263 }); |
| 264 |
| 265 test('throws if path does not match existing uri', () { |
| 266 var uri = Uri.parse('https://test.example.com/static/dir/file.html'); |
| 267 var request = new Request('GET', uri, |
| 268 handlerPath: '/static/', url: Uri.parse('dir/file.html')); |
| 269 |
| 270 expect(() => request.change(path: 'wrong'), throwsArgumentError); |
| 271 }); |
| 272 |
| 273 test("throws if path isn't a path boundary", () { |
| 274 var uri = Uri.parse('https://test.example.com/static/dir/file.html'); |
| 275 var request = new Request('GET', uri, |
| 276 handlerPath: '/static/', url: Uri.parse('dir/file.html')); |
| 277 |
| 278 expect(() => request.change(path: 'di'), throwsArgumentError); |
| 279 }); |
| 280 }); |
| 281 |
| 282 test("allows the original request to be read", () { |
| 283 var request = _request(); |
| 284 var changed = request.change(); |
| 285 |
| 286 expect(request.read().toList(), completion(isEmpty)); |
| 287 expect(changed.read, throwsStateError); |
| 288 }); |
| 289 |
| 290 test("allows the changed request to be read", () { |
| 291 var request = _request(); |
| 292 var changed = request.change(); |
| 293 |
| 294 expect(changed.read().toList(), completion(isEmpty)); |
| 295 expect(request.read, throwsStateError); |
| 296 }); |
| 297 |
| 298 test("allows another changed request to be read", () { |
| 299 var request = _request(); |
| 300 var changed1 = request.change(); |
| 301 var changed2 = request.change(); |
| 302 |
| 303 expect(changed2.read().toList(), completion(isEmpty)); |
| 304 expect(changed1.read, throwsStateError); |
| 305 expect(request.read, throwsStateError); |
| 306 }); |
| 307 }); |
| 308 } |
OLD | NEW |