| 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 handle_access_token_response_test; | |
| 6 | |
| 7 import 'dart:convert'; | |
| 8 | |
| 9 import 'package:http/http.dart' as http; | |
| 10 import 'package:oauth2/oauth2.dart' as oauth2; | |
| 11 import 'package:oauth2/src/handle_access_token_response.dart'; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import 'utils.dart'; | |
| 15 | |
| 16 final Uri tokenEndpoint = Uri.parse("https://example.com/token"); | |
| 17 | |
| 18 final DateTime startTime = new DateTime.now(); | |
| 19 | |
| 20 oauth2.Credentials handle(http.Response response) => | |
| 21 handleAccessTokenResponse(response, tokenEndpoint, startTime, ["scope"]); | |
| 22 | |
| 23 void main() { | |
| 24 group('an error response', () { | |
| 25 oauth2.Credentials handleError( | |
| 26 {String body: '{"error": "invalid_request"}', | |
| 27 int statusCode: 400, | |
| 28 Map<String, String> headers: | |
| 29 const {"content-type": "application/json"}}) => | |
| 30 handle(new http.Response(body, statusCode, headers: headers)); | |
| 31 | |
| 32 test('causes an AuthorizationException', () { | |
| 33 expect(() => handleError(), throwsAuthorizationException); | |
| 34 }); | |
| 35 | |
| 36 test('with a 401 code causes an AuthorizationException', () { | |
| 37 expect(() => handleError(statusCode: 401), throwsAuthorizationException); | |
| 38 }); | |
| 39 | |
| 40 test('with an unexpected code causes a FormatException', () { | |
| 41 expect(() => handleError(statusCode: 500), throwsFormatException); | |
| 42 }); | |
| 43 | |
| 44 test('with no content-type causes a FormatException', () { | |
| 45 expect(() => handleError(headers: {}), throwsFormatException); | |
| 46 }); | |
| 47 | |
| 48 test('with a non-JSON content-type causes a FormatException', () { | |
| 49 expect(() => handleError(headers: { | |
| 50 'content-type': 'text/plain' | |
| 51 }), throwsFormatException); | |
| 52 }); | |
| 53 | |
| 54 test('with a JSON content-type and charset causes an ' | |
| 55 'AuthorizationException', () { | |
| 56 expect(() => handleError(headers: { | |
| 57 'content-type': 'application/json; charset=UTF-8' | |
| 58 }), throwsAuthorizationException); | |
| 59 }); | |
| 60 | |
| 61 test('with invalid JSON causes a FormatException', () { | |
| 62 expect(() => handleError(body: 'not json'), throwsFormatException); | |
| 63 }); | |
| 64 | |
| 65 test('with a non-string error causes a FormatException', () { | |
| 66 expect(() => handleError(body: '{"error": 12}'), throwsFormatException); | |
| 67 }); | |
| 68 | |
| 69 test('with a non-string error_description causes a FormatException', () { | |
| 70 expect(() => handleError(body: JSON.encode({ | |
| 71 "error": "invalid_request", | |
| 72 "error_description": 12 | |
| 73 })), throwsFormatException); | |
| 74 }); | |
| 75 | |
| 76 test('with a non-string error_uri causes a FormatException', () { | |
| 77 expect(() => handleError(body: JSON.encode({ | |
| 78 "error": "invalid_request", | |
| 79 "error_uri": 12 | |
| 80 })), throwsFormatException); | |
| 81 }); | |
| 82 | |
| 83 test('with a string error_description causes a AuthorizationException', () { | |
| 84 expect(() => handleError(body: JSON.encode({ | |
| 85 "error": "invalid_request", | |
| 86 "error_description": "description" | |
| 87 })), throwsAuthorizationException); | |
| 88 }); | |
| 89 | |
| 90 test('with a string error_uri causes a AuthorizationException', () { | |
| 91 expect(() => handleError(body: JSON.encode({ | |
| 92 "error": "invalid_request", | |
| 93 "error_uri": "http://example.com/error" | |
| 94 })), throwsAuthorizationException); | |
| 95 }); | |
| 96 }); | |
| 97 | |
| 98 group('a success response', () { | |
| 99 oauth2.Credentials handleSuccess( | |
| 100 {String contentType: "application/json", | |
| 101 accessToken: 'access token', | |
| 102 tokenType: 'bearer', | |
| 103 expiresIn, | |
| 104 refreshToken, | |
| 105 scope}) { | |
| 106 return handle(new http.Response(JSON.encode({ | |
| 107 'access_token': accessToken, | |
| 108 'token_type': tokenType, | |
| 109 'expires_in': expiresIn, | |
| 110 'refresh_token': refreshToken, | |
| 111 'scope': scope | |
| 112 }), 200, headers: {'content-type': contentType})); | |
| 113 } | |
| 114 | |
| 115 test('returns the correct credentials', () { | |
| 116 var credentials = handleSuccess(); | |
| 117 expect(credentials.accessToken, equals('access token')); | |
| 118 expect(credentials.tokenEndpoint.toString(), | |
| 119 equals(tokenEndpoint.toString())); | |
| 120 }); | |
| 121 | |
| 122 test('with no content-type causes a FormatException', () { | |
| 123 expect(() => handleSuccess(contentType: null), throwsFormatException); | |
| 124 }); | |
| 125 | |
| 126 test('with a non-JSON content-type causes a FormatException', () { | |
| 127 expect(() => handleSuccess(contentType: 'text/plain'), | |
| 128 throwsFormatException); | |
| 129 }); | |
| 130 | |
| 131 test('with a JSON content-type and charset returns the correct ' | |
| 132 'credentials', () { | |
| 133 var credentials = handleSuccess( | |
| 134 contentType: 'application/json; charset=UTF-8'); | |
| 135 expect(credentials.accessToken, equals('access token')); | |
| 136 }); | |
| 137 | |
| 138 test('with a JavScript content-type returns the correct credentials', () { | |
| 139 var credentials = handleSuccess(contentType: 'text/javascript'); | |
| 140 expect(credentials.accessToken, equals('access token')); | |
| 141 }); | |
| 142 | |
| 143 test('with a null access token throws a FormatException', () { | |
| 144 expect(() => handleSuccess(accessToken: null), throwsFormatException); | |
| 145 }); | |
| 146 | |
| 147 test('with a non-string access token throws a FormatException', () { | |
| 148 expect(() => handleSuccess(accessToken: 12), throwsFormatException); | |
| 149 }); | |
| 150 | |
| 151 test('with a null token type throws a FormatException', () { | |
| 152 expect(() => handleSuccess(tokenType: null), throwsFormatException); | |
| 153 }); | |
| 154 | |
| 155 test('with a non-string token type throws a FormatException', () { | |
| 156 expect(() => handleSuccess(tokenType: 12), throwsFormatException); | |
| 157 }); | |
| 158 | |
| 159 test('with a non-"bearer" token type throws a FormatException', () { | |
| 160 expect(() => handleSuccess(tokenType: "mac"), throwsFormatException); | |
| 161 }); | |
| 162 | |
| 163 test('with a non-int expires-in throws a FormatException', () { | |
| 164 expect(() => handleSuccess(expiresIn: "whenever"), throwsFormatException); | |
| 165 }); | |
| 166 | |
| 167 test('with expires-in sets the expiration to ten seconds earlier than the ' | |
| 168 'server says', () { | |
| 169 var credentials = handleSuccess(expiresIn: 100); | |
| 170 expect(credentials.expiration.millisecondsSinceEpoch, | |
| 171 startTime.millisecondsSinceEpoch + 90 * 1000); | |
| 172 }); | |
| 173 | |
| 174 test('with a non-string refresh token throws a FormatException', () { | |
| 175 expect(() => handleSuccess(refreshToken: 12), throwsFormatException); | |
| 176 }); | |
| 177 | |
| 178 test('with a refresh token sets the refresh token', () { | |
| 179 var credentials = handleSuccess(refreshToken: "refresh me"); | |
| 180 expect(credentials.refreshToken, equals("refresh me")); | |
| 181 }); | |
| 182 | |
| 183 test('with a non-string scope throws a FormatException', () { | |
| 184 expect(() => handleSuccess(scope: 12), throwsFormatException); | |
| 185 }); | |
| 186 | |
| 187 test('with a scope sets the scopes', () { | |
| 188 var credentials = handleSuccess(scope: "scope1 scope2"); | |
| 189 expect(credentials.scopes, equals(["scope1", "scope2"])); | |
| 190 }); | |
| 191 }); | |
| 192 } | |
| OLD | NEW |