| 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:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| 11 import 'package:scheduled_test/scheduled_process.dart'; | 11 import 'package:scheduled_test/scheduled_process.dart'; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; | 12 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 import 'package:scheduled_test/scheduled_server.dart'; | 13 import 'package:scheduled_test/scheduled_server.dart'; |
| 14 import 'package:shelf/shelf.dart' as shelf; |
| 14 | 15 |
| 15 import '../../lib/src/io.dart'; | 16 import '../../lib/src/io.dart'; |
| 16 import '../../lib/src/utils.dart'; | 17 import '../../lib/src/utils.dart'; |
| 17 | 18 |
| 18 void authorizePub(ScheduledProcess pub, ScheduledServer server, | 19 void authorizePub(ScheduledProcess pub, ScheduledServer server, |
| 19 [String accessToken="access token"]) { | 20 [String accessToken="access token"]) { |
| 20 pub.stdout.expect('Pub needs your authorization to upload packages on your ' | 21 pub.stdout.expect('Pub needs your authorization to upload packages on your ' |
| 21 'behalf.'); | 22 'behalf.'); |
| 22 | 23 |
| 23 schedule(() { | 24 schedule(() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 34 expect(response.headers['location'], | 35 expect(response.headers['location'], |
| 35 equals('http://pub.dartlang.org/authorized')); | 36 equals('http://pub.dartlang.org/authorized')); |
| 36 }); | 37 }); |
| 37 }); | 38 }); |
| 38 | 39 |
| 39 handleAccessTokenRequest(server, accessToken); | 40 handleAccessTokenRequest(server, accessToken); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { | 43 void handleAccessTokenRequest(ScheduledServer server, String accessToken) { |
| 43 server.handle('POST', '/token', (request) { | 44 server.handle('POST', '/token', (request) { |
| 44 return new ByteStream(request).toBytes().then((bytes) { | 45 return request.readAsString().then((body) { |
| 45 var body = new String.fromCharCodes(bytes); | |
| 46 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); | 46 expect(body, matches(new RegExp(r'(^|&)code=access\+code(&|$)'))); |
| 47 | 47 |
| 48 request.response.headers.contentType = | 48 return new shelf.Response.ok(JSON.encode({ |
| 49 new ContentType("application", "json"); | |
| 50 request.response.write(JSON.encode({ | |
| 51 "access_token": accessToken, | 49 "access_token": accessToken, |
| 52 "token_type": "bearer" | 50 "token_type": "bearer" |
| 53 })); | 51 }), headers: {'content-type': 'application/json'}); |
| 54 request.response.close(); | |
| 55 }); | 52 }); |
| 56 }); | 53 }); |
| 57 } | 54 } |
| 58 | 55 |
| OLD | NEW |