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 client_test; | 5 library client_test; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 }); | 110 }); |
111 | 111 |
112 test("without a refresh token can't manually refresh the credentials", () { | 112 test("without a refresh token can't manually refresh the credentials", () { |
113 var credentials = new oauth2.Credentials('access token'); | 113 var credentials = new oauth2.Credentials('access token'); |
114 var client = new oauth2.Client('identifier', 'secret', credentials, | 114 var client = new oauth2.Client('identifier', 'secret', credentials, |
115 httpClient: httpClient); | 115 httpClient: httpClient); |
116 | 116 |
117 expect(client.refreshCredentials(), throwsStateError); | 117 expect(client.refreshCredentials(), throwsStateError); |
118 }); | 118 }); |
119 }); | 119 }); |
| 120 |
| 121 group('with invalid credentials', () { |
| 122 setUp(createHttpClient); |
| 123 |
| 124 test('throws an AuthorizationException for a 401 response', () { |
| 125 var credentials = new oauth2.Credentials('access token'); |
| 126 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 127 httpClient: httpClient); |
| 128 |
| 129 httpClient.expectRequest((request) { |
| 130 expect(request.method, equals('GET')); |
| 131 expect(request.url.toString(), equals(requestUri.toString())); |
| 132 expect(request.headers['authorization'], |
| 133 equals('Bearer access token')); |
| 134 |
| 135 var authenticate = 'Bearer error="invalid_token", error_description=' |
| 136 '"Something is terribly wrong."'; |
| 137 return new Future.immediate(new http.Response('bad job', 401, |
| 138 headers: {'www-authenticate': authenticate})); |
| 139 }); |
| 140 |
| 141 expect(client.read(requestUri), throwsAuthorizationException); |
| 142 }); |
| 143 |
| 144 test('passes through a 401 response without www-authenticate', () { |
| 145 var credentials = new oauth2.Credentials('access token'); |
| 146 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 147 httpClient: httpClient); |
| 148 |
| 149 httpClient.expectRequest((request) { |
| 150 expect(request.method, equals('GET')); |
| 151 expect(request.url.toString(), equals(requestUri.toString())); |
| 152 expect(request.headers['authorization'], |
| 153 equals('Bearer access token')); |
| 154 |
| 155 return new Future.immediate(new http.Response('bad job', 401)); |
| 156 }); |
| 157 |
| 158 expect( |
| 159 client.get(requestUri).transform((response) => response.statusCode), |
| 160 completion(equals(401))); |
| 161 }); |
| 162 |
| 163 test('passes through a 401 response with invalid www-authenticate', () { |
| 164 var credentials = new oauth2.Credentials('access token'); |
| 165 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 166 httpClient: httpClient); |
| 167 |
| 168 httpClient.expectRequest((request) { |
| 169 expect(request.method, equals('GET')); |
| 170 expect(request.url.toString(), equals(requestUri.toString())); |
| 171 expect(request.headers['authorization'], |
| 172 equals('Bearer access token')); |
| 173 |
| 174 var authenticate = 'Bearer error="invalid_token", error_description=' |
| 175 '"Something is terribly wrong.", '; |
| 176 return new Future.immediate(new http.Response('bad job', 401, |
| 177 headers: {'www-authenticate': authenticate})); |
| 178 }); |
| 179 |
| 180 expect( |
| 181 client.get(requestUri).transform((response) => response.statusCode), |
| 182 completion(equals(401))); |
| 183 }); |
| 184 |
| 185 test('passes through a 401 response with non-bearer www-authenticate', () { |
| 186 var credentials = new oauth2.Credentials('access token'); |
| 187 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 188 httpClient: httpClient); |
| 189 |
| 190 httpClient.expectRequest((request) { |
| 191 expect(request.method, equals('GET')); |
| 192 expect(request.url.toString(), equals(requestUri.toString())); |
| 193 expect(request.headers['authorization'], |
| 194 equals('Bearer access token')); |
| 195 |
| 196 return new Future.immediate(new http.Response('bad job', 401, |
| 197 headers: {'www-authenticate': 'Digest'})); |
| 198 }); |
| 199 |
| 200 expect( |
| 201 client.get(requestUri).transform((response) => response.statusCode), |
| 202 completion(equals(401))); |
| 203 }); |
| 204 |
| 205 test('passes through a 401 response with non-OAuth2 www-authenticate', () { |
| 206 var credentials = new oauth2.Credentials('access token'); |
| 207 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 208 httpClient: httpClient); |
| 209 |
| 210 httpClient.expectRequest((request) { |
| 211 expect(request.method, equals('GET')); |
| 212 expect(request.url.toString(), equals(requestUri.toString())); |
| 213 expect(request.headers['authorization'], |
| 214 equals('Bearer access token')); |
| 215 |
| 216 return new Future.immediate(new http.Response('bad job', 401, |
| 217 headers: {'www-authenticate': 'Bearer'})); |
| 218 }); |
| 219 |
| 220 expect( |
| 221 client.get(requestUri).transform((response) => response.statusCode), |
| 222 completion(equals(401))); |
| 223 }); |
| 224 }); |
120 } | 225 } |
OLD | NEW |