OLD | NEW |
(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 oauth2.utils; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:json' as json; |
| 9 import 'dart:uri'; |
| 10 |
| 11 import 'package:http/http.dart' as http; |
| 12 import 'package:scheduled_test/scheduled_process.dart'; |
| 13 import 'package:scheduled_test/scheduled_test.dart'; |
| 14 import 'package:scheduled_test/scheduled_server.dart'; |
| 15 |
| 16 import '../../../pub/io.dart'; |
| 17 import '../../../pub/utils.dart'; |
| 18 import '../test_pub.dart'; |
| 19 |
| 20 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
| 21 [String accessToken="access token"]) { |
| 22 // TODO(rnystrom): The confirm line is run together with this one because |
| 23 // in normal usage, the user will have entered a newline on stdin which |
| 24 // gets echoed to the terminal. Do something better here? |
| 25 expect(pub.nextLine(), completion(equals( |
| 26 'Looks great! Are you ready to upload your package (y/n)? ' |
| 27 'Pub needs your authorization to upload packages on your behalf.'))); |
| 28 |
| 29 expect(pub.nextLine().then((line) { |
| 30 var match = new RegExp(r'[?&]redirect_uri=([0-9a-zA-Z%+-]+)[$&]') |
| 31 .firstMatch(line); |
| 32 expect(match, isNotNull); |
| 33 |
| 34 var redirectUrl = Uri.parse(decodeUriComponent(match.group(1))); |
| 35 redirectUrl = addQueryParameters(redirectUrl, {'code': 'access code'}); |
| 36 return (new http.Request('GET', redirectUrl)..followRedirects = false) |
| 37 .send(); |
| 38 }).then((response) { |
| 39 expect(response.headers['location'], |
| 40 equals('http://pub.dartlang.org/authorized')); |
| 41 }), completes); |
| 42 |
| 43 handleAccessTokenRequest(server, accessToken); |
| 44 } |
| 45 |
| 46 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
| 47 server.handle('POST', '/token', (request) { |
| 48 return new ByteStream(request).toBytes().then((bytes) { |
| 49 var body = new String.fromCharCodes(bytes); |
| 50 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
| 51 |
| 52 request.response.headers.contentType = |
| 53 new ContentType("application", "json"); |
| 54 request.response.write(json.stringify({ |
| 55 "access_token": accessToken, |
| 56 "token_type": "bearer" |
| 57 })); |
| 58 request.response.close(); |
| 59 }); |
| 60 }); |
| 61 } |
| 62 |
OLD | NEW |