| 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; | 5 library test.util.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 import '../utils.dart'; | |
| 11 | |
| 12 /// A Shelf handler that provides support for one-time handlers. | 10 /// A Shelf handler that provides support for one-time handlers. |
| 13 /// | 11 /// |
| 14 /// 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 |
| 15 /// invalid and don't need to have a persistent URL. | 13 /// invalid and don't need to have a persistent URL. |
| 16 class OneOffHandler { | 14 class OneOffHandler { |
| 17 /// A map from URL paths to handlers. | 15 /// A map from URL paths to handlers. |
| 18 final _handlers = new Map<String, shelf.Handler>(); | 16 final _handlers = new Map<String, shelf.Handler>(); |
| 19 | 17 |
| 20 /// The counter of handlers that have been activated. | 18 /// The counter of handlers that have been activated. |
| 21 var _counter = 0; | 19 var _counter = 0; |
| 22 | 20 |
| 23 /// The actual [shelf.Handler] that dispatches requests. | 21 /// The actual [shelf.Handler] that dispatches requests. |
| 24 shelf.Handler get handler => _onRequest; | 22 shelf.Handler get handler => _onRequest; |
| 25 | 23 |
| 26 /// Creates a new one-off handler that forwards to [handler]. | 24 /// Creates a new one-off handler that forwards to [handler]. |
| 27 /// | 25 /// |
| 28 /// Returns a string that's the URL path for hitting this handler, relative to | 26 /// Returns a string that's the URL path for hitting this handler, relative to |
| 29 /// the URL for the one-off handler itself. | 27 /// the URL for the one-off handler itself. |
| 30 /// | 28 /// |
| 31 /// [handler] will be unmounted as soon as it receives a request. | 29 /// [handler] will be unmounted as soon as it receives a request. |
| 32 String create(shelf.Handler handler) { | 30 String create(shelf.Handler handler) { |
| 33 var path = _counter.toString(); | 31 var path = _counter.toString(); |
| 34 _handlers[path] = handler; | 32 _handlers[path] = handler; |
| 35 _counter++; | 33 _counter++; |
| 36 return path; | 34 return path; |
| 37 } | 35 } |
| 38 | 36 |
| 39 /// Dispatches [request] to the appropriate handler. | 37 /// Dispatches [request] to the appropriate handler. |
| 40 _onRequest(shelf.Request request) { | 38 _onRequest(shelf.Request request) { |
| 41 var components = p.url.split(shelfUrl(request).path); | 39 var components = p.url.split(request.url.path); |
| 42 if (components.isEmpty) return new shelf.Response.notFound(null); | 40 if (components.isEmpty) return new shelf.Response.notFound(null); |
| 43 | 41 |
| 44 var path = components.removeAt(0); | 42 var path = components.removeAt(0); |
| 45 var handler = _handlers.remove(path); | 43 var handler = _handlers.remove(path); |
| 46 if (handler == null) return new shelf.Response.notFound(null); | 44 if (handler == null) return new shelf.Response.notFound(null); |
| 47 return handler(shelfChange(request, path: path)); | 45 return handler(request.change(path: path)); |
| 48 } | 46 } |
| 49 } | 47 } |
| OLD | NEW |