| 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' as json; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 | 10 |
| 11 import 'test_pub.dart'; | 11 import 'test_pub.dart'; |
| 12 import '../../../pkg/http/lib/http.dart' as http; | 12 import '../../../pkg/http/lib/http.dart' as http; |
| 13 import '../../../pkg/unittest/lib/unittest.dart'; | 13 import '../../../pkg/unittest/lib/unittest.dart'; |
| 14 import '../../pub/io.dart'; | 14 import '../../pub/io.dart'; |
| 15 import '../../pub/utils.dart'; | 15 import '../../pub/utils.dart'; |
| 16 | 16 |
| 17 main() { | 17 main() { |
| 18 setUp(() => normalPackage.scheduleCreate()); | 18 setUp(() => normalPackage.scheduleCreate()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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).then((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(); |
| 82 }); | 82 }); |
| 83 }); | 83 }); |
| 84 | 84 |
| 85 server.handle('GET', '/packages/versions/new.json', (request, response) { | 85 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 86 expect(request.headers.value('authorization'), | 86 expect(request.headers.value('authorization'), |
| 87 equals('Bearer new access token')); | 87 equals('Bearer new access token')); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 var server = new ScheduledServer(); | 155 var server = new ScheduledServer(); |
| 156 credentialsFile(server, 'access token').scheduleCreate(); | 156 credentialsFile(server, 'access token').scheduleCreate(); |
| 157 var pub = startPubLish(server); | 157 var pub = startPubLish(server); |
| 158 | 158 |
| 159 confirmPublish(pub); | 159 confirmPublish(pub); |
| 160 | 160 |
| 161 server.handle('GET', '/packages/versions/new.json', (request, response) { | 161 server.handle('GET', '/packages/versions/new.json', (request, response) { |
| 162 response.statusCode = 401; | 162 response.statusCode = 401; |
| 163 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' | 163 response.headers.set('www-authenticate', 'Bearer error="invalid_token",' |
| 164 ' error_description="your token sucks"'); | 164 ' error_description="your token sucks"'); |
| 165 response.outputStream.writeString(JSON.stringify({ | 165 response.outputStream.writeString(json.stringify({ |
| 166 'error': {'message': 'your token sucks'} | 166 'error': {'message': 'your token sucks'} |
| 167 })); | 167 })); |
| 168 response.outputStream.close(); | 168 response.outputStream.close(); |
| 169 }); | 169 }); |
| 170 | 170 |
| 171 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' | 171 expectLater(pub.nextErrLine(), equals('OAuth2 authorization failed (your ' |
| 172 'token sucks).')); | 172 'token sucks).')); |
| 173 // TODO(rnystrom): The confirm line is run together with this one because | 173 // TODO(rnystrom): The confirm line is run together with this one because |
| 174 // in normal usage, the user will have entered a newline on stdin which | 174 // in normal usage, the user will have entered a newline on stdin which |
| 175 // gets echoed to the terminal. Do something better here? | 175 // gets echoed to the terminal. Do something better here? |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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).then((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 |