Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(541)

Side by Side Diff: pkg/oauth2/test/utils.dart

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 '../../unittest/lib/unittest.dart';
8 import '../../http/lib/http.dart' as http;
9 import '../../http/lib/testing.dart';
10 import '../lib/oauth2.dart' as oauth2;
11
12 class ExpectClient extends MockClient {
13 final Queue<MockClientHandler> _handlers;
14
15 ExpectClient._(MockClientHandler fn)
16 : _handlers = new Queue<MockClientHandler>(),
17 super(fn);
18
19 factory ExpectClient() {
20 var client;
21 client = new ExpectClient._((request) =>
22 client._handleRequest(request));
23 return client;
24 }
25
26 void expectRequest(MockClientHandler fn) {
27 var completer = new Completer();
28 expect(completer.future, completes);
29
30 _handlers.add((request) {
31 completer.complete(null);
32 return fn(request);
33 });
34 }
35
36 Future<http.Response> _handleRequest(http.Request request) {
37 if (_handlers.isEmpty) {
38 return new Future.immediate(new http.Response('not found', 404));
39 } else {
40 return _handlers.removeFirst()(request);
41 }
42 }
43 }
44
45 // TODO(nweiz): remove this once it's built in to unittest
46 /// A matcher for StateErrors.
47 const isStateError = const _StateError();
48
49 /// A matcher for functions that throw StateError.
50 const Matcher throwsStateError =
51 const Throws(isStateError);
52
53 class _StateError extends TypeMatcher {
54 const _StateError() : super("StateError");
55 bool matches(item, MatchState matchState) => item is StateError;
56 }
57
58 /// A matcher for AuthorizationExceptions.
59 const isAuthorizationException = const _AuthorizationException();
60
61 /// A matcher for functions that throw AuthorizationException.
62 const Matcher throwsAuthorizationException =
63 const Throws(isAuthorizationException);
64
65 class _AuthorizationException extends TypeMatcher {
66 const _AuthorizationException() : super("AuthorizationException");
67 bool matches(item, MatchState matchState) =>
68 item is oauth2.AuthorizationException;
69 }
70
71 /// A matcher for ExpirationExceptions.
72 const isExpirationException = const _ExpirationException();
73
74 /// A matcher for functions that throw ExpirationException.
75 const Matcher throwsExpirationException =
76 const Throws(isExpirationException);
77
78 class _ExpirationException extends TypeMatcher {
79 const _ExpirationException() : super("ExpirationException");
80 bool matches(item, MatchState matchState) =>
81 item is oauth2.ExpirationException;
82 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698