OLD | NEW |
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 oauth2.utils; | 1 library oauth2.utils; |
6 | |
7 import 'dart:convert'; | 2 import 'dart:convert'; |
8 | |
9 import 'package:http/http.dart' as http; | 3 import 'package:http/http.dart' as http; |
10 import 'package:scheduled_test/scheduled_process.dart'; | 4 import 'package:scheduled_test/scheduled_process.dart'; |
11 import 'package:scheduled_test/scheduled_test.dart'; | 5 import 'package:scheduled_test/scheduled_test.dart'; |
12 import 'package:scheduled_test/scheduled_server.dart'; | 6 import 'package:scheduled_test/scheduled_server.dart'; |
13 import 'package:shelf/shelf.dart' as shelf; | 7 import 'package:shelf/shelf.dart' as shelf; |
14 | |
15 import '../../lib/src/utils.dart'; | 8 import '../../lib/src/utils.dart'; |
16 | |
17 void authorizePub(ScheduledProcess pub, ScheduledServer server, | 9 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
18 [String accessToken="access token"]) { | 10 [String accessToken = "access token"]) { |
19 pub.stdout.expect('Pub needs your authorization to upload packages on your ' | 11 pub.stdout.expect( |
20 'behalf.'); | 12 'Pub needs your authorization to upload packages on your ' 'behalf.'); |
21 | |
22 schedule(() { | 13 schedule(() { |
23 return pub.stdout.next().then((line) { | 14 return pub.stdout.next().then((line) { |
24 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z.%+-]+)[$&]') | 15 var match = |
25 .firstMatch(line); | 16 new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z.%+-]+)[$&]').firstMatch(line
); |
26 expect(match, isNotNull); | 17 expect(match, isNotNull); |
27 | |
28 var redirectUrl = Uri.parse(Uri.decodeComponent(match.group(1))); | 18 var redirectUrl = Uri.parse(Uri.decodeComponent(match.group(1))); |
29 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); | 19 redirectUrl = addQueryParameters(redirectUrl, { |
30 return (new http.Request('GET', redirectUrl)..followRedirects = false) | 20 'code': 'access code' |
31 .send(); | 21 }); |
| 22 return (new http.Request('GET', redirectUrl)..followRedirects = |
| 23 false).send(); |
32 }).then((response) { | 24 }).then((response) { |
33 expect(response.headers['location'], | 25 expect( |
| 26 response.headers['location'], |
34 equals('http://pub.dartlang.org/authorized')); | 27 equals('http://pub.dartlang.org/authorized')); |
35 }); | 28 }); |
36 }); | 29 }); |
37 | |
38 handleAccessTokenRequest(server, accessToken); | 30 handleAccessTokenRequest(server, accessToken); |
39 } | 31 } |
40 | |
41 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 32 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
42 server.handle('POST', '/token', (request) { | 33 server.handle('POST', '/token', (request) { |
43 return request.readAsString().then((body) { | 34 return request.readAsString().then((body) { |
44 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 35 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
45 | |
46 return new shelf.Response.ok(JSON.encode({ | 36 return new shelf.Response.ok(JSON.encode({ |
47 "access_token": accessToken, | 37 "access_token": accessToken, |
48 "token_type": "bearer" | 38 "token_type": "bearer" |
49 }), headers: {'content-type': 'application/json'}); | 39 }), headers: { |
| 40 'content-type': 'application/json' |
| 41 }); |
50 }); | 42 }); |
51 }); | 43 }); |
52 } | 44 } |
53 | |
OLD | NEW |