LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 { | 50 { |
51 // ClientSecrets are missing. | 51 // ClientSecrets are missing. |
52 try | 52 try |
53 { | 53 { |
54 new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer( | 54 new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer( |
55 "https://authorization_code.com", "https://token.com")); | 55 "https://authorization_code.com", "https://token.com")); |
56 Assert.Fail(); | 56 Assert.Fail(); |
57 } | 57 } |
58 catch (ArgumentException ex) | 58 catch (ArgumentException ex) |
59 { | 59 { |
60 Assert.True(ex.Message.Contains("client secret or client secret
stream MUST be set"), | 60 Assert.True(ex.Message.Contains("You MUST set ClientSecret or Cl
ientSecretStream")); |
61 "User MUST specify client secrets!"); | |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 [Test] | 64 [Test] |
66 public void TestConstructor_DefaultValues() | 65 public void TestConstructor_DefaultValues() |
67 { | 66 { |
68 var flow = CreateFlow(); | 67 var flow = CreateFlow(); |
69 Assert.NotNull(flow.AccessMethod); | 68 Assert.NotNull(flow.AccessMethod); |
70 Assert.That(flow.AccessMethod, Is.InstanceOf<BearerToken.Authorizati
onHeaderAccessMethod>()); | 69 Assert.That(flow.AccessMethod, Is.InstanceOf<BearerToken.Authorizati
onHeaderAccessMethod>()); |
71 Assert.That(flow.AuthorizationServerUrl, Is.EqualTo("https://authori
zation.com")); | 70 Assert.That(flow.AuthorizationServerUrl, Is.EqualTo("https://authori
zation.com")); |
72 Assert.NotNull(flow.ClientSecrets); | 71 Assert.NotNull(flow.ClientSecrets); |
73 Assert.That(flow.ClientSecrets.ClientId, Is.EqualTo("id")); | 72 Assert.That(flow.ClientSecrets.ClientId, Is.EqualTo("id")); |
74 Assert.That(flow.ClientSecrets.ClientSecret, Is.EqualTo("secret")); | 73 Assert.That(flow.ClientSecrets.ClientSecret, Is.EqualTo("secret")); |
75 Assert.That(flow.Clock, Is.InstanceOf<SystemClock>()); | 74 Assert.That(flow.Clock, Is.InstanceOf<SystemClock>()); |
76 Assert.Null(flow.DataStore); | 75 Assert.Null(flow.DataStore); |
77 Assert.NotNull(flow.HttpClient); | 76 Assert.NotNull(flow.HttpClient); |
78 Assert.NotNull(flow.Scopes); | 77 Assert.NotNull(flow.Scopes); |
79 Assert.That(flow.TokenServerEncodedUrl, Is.EqualTo("https://token.co
m")); | 78 Assert.That(flow.TokenServerUrl, Is.EqualTo("https://token.com")); |
80 | 79 |
81 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl
ers.Count(), Is.EqualTo(1)); | 80 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl
ers.Count(), Is.EqualTo(1)); |
82 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl
ers.First(), | 81 Assert.That(flow.HttpClient.MessageHandler.UnsuccessfulResponseHandl
ers.First(), |
83 Is.InstanceOf<BackOffHandler>()); | 82 Is.InstanceOf<BackOffHandler>()); |
84 } | 83 } |
85 | 84 |
86 #endregion | 85 #endregion |
87 | 86 |
88 #region LoadToken | 87 #region LoadToken |
89 | 88 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 handler.RefreshTokenRequest = new RefreshTokenRequest() | 355 handler.RefreshTokenRequest = new RefreshTokenRequest() |
357 { | 356 { |
358 RefreshToken = "REFRESH", | 357 RefreshToken = "REFRESH", |
359 Scope = "a" | 358 Scope = "a" |
360 }; | 359 }; |
361 handler.Error = true; | 360 handler.Error = true; |
362 SubtestFetchTokenAsync_Error(handler); | 361 SubtestFetchTokenAsync_Error(handler); |
363 } | 362 } |
364 | 363 |
365 /// <summary>Subtest for receiving an error token response.</summary> | 364 /// <summary>Subtest for receiving an error token response.</summary> |
366 /// <param name="handler">The message handler</param> | 365 /// <param name="handler">The message handler.</param> |
367 private void SubtestFetchTokenAsync_Error(FetchTokenMessageHandler handl
er) | 366 private void SubtestFetchTokenAsync_Error(FetchTokenMessageHandler handl
er) |
368 { | 367 { |
369 MockHttpClientFactory mockFactory = new MockHttpClientFactory(handle
r); | 368 MockHttpClientFactory mockFactory = new MockHttpClientFactory(handle
r); |
370 var flow = CreateFlow(httpClientFactory: mockFactory); | 369 var flow = CreateFlow(httpClientFactory: mockFactory); |
371 try | 370 try |
372 { | 371 { |
373 var request = | 372 var request = |
374 (TokenRequest)handler.AuthorizationCodeTokenRequest ?? (Toke
nRequest)handler.RefreshTokenRequest; | 373 (TokenRequest)handler.AuthorizationCodeTokenRequest ?? (Toke
nRequest)handler.RefreshTokenRequest; |
375 var result = flow.FetchTokenAsync("user", request, CancellationT
oken.None).Result; | 374 var result = flow.FetchTokenAsync("user", request, CancellationT
oken.None).Result; |
376 Assert.Fail(); | 375 Assert.Fail(); |
377 } | 376 } |
378 catch (AggregateException aex) | 377 catch (AggregateException aex) |
379 { | 378 { |
380 var ex = aex.InnerException as TokenResponseException; | 379 var ex = aex.InnerException as TokenResponseException; |
381 Assert.IsNotNull(ex); | 380 Assert.IsNotNull(ex); |
382 var result = ex.Error; | 381 var result = ex.Error; |
383 Assert.That(result.Error, Is.EqualTo("error")); | 382 Assert.That(result.Error, Is.EqualTo("error")); |
384 Assert.That(result.ErrorDescription, Is.EqualTo("desc")); | 383 Assert.That(result.ErrorDescription, Is.EqualTo("desc")); |
385 Assert.That(result.ErrorUri, Is.EqualTo("uri")); | 384 Assert.That(result.ErrorUri, Is.EqualTo("uri")); |
386 } | 385 } |
387 } | 386 } |
388 | 387 |
389 #endregion | 388 #endregion |
390 | 389 |
391 /// <summary>Creates an authorization code flow with the given parameter
s.</summary> | 390 /// <summary>Creates an authorization code flow with the given parameter
s.</summary> |
392 /// <param name="dataStore">The data store.</param> | 391 /// <param name="dataStore">The data store.</param> |
393 /// <param name="scopes">The Scopes</param> | 392 /// <param name="scopes">The Scopes.</param> |
394 /// <param name="httpClientFactory">The HTTP client factory. If not set
the default will be used</param> | 393 /// <param name="httpClientFactory">The HTTP client factory. If not set
the default will be used.</param> |
395 /// <returns>Authorization code flow</returns> | 394 /// <returns>Authorization code flow</returns> |
396 private AuthorizationCodeFlow CreateFlow(IDataStore dataStore = null, IE
numerable<string> scopes = null, | 395 private AuthorizationCodeFlow CreateFlow(IDataStore dataStore = null, IE
numerable<string> scopes = null, |
397 IHttpClientFactory httpClientFactory = null) | 396 IHttpClientFactory httpClientFactory = null) |
398 { | 397 { |
399 var secrets = new ClientSecrets() { ClientId = "id", ClientSecret =
"secret" }; | 398 var secrets = new ClientSecrets() { ClientId = "id", ClientSecret =
"secret" }; |
400 var initializer = new AuthorizationCodeFlow.Initializer(Authorizatio
nCodeUrl, TokenUrl) | 399 var initializer = new AuthorizationCodeFlow.Initializer(Authorizatio
nCodeUrl, TokenUrl) |
401 { | 400 { |
402 ClientSecrets = secrets, | 401 ClientSecrets = secrets, |
403 HttpClientFactory = httpClientFactory | 402 HttpClientFactory = httpClientFactory |
404 }; | 403 }; |
(...skipping 12 matching lines...) Expand all Loading... |
417 /// <summary>Verifies that the token response contains the expected data
.</summary> | 416 /// <summary>Verifies that the token response contains the expected data
.</summary> |
418 /// <param name="response">The token response</param> | 417 /// <param name="response">The token response</param> |
419 private void SubtestTokenResponse(TokenResponse response) | 418 private void SubtestTokenResponse(TokenResponse response) |
420 { | 419 { |
421 Assert.That(response.RefreshToken, Is.EqualTo("r")); | 420 Assert.That(response.RefreshToken, Is.EqualTo("r")); |
422 Assert.That(response.ExpiresInSeconds, Is.EqualTo(100)); | 421 Assert.That(response.ExpiresInSeconds, Is.EqualTo(100)); |
423 Assert.That(response.Scope, Is.EqualTo("b")); | 422 Assert.That(response.Scope, Is.EqualTo("b")); |
424 } | 423 } |
425 } | 424 } |
426 } | 425 } |
LEFT | RIGHT |