OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 oauth2_test; | 5 library oauth2_test; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 var server = new ScheduledServer(); | 61 var server = new ScheduledServer(); |
62 credentialsFile(server, 'access token', | 62 credentialsFile(server, 'access token', |
63 refreshToken: 'refresh token', | 63 refreshToken: 'refresh token', |
64 expiration: new Date.now().subtract(new Duration(hours: 1))) | 64 expiration: new Date.now().subtract(new Duration(hours: 1))) |
65 .scheduleCreate(); | 65 .scheduleCreate(); |
66 | 66 |
67 var pub = startPubLish(server); | 67 var pub = startPubLish(server); |
68 confirmPublish(pub); | 68 confirmPublish(pub); |
69 | 69 |
70 server.handle('POST', '/token', (request, response) { | 70 server.handle('POST', '/token', (request, response) { |
71 return consumeInputStream(request.inputStream).transform((bytes) { | 71 return consumeInputStream(request.inputStream).then((bytes) { |
72 var body = new String.fromCharCodes(bytes); | 72 var body = new String.fromCharCodes(bytes); |
73 expect(body, matches( | 73 expect(body, matches( |
74 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); | 74 new RegExp(r'(^|&)refresh_token=refresh\+token(&|$)'))); |
75 | 75 |
76 response.headers.contentType = new ContentType("application", "json"); | 76 response.headers.contentType = new ContentType("application", "json"); |
77 response.outputStream.writeString(JSON.stringify({ | 77 response.outputStream.writeString(JSON.stringify({ |
78 "access_token": "new access token", | 78 "access_token": "new access token", |
79 "token_type": "bearer" | 79 "token_type": "bearer" |
80 })); | 80 })); |
81 response.outputStream.close(); | 81 response.outputStream.close(); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 192 |
193 expectLater(pub.nextLine().chain((line) { | 193 expectLater(pub.nextLine().chain((line) { |
194 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') | 194 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') |
195 .firstMatch(line); | 195 .firstMatch(line); |
196 expect(match, isNotNull); | 196 expect(match, isNotNull); |
197 | 197 |
198 var redirectUrl = new Uri.fromString(decodeUriComponent(match.group(1))); | 198 var redirectUrl = new Uri.fromString(decodeUriComponent(match.group(1))); |
199 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); | 199 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); |
200 return (new http.Request('GET', redirectUrl)..followRedirects = false) | 200 return (new http.Request('GET', redirectUrl)..followRedirects = false) |
201 .send(); | 201 .send(); |
202 }).transform((response) { | 202 }).then((response) { |
203 expect(response.headers['location'], | 203 expect(response.headers['location'], |
204 equals(['http://pub.dartlang.org/authorized'])); | 204 equals(['http://pub.dartlang.org/authorized'])); |
205 }), anything); | 205 }), anything); |
206 | 206 |
207 handleAccessTokenRequest(server, accessToken); | 207 handleAccessTokenRequest(server, accessToken); |
208 } | 208 } |
209 | 209 |
210 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 210 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
211 server.handle('POST', '/token', (request, response) { | 211 server.handle('POST', '/token', (request, response) { |
212 return consumeInputStream(request.inputStream).transform((bytes) { | 212 return consumeInputStream(request.inputStream).then((bytes) { |
213 var body = new String.fromCharCodes(bytes); | 213 var body = new String.fromCharCodes(bytes); |
214 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 214 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
215 | 215 |
216 response.headers.contentType = new ContentType("application", "json"); | 216 response.headers.contentType = new ContentType("application", "json"); |
217 response.outputStream.writeString(JSON.stringify({ | 217 response.outputStream.writeString(JSON.stringify({ |
218 "access_token": accessToken, | 218 "access_token": accessToken, |
219 "token_type": "bearer" | 219 "token_type": "bearer" |
220 })); | 220 })); |
221 response.outputStream.close(); | 221 response.outputStream.close(); |
222 }); | 222 }); |
223 }); | 223 }); |
224 } | 224 } |
OLD | NEW |