summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/CoordinatorBase.cs
blob: 48c63713c7380c5d9ee1f75c84606a7e75b68fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//-----------------------------------------------------------------------
// <copyright file="CoordinatorBase.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test {
	using System;
	using System.Collections.Generic;
	using System.Net;
	using System.Net.Http;
	using System.Net.Http.Headers;
	using System.Threading;
	using System.Threading.Tasks;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OpenId.Provider;
	using DotNetOpenAuth.OpenId.RelyingParty;
	using DotNetOpenAuth.Test.Mocks;
	using DotNetOpenAuth.Test.OpenId;

	using NUnit.Framework;
	using Validation;

	using System.Linq;

	internal class CoordinatorBase {
		private Func<IHostFactories, CancellationToken, Task> driver;

		internal CoordinatorBase(Func<IHostFactories, CancellationToken, Task> driver, params Handler[] handlers) {
			Requires.NotNull(driver, "driver");
			Requires.NotNull(handlers, "handlers");

			this.driver = driver;
			this.HostFactories = new MockingHostFactories(handlers.ToList());
		}

		internal MockingHostFactories HostFactories { get; set; }

		internal static Task RunAsync(Func<IHostFactories, CancellationToken, Task> driver, params Handler[] handlers) {
			var coordinator = new CoordinatorBase(driver, handlers);
			return coordinator.RunAsync();
		}

		protected internal virtual async Task RunAsync(CancellationToken cancellationToken = default(CancellationToken)) {
			await this.driver(this.HostFactories, cancellationToken);
		}

		internal static Handler Handle(Uri uri) {
			return new Handler(uri);
		}

		internal struct Handler {
			internal Handler(Uri uri)
				: this() {
				this.Uri = uri;
			}

			public Uri Uri { get; private set; }

			public Func<IHostFactories, HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> MessageHandler { get; private set; }

			internal Handler By(Func<IHostFactories, HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler) {
				return new Handler(this.Uri) { MessageHandler = handler };
			}

			internal Handler By(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler) {
				return By((hf, req, ct) => handler(req, ct));
			}

			internal Handler By(Func<HttpRequestMessage, HttpResponseMessage> handler) {
				return By((req, ct) => Task.FromResult(handler(req)));
			}

			internal Handler By(string responseContent, string contentType, HttpStatusCode statusCode = HttpStatusCode.OK) {
				return By(
					req => {
						var response = new HttpResponseMessage(statusCode);
						response.Content = new StringContent(responseContent);
						response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
						return response;
					});
			}
		}
	}
}