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 test.util.one_off_handler; | |
6 | |
7 import 'package:path/path.dart' as p; | 5 import 'package:path/path.dart' as p; |
8 import 'package:shelf/shelf.dart' as shelf; | 6 import 'package:shelf/shelf.dart' as shelf; |
9 | 7 |
10 /// A Shelf handler that provides support for one-time handlers. | 8 /// A Shelf handler that provides support for one-time handlers. |
11 /// | 9 /// |
12 /// This is useful for handlers that only expect to be hit once before becoming | 10 /// 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. | 11 /// invalid and don't need to have a persistent URL. |
14 class OneOffHandler { | 12 class OneOffHandler { |
15 /// A map from URL paths to handlers. | 13 /// A map from URL paths to handlers. |
16 final _handlers = new Map<String, shelf.Handler>(); | 14 final _handlers = new Map<String, shelf.Handler>(); |
(...skipping 21 matching lines...) Expand all Loading... |
38 _onRequest(shelf.Request request) { | 36 _onRequest(shelf.Request request) { |
39 var components = p.url.split(request.url.path); | 37 var components = p.url.split(request.url.path); |
40 if (components.isEmpty) return new shelf.Response.notFound(null); | 38 if (components.isEmpty) return new shelf.Response.notFound(null); |
41 | 39 |
42 var path = components.removeAt(0); | 40 var path = components.removeAt(0); |
43 var handler = _handlers.remove(path); | 41 var handler = _handlers.remove(path); |
44 if (handler == null) return new shelf.Response.notFound(null); | 42 if (handler == null) return new shelf.Response.notFound(null); |
45 return handler(request.change(path: path)); | 43 return handler(request.change(path: path)); |
46 } | 44 } |
47 } | 45 } |
OLD | NEW |