| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 utils; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection' show Queue; | |
| 9 | |
| 10 import 'package:http/http.dart' as http; | |
| 11 import 'package:http/testing.dart'; | |
| 12 import 'package:oauth2/oauth2.dart' as oauth2; | |
| 13 import 'package:unittest/unittest.dart'; | |
| 14 | |
| 15 class ExpectClient extends MockClient { | |
| 16 final Queue<MockClientHandler> _handlers; | |
| 17 | |
| 18 ExpectClient._(MockClientHandler fn) | |
| 19 : _handlers = new Queue<MockClientHandler>(), | |
| 20 super(fn); | |
| 21 | |
| 22 factory ExpectClient() { | |
| 23 var client; | |
| 24 client = new ExpectClient._((request) => | |
| 25 client._handleRequest(request)); | |
| 26 return client; | |
| 27 } | |
| 28 | |
| 29 void expectRequest(MockClientHandler fn) { | |
| 30 var completer = new Completer(); | |
| 31 expect(completer.future, completes); | |
| 32 | |
| 33 _handlers.add((request) { | |
| 34 completer.complete(null); | |
| 35 return fn(request); | |
| 36 }); | |
| 37 } | |
| 38 | |
| 39 Future<http.Response> _handleRequest(http.Request request) { | |
| 40 if (_handlers.isEmpty) { | |
| 41 return new Future.value(new http.Response('not found', 404)); | |
| 42 } else { | |
| 43 return _handlers.removeFirst()(request); | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 /// A matcher for AuthorizationExceptions. | |
| 49 const isAuthorizationException = const _AuthorizationException(); | |
| 50 | |
| 51 /// A matcher for functions that throw AuthorizationException. | |
| 52 const Matcher throwsAuthorizationException = | |
| 53 const Throws(isAuthorizationException); | |
| 54 | |
| 55 class _AuthorizationException extends TypeMatcher { | |
| 56 const _AuthorizationException() : super("AuthorizationException"); | |
| 57 bool matches(item, Map matchState) => | |
| 58 item is oauth2.AuthorizationException; | |
| 59 } | |
| 60 | |
| 61 /// A matcher for ExpirationExceptions. | |
| 62 const isExpirationException = const _ExpirationException(); | |
| 63 | |
| 64 /// A matcher for functions that throw ExpirationException. | |
| 65 const Matcher throwsExpirationException = | |
| 66 const Throws(isExpirationException); | |
| 67 | |
| 68 class _ExpirationException extends TypeMatcher { | |
| 69 const _ExpirationException() : super("ExpirationException"); | |
| 70 bool matches(item, Map matchState) => | |
| 71 item is oauth2.ExpirationException; | |
| 72 } | |
| OLD | NEW |