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