Index: Src/GoogleApis.Tests/Apis/Http/ConfigurableMessageHandlerTest.cs
===================================================================
--- a/Src/GoogleApis.Tests/Apis/Http/ConfigurableMessageHandlerTest.cs
+++ b/Src/GoogleApis.Tests/Apis/Http/ConfigurableMessageHandlerTest.cs
@@ -33,22 +33,24 @@
namespace Google.Apis.Tests.Apis.Http
{
- /// Tests for .
+ /// Tests for .
[TestFixture]
public class ConfigurableMessageHandlerTest
{
#region Handlers
- /// Unsuccessful handler which always returns true.
+ /// Unsuccessful handler which always returns true.
private class TrueUnsuccessfulResponseHandler : IHttpUnsuccessfulResponseHandler
{
- public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
+ public Task HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
{
- return true;
+ TaskCompletionSource tcs = new TaskCompletionSource();
+ tcs.SetResult(true);
+ return tcs.Task;
}
}
- /// Message handler which returns a new successful (and empty) response.
+ /// Message handler which returns a new successful (and empty) response.
private class MockMessageHandler : HttpMessageHandler
{
protected override Task SendAsync(HttpRequestMessage request,
@@ -64,13 +66,13 @@
#region Redirect
- /// Redirect message handler which return redirect response.
+ /// Redirect message handler which return redirect response.
private class RedirectMessageHandler : CountableMessageHandler
{
- /// Gets or sets the redirect location Uri string.
+ /// Gets or sets the redirect location Uri string.
private string Location { get; set; }
- /// Constructs a new redirect message handler with the given location.
+ /// Constructs a new redirect message handler with the given location.
public RedirectMessageHandler(string location)
{
Location = location;
@@ -110,7 +112,7 @@
}
}
- /// Tests that the message handler handles redirect messages successfully.
+ /// Tests that the message handler handles redirect messages successfully.
[Test]
public void SendAsync_Redirect()
{
@@ -136,7 +138,7 @@
}
}
- ///
+ ///
/// Tests that the message handler doesn't handle redirect messages when follow redirect is false.
///
[Test]
@@ -171,12 +173,12 @@
#region Execute interceptor
- ///
+ ///
/// Mock interceptor handler which verifies that an interceptor is being called on a request.
///
private class InterceptorMessageHandler : CountableMessageHandler
{
- /// Gets or sets an injected response message which will be returned on send.
+ /// Gets or sets an injected response message which will be returned on send.
public HttpResponseMessage InjectedResponseMessage { get; set; }
const string InjectedHeader = "Some-Header";
@@ -192,27 +194,28 @@
return tcs.Task;
}
- /// A mock interceptor which inject a header to a request.
+ /// A mock interceptor which inject a header to a request.
internal class Interceptor : IHttpExecuteInterceptor
{
public int Calls { get; set; }
- public void Intercept(HttpRequestMessage request)
+ public Task InterceptAsync(HttpRequestMessage request, CancellationToken token)
{
++Calls;
request.Headers.Add(InjectedHeader, InjectedValue);
+ return TaskEx.Delay(0);
}
}
}
- /// Tests that execute interceptor is called on successful response.
+ /// Tests that execute interceptor is called on successful response.
[Test]
public void SendAsync_ExecuteInterceptor()
{
SubtestSendAsyncExecuteInterceptor(HttpStatusCode.OK);
}
- ///
+ ///
/// Tests that execute interceptor is called once on unsuccessful request. In this test unsuccessful response
/// handler isn't plugged to the handler.
///
@@ -222,7 +225,7 @@
SubtestSendAsyncExecuteInterceptor(HttpStatusCode.BadRequest);
}
- /// Tests that execute interceptor is called.
+ /// Tests that execute interceptor is called.
private void SubtestSendAsyncExecuteInterceptor(HttpStatusCode code)
{
var handler = new InterceptorMessageHandler();
@@ -245,7 +248,7 @@
}
}
- ///
+ ///
/// Tests that execute interceptor is called for each request. In this case an unsuccessful response handler is
/// plugged to the handler
///
@@ -277,18 +280,18 @@
#region Unsuccessful reponse handler
- ///
+ ///
/// Mock unsuccessful response handler which verifies that unsuccessful response handler is being called.
///
private class UnsuccessfulResponseMessageHandler : CountableMessageHandler
{
- /// Gets or sets the status code to return on the response.
+ /// Gets or sets the status code to return on the response.
public HttpStatusCode ResponseStatusCode { get; set; }
- /// Gets or sets the cancellation token source.
+ /// Gets or sets the cancellation token source.
public CancellationTokenSource CancellationTokenSource { get; set; }
- ///
+ ///
/// Gets or sets the request number to invoke the Cancel method on .
///
public int CancelRequestNum { get; set; }
@@ -306,20 +309,22 @@
return tcs.Task;
}
- /// Unsuccessful response handler which "handles" only service unavailable responses.
+ /// Unsuccessful response handler which "handles" only service unavailable responses.
internal class ServiceUnavailableResponseHandler : IHttpUnsuccessfulResponseHandler
{
public int Calls { get; set; }
- public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
+ public Task HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
{
++Calls;
- return args.Response.StatusCode.Equals(HttpStatusCode.ServiceUnavailable);
+ TaskCompletionSource tcs = new TaskCompletionSource();
+ tcs.SetResult(args.Response.StatusCode.Equals(HttpStatusCode.ServiceUnavailable));
+ return tcs.Task;
}
}
}
- /// Test helper for testing unsuccessful response handlers.
+ /// Test helper for testing unsuccessful response handlers.
private void SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode code)
{
var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = code };
@@ -351,14 +356,14 @@
}
}
- /// Tests that unsuccessful response handler isn't called when the response is successful.
+ /// Tests that unsuccessful response handler isn't called when the response is successful.
[Test]
public void SendAsync_UnsuccessfulReponseHanlder_SuccessfulReponse()
{
SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode.OK);
}
- ///
+ ///
/// Tests that unsuccessful response handler is called when the response is unsuccessful, but the handler can't
/// handle the abnormal response (e.g. different status code).
///
@@ -368,7 +373,7 @@
SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode.BadGateway);
}
- ///
+ ///
/// Tests that unsuccessful response handler is called when the response is unsuccessful and the handler can
/// handle the abnormal response (e.g. same status code).
///
@@ -378,7 +383,7 @@
SubtestSendAsyncUnsuccessfulReponseHanlder(HttpStatusCode.ServiceUnavailable);
}
- /// Tests abnormal response when unsuccessful response handler isn't plugged.
+ /// Tests abnormal response when unsuccessful response handler isn't plugged.
[Test]
public void SendAsync_AbnormalResponse_WithoutUnsuccessfulReponseHandler()
{
@@ -402,7 +407,7 @@
#region Exception Handler
- /// Mock exception message handler which verifies that exception handler is being called.
+ /// Mock exception message handler which verifies that exception handler is being called.
private class ExceptionMessageHandler : CountableMessageHandler
{
public ExceptionMessageHandler()
@@ -410,15 +415,15 @@
Exception = new Exception(ExceptionMessage);
}
- /// Gets or sets indication if exception should be thrown.
+ /// Gets or sets indication if exception should be thrown.
public bool ThrowException { get; set; }
- ///
+ ///
/// Gets or sets a specific exception to throw. Default value is
- /// with .
+ /// with .
public Exception Exception { get; set; }
- ///
+ ///
/// The exception message which is thrown in case is true.
///
public const string ExceptionMessage = "Exception from execute";
@@ -436,7 +441,7 @@
return tcs.Task;
}
- /// Mock Exception handler which "handles" the exception.
+ /// Mock Exception handler which "handles" the exception.
internal class ExceptionHandler : IHttpExceptionHandler
{
public int Calls { get; set; }
@@ -447,15 +452,17 @@
Handle = handle;
}
- public bool HandleException(HandleExceptionArgs args)
+ public Task HandleExceptionAsync(HandleExceptionArgs args)
{
++Calls;
- return Handle;
+ TaskCompletionSource tcs = new TaskCompletionSource();
+ tcs.SetResult(Handle);
+ return tcs.Task;
}
}
}
- /// Subtest for exception handler which tests that exception handler is invoked.
+ /// Subtest for exception handler which tests that exception handler is invoked.
private void SubtestSendAsyncExceptionHandler(bool throwException, bool handle)
{
var handler = new ExceptionMessageHandler { ThrowException = throwException };
@@ -497,14 +504,14 @@
}
- /// Tests that the exception handler isn't called on successful response.
+ /// Tests that the exception handler isn't called on successful response.
[Test]
public void SendAsync_ExceptionHandler_SuccessReponse()
{
SubtestSendAsyncExceptionHandler(false, true);
}
- ///
+ ///
/// Tests that the exception handler is called when exception is thrown on execute, but it can't handle the
/// exception.
///
@@ -514,7 +521,7 @@
SubtestSendAsyncExceptionHandler(true, false);
}
- ///
+ ///
/// Tests that the exception handler is called when exception is thrown on execute, and it handles the
/// exception.
///
@@ -524,7 +531,7 @@
SubtestSendAsyncExceptionHandler(true, true);
}
- /// Tests an exception is thrown on execute and there is no exception handler.
+ /// Tests an exception is thrown on execute and there is no exception handler.
[Test]
public void SendAsync_ThrowException_WithoutExceptionHandler()
{
@@ -558,7 +565,7 @@
#region Exception
- ///
+ ///
/// Tests that back-off handler works as expected when exception is thrown.
/// Use default max time span (2 minutes).
///
@@ -570,7 +577,7 @@
SubtestSendAsync_BackOffExceptionHandler(true, initializer);
}
- ///
+ ///
/// Tests that back-off handler works as expected when exception is thrown.
/// Max time span is set to 200 milliseconds (as a result the back-off handler can't handle the exception).
///
@@ -584,7 +591,7 @@
SubtestSendAsync_BackOffExceptionHandler(true, initializer);
}
- ///
+ ///
/// Tests that back-off handler works as expected when exception is thrown.
/// Max time span is set to 1 hour.
///
@@ -598,7 +605,7 @@
SubtestSendAsync_BackOffExceptionHandler(true, initializer);
}
- ///
+ ///
/// Tests that back-off handler works as expected when
/// > is thrown.
///
@@ -623,7 +630,7 @@
SubtestSendAsync_BackOffExceptionHandler(true, initializer, new InvalidCastException());
}
- /// Tests that back-off handler works as expected when exception isn't thrown.
+ /// Tests that back-off handler works as expected when exception isn't thrown.
[Test]
public void SendAsync_BackOffExceptionHandler_DontThrow()
{
@@ -631,7 +638,7 @@
SubtestSendAsync_BackOffExceptionHandler(false, initializer);
}
- /// Subtest that back-off handler works as expected when exception is or isn't thrown.
+ /// Subtest that back-off handler works as expected when exception is or isn't thrown.
private void SubtestSendAsync_BackOffExceptionHandler(bool throwException,
BackOffHandler.Initializer initializer, Exception exceptionToThrow = null)
{
@@ -686,7 +693,7 @@
#region Unsuccessful Response Handler
- ///
+ ///
/// Tests that back-off handler works as expected when the server returns 5xx and the maximum time span is set
/// to 5 seconds.
///
@@ -700,7 +707,7 @@
SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer);
}
- ///
+ ///
/// Tests that back-off handler works as expected when the server returns 5xx and the maximum time span is set
/// to 10 hours.
///
@@ -714,7 +721,7 @@
SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer);
}
- ///
+ ///
/// Tests that back-off handler isn't be called when the server returns a successful response.
///
[Test]
@@ -724,7 +731,7 @@
SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.OK, initializer);
}
- /// Tests that back-off handler is canceled when cancellation token is used.
+ /// Tests that back-off handler is canceled when cancellation token is used.
[Test]
public void SendAsync_BackOffUnsuccessfulResponseHandler_Cancel()
{
@@ -737,7 +744,7 @@
SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer, 6);
}
- ///
+ ///
/// Subtest that back-off handler works as expected when a successful or abnormal response is returned.
/// For testing the back-off handler in case of a canceled request, set the cancelRequestNum
/// parameter to the index of the request you want to cancel.
@@ -813,7 +820,7 @@
#region Content
- /// Mock message handler which verifies that the content is correct on retry.
+ /// Mock message handler which verifies that the content is correct on retry.
private class ContentMessageHandler : CountableMessageHandler
{
public const int NumFails = 4;
@@ -832,7 +839,7 @@
}
}
- ///
+ ///
/// Defines the different content types we test in .
///
private enum ContentType
@@ -842,7 +849,7 @@
ByteArray
}
- /// Tests that retry works with different kind of contents (String, Stream and ByteArray).
+ /// Tests that retry works with different kind of contents (String, Stream and ByteArray).
private void SubtestSendAsyncRetryContent(ContentType type)
{
var content = "test-content";
@@ -882,21 +889,21 @@
}
}
- /// Tests that a string content works as expected on retry.
+ /// Tests that a string content works as expected on retry.
[Test]
public void SendAsync_Retry_CorrectStringContent()
{
SubtestSendAsyncRetryContent(ContentType.String);
}
- /// Tests that a stream content works as expected on retry.
+ /// Tests that a stream content works as expected on retry.
[Test]
public void SendAsync_Retry_CorrectStreamContent()
{
SubtestSendAsyncRetryContent(ContentType.Stream);
}
- /// Tests that a byte array content works as expected on retry.
+ /// Tests that a byte array content works as expected on retry.
[Test]
public void SendAsync_Retry_CorrectByteArrayContent()
{
@@ -905,7 +912,7 @@
#endregion
- /// Tests setting number of tries.
+ /// Tests setting number of tries.
[Test]
public void NumTries_Setter()
{
@@ -946,7 +953,7 @@
}
}
- ///
+ ///
/// Tests the number of tries in case of unsuccessful response when unsuccessful response handler is plugged to
/// the message handler.
///
@@ -990,7 +997,7 @@
}
}
- /// Tests that the configurable message handler sets the User-Agent header.
+ /// Tests that the configurable message handler sets the User-Agent header.
[Test]
public void SendAsync_UserAgent()
{