diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-17 18:12:49 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-17 18:12:49 -0700 |
commit | 2eb59292e2a2f22866d95684606334845d8997e5 (patch) | |
tree | c2a6743f536b1691667c43848988c75294ec78b0 | |
parent | 2d2ef93da49ae2f9963e83b67b845fc41824f229 (diff) | |
download | DotNetOpenAuth-2eb59292e2a2f22866d95684606334845d8997e5.zip DotNetOpenAuth-2eb59292e2a2f22866d95684606334845d8997e5.tar.gz DotNetOpenAuth-2eb59292e2a2f22866d95684606334845d8997e5.tar.bz2 |
Adds missing file.
-rw-r--r-- | src/DotNetOpenAuth.Test/MockingHostFactories.cs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/MockingHostFactories.cs b/src/DotNetOpenAuth.Test/MockingHostFactories.cs new file mode 100644 index 0000000..d1f0792 --- /dev/null +++ b/src/DotNetOpenAuth.Test/MockingHostFactories.cs @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------- +// <copyright file="MockingHostFactories.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test { + using System.Collections.Generic; + using System.Net; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + using System.Linq; + + using Validation; + + internal class MockingHostFactories : IHostFactories { + private readonly List<CoordinatorBase.Handler> handlers; + + public MockingHostFactories(List<CoordinatorBase.Handler> handlers = null) { + this.handlers = handlers ?? new List<CoordinatorBase.Handler>(); + } + + public List<CoordinatorBase.Handler> Handlers { + get { return this.handlers; } + } + + public HttpMessageHandler CreateHttpMessageHandler() { + return new ForwardingMessageHandler(this.handlers, this); + } + + public HttpClient CreateHttpClient(HttpMessageHandler handler = null) { + return new HttpClient(handler ?? this.CreateHttpMessageHandler()); + } + + private class ForwardingMessageHandler : HttpMessageHandler { + private readonly IEnumerable<CoordinatorBase.Handler> handlers; + + private readonly IHostFactories hostFactories; + + public ForwardingMessageHandler(IEnumerable<CoordinatorBase.Handler> handlers, IHostFactories hostFactories) { + Requires.NotNull(handlers, "handlers"); + + this.handlers = handlers; + this.hostFactories = hostFactories; + } + + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + foreach (var handler in this.handlers) { + if (handler.Uri.IsBaseOf(request.RequestUri) && handler.Uri.AbsolutePath == request.RequestUri.AbsolutePath) { + var response = await handler.MessageHandler(this.hostFactories, request, cancellationToken); + if (response != null) { + return response; + } + } + } + + return new HttpResponseMessage(HttpStatusCode.NotFound); + } + } + } +}
\ No newline at end of file |