OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 library unittest.one_off_handler; | 5 library test.one_off_handler; |
6 | 6 |
7 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
8 import 'package:shelf/shelf.dart' as shelf; | 8 import 'package:shelf/shelf.dart' as shelf; |
9 | 9 |
10 /// A Shelf handler that provides support for one-time handlers. | 10 /// A Shelf handler that provides support for one-time handlers. |
11 /// | 11 /// |
12 /// This is useful for handlers that only expect to be hit once before becoming | 12 /// This is useful for handlers that only expect to be hit once before becoming |
13 /// invalid and don't need to have a persistent URL. | 13 /// invalid and don't need to have a persistent URL. |
14 class OneOffHandler { | 14 class OneOffHandler { |
15 /// A map from URL paths to handlers. | 15 /// A map from URL paths to handlers. |
(...skipping 22 matching lines...) Expand all Loading... |
38 _onRequest(shelf.Request request) { | 38 _onRequest(shelf.Request request) { |
39 // Skip the first component because it's always "/". | 39 // Skip the first component because it's always "/". |
40 var components = p.url.split(request.url.path).skip(1).toList(); | 40 var components = p.url.split(request.url.path).skip(1).toList(); |
41 if (components.isEmpty) return new shelf.Response.notFound(null); | 41 if (components.isEmpty) return new shelf.Response.notFound(null); |
42 | 42 |
43 var handler = _handlers.remove(components.removeAt(0)); | 43 var handler = _handlers.remove(components.removeAt(0)); |
44 if (handler == null) return new shelf.Response.notFound(null); | 44 if (handler == null) return new shelf.Response.notFound(null); |
45 return handler(request); | 45 return handler(request); |
46 } | 46 } |
47 } | 47 } |
OLD | NEW |