summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/MockingHostFactories.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOpenAuth.Test/MockingHostFactories.cs')
-rw-r--r--src/DotNetOpenAuth.Test/MockingHostFactories.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/MockingHostFactories.cs b/src/DotNetOpenAuth.Test/MockingHostFactories.cs
new file mode 100644
index 0000000..b8cbeb0
--- /dev/null
+++ b/src/DotNetOpenAuth.Test/MockingHostFactories.cs
@@ -0,0 +1,83 @@
+//-----------------------------------------------------------------------
+// <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 DotNetOpenAuth.OpenId;
+
+ using Validation;
+ using System;
+
+ internal class MockingHostFactories : IHostFactories {
+ public MockingHostFactories(Dictionary<Uri, Func<HttpRequestMessage, Task<HttpResponseMessage>>> handlers = null) {
+ this.Handlers = handlers ?? new Dictionary<Uri, Func<HttpRequestMessage, Task<HttpResponseMessage>>>();
+ this.CookieContainer = new CookieContainer();
+ this.AllowAutoRedirects = true;
+ }
+
+ public Dictionary<Uri, Func<HttpRequestMessage, Task<HttpResponseMessage>>> Handlers { get; private set; }
+
+ public CookieContainer CookieContainer { get; set; }
+
+ public bool AllowAutoRedirects { get; set; }
+
+ public bool InstallUntrustedWebReqestHandler { get; set; }
+
+ public HttpMessageHandler CreateHttpMessageHandler() {
+ var forwardingMessageHandler = new ForwardingMessageHandler(this.Handlers, this);
+ var cookieDelegatingHandler = new CookieDelegatingHandler(forwardingMessageHandler, this.CookieContainer);
+ if (this.InstallUntrustedWebReqestHandler) {
+ var untrustedHandler = new UntrustedWebRequestHandler(cookieDelegatingHandler);
+ untrustedHandler.AllowAutoRedirect = this.AllowAutoRedirects;
+ return untrustedHandler;
+ } else if (this.AllowAutoRedirects) {
+ return new AutoRedirectHandler(cookieDelegatingHandler);
+ } else {
+ return cookieDelegatingHandler;
+ }
+ }
+
+ public HttpClient CreateHttpClient(HttpMessageHandler handler = null) {
+ return new HttpClient(handler ?? this.CreateHttpMessageHandler());
+ }
+
+ private class ForwardingMessageHandler : HttpMessageHandler {
+ private readonly Dictionary<Uri, Func<HttpRequestMessage, Task<HttpResponseMessage>>> handlers;
+
+ private readonly IHostFactories hostFactories;
+
+ public ForwardingMessageHandler(Dictionary<Uri, Func<HttpRequestMessage, Task<HttpResponseMessage>>> 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 pair in this.handlers) {
+ if (pair.Key.IsBaseOf(request.RequestUri) && pair.Key.AbsolutePath == request.RequestUri.AbsolutePath) {
+ var response = await pair.Value(request);
+ if (response != null) {
+ if (response.RequestMessage == null) {
+ response.RequestMessage = request;
+ }
+
+ return response;
+ }
+ }
+ }
+
+ return new HttpResponseMessage(HttpStatusCode.NotFound);
+ }
+ }
+ }
+} \ No newline at end of file