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

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

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Refresh token fixes and 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 client_test;
6
7 import 'dart:io';
8 import 'dart:json';
9 import 'dart:uri';
10
11 import '../../unittest/lib/unittest.dart';
12 import '../../http/lib/http.dart' as http;
13 import '../lib/oauth2.dart' as oauth2;
14 import 'utils.dart';
15
16 final Uri requestUri = new Uri.fromString("http://example.com/resource");
17
18 final Uri tokenEndpoint = new Uri.fromString('http://example.com/token');
19
20 ExpectClient httpClient;
21
22 void createHttpClient() {
23 httpClient = new ExpectClient();
24 }
25
26 void main() {
27 group('with expired credentials', () {
28 setUp(createHttpClient);
29
30 test("that can't be refreshed throws an ExpirationException on send", () {
31 var expiration = new Date.now().subtract(new Duration(hours: 1));
32 var credentials = new oauth2.Credentials(
33 'access token', null, null, [], expiration);
34 var client = new oauth2.Client('identifier', 'secret', credentials,
35 httpClient: httpClient);
36
37 expect(client.get(requestUri), throwsExpirationException);
38 });
39
40 test("that can be refreshed refreshes the credentials and sends the "
41 "request", () {
42 var expiration = new Date.now().subtract(new Duration(hours: 1));
43 var credentials = new oauth2.Credentials(
44 'access token', 'refresh token', tokenEndpoint, [], expiration);
45 var client = new oauth2.Client('identifier', 'secret', credentials,
46 httpClient: httpClient);
47
48 httpClient.expectRequest((request) {
49 expect(request.method, equals('POST'));
50 expect(request.url.toString(), equals(tokenEndpoint.toString()));
51 return new Future.immediate(new http.Response(JSON.stringify({
52 'access_token': 'new access token',
53 'token_type': 'bearer'
54 }), 200, headers: {'content-type': 'application/json'}));
55 });
56
57 httpClient.expectRequest((request) {
58 expect(request.method, equals('GET'));
59 expect(request.url.toString(), equals(requestUri.toString()));
60 expect(request.headers['authorization'],
61 equals('Bearer new access token'));
62
63 return new Future.immediate(new http.Response('good job', 200));
64 });
65
66 expect(client.read(requestUri).transform((_) {
67 expect(client.credentials.accessToken, equals('new access token'));
68 }), completes);
69 });
70 });
71
72 group('with valid credentials', () {
73 setUp(createHttpClient);
74
75 test("sends a request with bearer authorization", () {
76 var credentials = new oauth2.Credentials('access token');
77 var client = new oauth2.Client('identifier', 'secret', credentials,
78 httpClient: httpClient);
79
80 httpClient.expectRequest((request) {
81 expect(request.method, equals('GET'));
82 expect(request.url.toString(), equals(requestUri.toString()));
83 expect(request.headers['authorization'],
84 equals('Bearer access token'));
85
86 return new Future.immediate(new http.Response('good job', 200));
87 });
88
89 expect(client.read(requestUri), completion(equals('good job')));
90 });
91
92 test("can manually refresh the credentials", () {
93 var credentials = new oauth2.Credentials(
94 'access token', 'refresh token', tokenEndpoint);
95 var client = new oauth2.Client('identifier', 'secret', credentials,
96 httpClient: httpClient);
97
98 httpClient.expectRequest((request) {
99 expect(request.method, equals('POST'));
100 expect(request.url.toString(), equals(tokenEndpoint.toString()));
101 return new Future.immediate(new http.Response(JSON.stringify({
102 'access_token': 'new access token',
103 'token_type': 'bearer'
104 }), 200, headers: {'content-type': 'application/json'}));
105 });
106
107 expect(client.refreshCredentials().transform((_) {
108 expect(client.credentials.accessToken, equals('new access token'));
109 }), completes);
110 });
111
112 test("without a refresh token can't manually refresh the credentials", () {
113 var credentials = new oauth2.Credentials('access token');
114 var client = new oauth2.Client('identifier', 'secret', credentials,
115 httpClient: httpClient);
116
117 expect(client.refreshCredentials(), throwsStateError);
118 });
119 });
120 }
OLDNEW
« no previous file with comments | « pkg/oauth2/test/authorization_code_grant_test.dart ('k') | pkg/oauth2/test/credentials_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698