| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 httpClient.expectRequest((request) { | 57 httpClient.expectRequest((request) { |
| 58 expect(request.method, equals('GET')); | 58 expect(request.method, equals('GET')); |
| 59 expect(request.url.toString(), equals(requestUri.toString())); | 59 expect(request.url.toString(), equals(requestUri.toString())); |
| 60 expect(request.headers['authorization'], | 60 expect(request.headers['authorization'], |
| 61 equals('Bearer new access token')); | 61 equals('Bearer new access token')); |
| 62 | 62 |
| 63 return new Future.immediate(new http.Response('good job', 200)); | 63 return new Future.immediate(new http.Response('good job', 200)); |
| 64 }); | 64 }); |
| 65 | 65 |
| 66 expect(client.read(requestUri).transform((_) { | 66 expect(client.read(requestUri).then((_) { |
| 67 expect(client.credentials.accessToken, equals('new access token')); | 67 expect(client.credentials.accessToken, equals('new access token')); |
| 68 }), completes); | 68 }), completes); |
| 69 }); | 69 }); |
| 70 }); | 70 }); |
| 71 | 71 |
| 72 group('with valid credentials', () { | 72 group('with valid credentials', () { |
| 73 setUp(createHttpClient); | 73 setUp(createHttpClient); |
| 74 | 74 |
| 75 test("sends a request with bearer authorization", () { | 75 test("sends a request with bearer authorization", () { |
| 76 var credentials = new oauth2.Credentials('access token'); | 76 var credentials = new oauth2.Credentials('access token'); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 97 | 97 |
| 98 httpClient.expectRequest((request) { | 98 httpClient.expectRequest((request) { |
| 99 expect(request.method, equals('POST')); | 99 expect(request.method, equals('POST')); |
| 100 expect(request.url.toString(), equals(tokenEndpoint.toString())); | 100 expect(request.url.toString(), equals(tokenEndpoint.toString())); |
| 101 return new Future.immediate(new http.Response(JSON.stringify({ | 101 return new Future.immediate(new http.Response(JSON.stringify({ |
| 102 'access_token': 'new access token', | 102 'access_token': 'new access token', |
| 103 'token_type': 'bearer' | 103 'token_type': 'bearer' |
| 104 }), 200, headers: {'content-type': 'application/json'})); | 104 }), 200, headers: {'content-type': 'application/json'})); |
| 105 }); | 105 }); |
| 106 | 106 |
| 107 expect(client.refreshCredentials().transform((_) { | 107 expect(client.refreshCredentials().then((_) { |
| 108 expect(client.credentials.accessToken, equals('new access token')); | 108 expect(client.credentials.accessToken, equals('new access token')); |
| 109 }), completes); | 109 }), completes); |
| 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); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 httpClient.expectRequest((request) { | 149 httpClient.expectRequest((request) { |
| 150 expect(request.method, equals('GET')); | 150 expect(request.method, equals('GET')); |
| 151 expect(request.url.toString(), equals(requestUri.toString())); | 151 expect(request.url.toString(), equals(requestUri.toString())); |
| 152 expect(request.headers['authorization'], | 152 expect(request.headers['authorization'], |
| 153 equals('Bearer access token')); | 153 equals('Bearer access token')); |
| 154 | 154 |
| 155 return new Future.immediate(new http.Response('bad job', 401)); | 155 return new Future.immediate(new http.Response('bad job', 401)); |
| 156 }); | 156 }); |
| 157 | 157 |
| 158 expect( | 158 expect( |
| 159 client.get(requestUri).transform((response) => response.statusCode), | 159 client.get(requestUri).then((response) => response.statusCode), |
| 160 completion(equals(401))); | 160 completion(equals(401))); |
| 161 }); | 161 }); |
| 162 | 162 |
| 163 test('passes through a 401 response with invalid www-authenticate', () { | 163 test('passes through a 401 response with invalid www-authenticate', () { |
| 164 var credentials = new oauth2.Credentials('access token'); | 164 var credentials = new oauth2.Credentials('access token'); |
| 165 var client = new oauth2.Client('identifier', 'secret', credentials, | 165 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 166 httpClient: httpClient); | 166 httpClient: httpClient); |
| 167 | 167 |
| 168 httpClient.expectRequest((request) { | 168 httpClient.expectRequest((request) { |
| 169 expect(request.method, equals('GET')); | 169 expect(request.method, equals('GET')); |
| 170 expect(request.url.toString(), equals(requestUri.toString())); | 170 expect(request.url.toString(), equals(requestUri.toString())); |
| 171 expect(request.headers['authorization'], | 171 expect(request.headers['authorization'], |
| 172 equals('Bearer access token')); | 172 equals('Bearer access token')); |
| 173 | 173 |
| 174 var authenticate = 'Bearer error="invalid_token", error_description=' | 174 var authenticate = 'Bearer error="invalid_token", error_description=' |
| 175 '"Something is terribly wrong.", '; | 175 '"Something is terribly wrong.", '; |
| 176 return new Future.immediate(new http.Response('bad job', 401, | 176 return new Future.immediate(new http.Response('bad job', 401, |
| 177 headers: {'www-authenticate': authenticate})); | 177 headers: {'www-authenticate': authenticate})); |
| 178 }); | 178 }); |
| 179 | 179 |
| 180 expect( | 180 expect( |
| 181 client.get(requestUri).transform((response) => response.statusCode), | 181 client.get(requestUri).then((response) => response.statusCode), |
| 182 completion(equals(401))); | 182 completion(equals(401))); |
| 183 }); | 183 }); |
| 184 | 184 |
| 185 test('passes through a 401 response with non-bearer www-authenticate', () { | 185 test('passes through a 401 response with non-bearer www-authenticate', () { |
| 186 var credentials = new oauth2.Credentials('access token'); | 186 var credentials = new oauth2.Credentials('access token'); |
| 187 var client = new oauth2.Client('identifier', 'secret', credentials, | 187 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 188 httpClient: httpClient); | 188 httpClient: httpClient); |
| 189 | 189 |
| 190 httpClient.expectRequest((request) { | 190 httpClient.expectRequest((request) { |
| 191 expect(request.method, equals('GET')); | 191 expect(request.method, equals('GET')); |
| 192 expect(request.url.toString(), equals(requestUri.toString())); | 192 expect(request.url.toString(), equals(requestUri.toString())); |
| 193 expect(request.headers['authorization'], | 193 expect(request.headers['authorization'], |
| 194 equals('Bearer access token')); | 194 equals('Bearer access token')); |
| 195 | 195 |
| 196 return new Future.immediate(new http.Response('bad job', 401, | 196 return new Future.immediate(new http.Response('bad job', 401, |
| 197 headers: {'www-authenticate': 'Digest'})); | 197 headers: {'www-authenticate': 'Digest'})); |
| 198 }); | 198 }); |
| 199 | 199 |
| 200 expect( | 200 expect( |
| 201 client.get(requestUri).transform((response) => response.statusCode), | 201 client.get(requestUri).then((response) => response.statusCode), |
| 202 completion(equals(401))); | 202 completion(equals(401))); |
| 203 }); | 203 }); |
| 204 | 204 |
| 205 test('passes through a 401 response with non-OAuth2 www-authenticate', () { | 205 test('passes through a 401 response with non-OAuth2 www-authenticate', () { |
| 206 var credentials = new oauth2.Credentials('access token'); | 206 var credentials = new oauth2.Credentials('access token'); |
| 207 var client = new oauth2.Client('identifier', 'secret', credentials, | 207 var client = new oauth2.Client('identifier', 'secret', credentials, |
| 208 httpClient: httpClient); | 208 httpClient: httpClient); |
| 209 | 209 |
| 210 httpClient.expectRequest((request) { | 210 httpClient.expectRequest((request) { |
| 211 expect(request.method, equals('GET')); | 211 expect(request.method, equals('GET')); |
| 212 expect(request.url.toString(), equals(requestUri.toString())); | 212 expect(request.url.toString(), equals(requestUri.toString())); |
| 213 expect(request.headers['authorization'], | 213 expect(request.headers['authorization'], |
| 214 equals('Bearer access token')); | 214 equals('Bearer access token')); |
| 215 | 215 |
| 216 return new Future.immediate(new http.Response('bad job', 401, | 216 return new Future.immediate(new http.Response('bad job', 401, |
| 217 headers: {'www-authenticate': 'Bearer'})); | 217 headers: {'www-authenticate': 'Bearer'})); |
| 218 }); | 218 }); |
| 219 | 219 |
| 220 expect( | 220 expect( |
| 221 client.get(requestUri).transform((response) => response.statusCode), | 221 client.get(requestUri).then((response) => response.statusCode), |
| 222 completion(equals(401))); | 222 completion(equals(401))); |
| 223 }); | 223 }); |
| 224 }); | 224 }); |
| 225 } | 225 } |
| OLD | NEW |