OLD | NEW |
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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 217 |
218 /// <summary>· | 218 /// <summary>· |
219 /// Mock authentication message handler which returns unauthorized respo
nse in the first call, and in the· | 219 /// Mock authentication message handler which returns unauthorized respo
nse in the first call, and in the· |
220 /// second call it returns a successful response. | 220 /// second call it returns a successful response. |
221 /// </summary> | 221 /// </summary> |
222 class MockAuthenticationMessageHandler : CountableMessageHandler | 222 class MockAuthenticationMessageHandler : CountableMessageHandler |
223 { | 223 { |
224 internal static string FirstToken = "invalid"; | 224 internal static string FirstToken = "invalid"; |
225 internal static string SecondToken = "valid"; | 225 internal static string SecondToken = "valid"; |
226 | 226 |
227 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt
pRequestMessage request, | 227 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque
stMessage request, |
228 CancellationToken cancellationToken) | 228 CancellationToken cancellationToken) |
229 { | 229 { |
| 230 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti
onSource<HttpResponseMessage>(); |
230 switch (Calls) | 231 switch (Calls) |
231 { | 232 { |
232 case 1: | 233 case 1: |
233 Assert.That(request.Headers.GetValues("Authorization").C
ount(), Is.EqualTo(1)); | 234 Assert.That(request.Headers.GetValues("Authorization").C
ount(), Is.EqualTo(1)); |
234 Assert.That(request.Headers.GetValues("Authorization").F
irst(), Is.EqualTo(FirstToken)); | 235 Assert.That(request.Headers.GetValues("Authorization").F
irst(), Is.EqualTo(FirstToken)); |
235 return new HttpResponseMessage() { StatusCode = System.N
et.HttpStatusCode.Unauthorized }; | 236 tcs.SetResult(new HttpResponseMessage |
| 237 { |
| 238 StatusCode = System.Net.HttpStatusCode.Unauthori
zed |
| 239 }); |
| 240 break; |
236 case 2: | 241 case 2: |
237 Assert.That(request.Headers.GetValues("Authorization").C
ount(), Is.EqualTo(1)); | 242 Assert.That(request.Headers.GetValues("Authorization").C
ount(), Is.EqualTo(1)); |
238 Assert.That(request.Headers.GetValues("Authorization").F
irst(), Is.EqualTo(SecondToken)); | 243 Assert.That(request.Headers.GetValues("Authorization").F
irst(), Is.EqualTo(SecondToken)); |
239 return new HttpResponseMessage(); | 244 tcs.SetResult(new HttpResponseMessage()); |
| 245 break; |
240 default: | 246 default: |
241 throw new Exception("There should be only two calls"); | 247 throw new Exception("There should be only two calls"); |
242 } | 248 } |
| 249 |
| 250 return tcs.Task; |
243 } | 251 } |
244 } | 252 } |
245 | 253 |
246 /// <summary> Mock Authenticator which adds Authorization header to a re
quest on the second call. </summary> | 254 /// <summary> Mock Authenticator which adds Authorization header to a re
quest on the second call. </summary> |
247 class Authenticator : IAuthenticator | 255 class Authenticator : IAuthenticator |
248 { | 256 { |
249 public int ApplyCalls { get; set; } | 257 public int ApplyCalls { get; set; } |
250 | 258 |
251 public void ApplyAuthenticationToRequest(System.Net.HttpWebRequest r
equest) | 259 public void ApplyAuthenticationToRequest(System.Net.HttpWebRequest r
equest) |
252 { | 260 { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 359 |
352 // one execute interceptor (for adding the "Authenticate" header | 360 // one execute interceptor (for adding the "Authenticate" header |
353 Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors.Co
unt, Is.EqualTo(1)); | 361 Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors.Co
unt, Is.EqualTo(1)); |
354 Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors[0]
, | 362 Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors[0]
, |
355 Is.InstanceOf<AuthenticatorInterceptor>()); | 363 Is.InstanceOf<AuthenticatorInterceptor>()); |
356 } | 364 } |
357 | 365 |
358 #endregion | 366 #endregion |
359 } | 367 } |
360 } | 368 } |
OLD | NEW |