summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs
blob: c18ea333ebf5e32b8e27e2f599263692da201e64 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//-----------------------------------------------------------------------
// <copyright file="MockHttpRequest.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Mocks {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics.Contracts;
	using System.Globalization;
	using System.IO;
	using System.Net;
	using System.Text;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OpenId;
	using DotNetOpenAuth.OpenId.RelyingParty;
	using DotNetOpenAuth.Test.OpenId;
	using DotNetOpenAuth.Yadis;

	internal class MockHttpRequest {
		private readonly Dictionary<Uri, IncomingWebResponse> registeredMockResponses = new Dictionary<Uri, IncomingWebResponse>();

		private MockHttpRequest(IDirectWebRequestHandler mockHandler) {
			Contract.Requires<ArgumentNullException>(mockHandler != null);
			this.MockWebRequestHandler = mockHandler;
		}

		internal IDirectWebRequestHandler MockWebRequestHandler { get; private set; }

		internal static MockHttpRequest CreateUntrustedMockHttpHandler() {
			TestWebRequestHandler testHandler = new TestWebRequestHandler();
			UntrustedWebRequestHandler untrustedHandler = new UntrustedWebRequestHandler(testHandler);
			if (!untrustedHandler.WhitelistHosts.Contains("localhost")) {
				untrustedHandler.WhitelistHosts.Add("localhost");
			}
			untrustedHandler.WhitelistHosts.Add(OpenIdTestBase.OPUri.Host);
			MockHttpRequest mock = new MockHttpRequest(untrustedHandler);
			testHandler.Callback = mock.GetMockResponse;
			return mock;
		}

		internal void RegisterMockResponse(IncomingWebResponse response) {
			Contract.Requires<ArgumentNullException>(response != null);
			if (this.registeredMockResponses.ContainsKey(response.RequestUri)) {
				Logger.Http.WarnFormat("Mock HTTP response already registered for {0}.", response.RequestUri);
			} else {
				this.registeredMockResponses.Add(response.RequestUri, response);
			}
		}

		internal void RegisterMockResponse(Uri requestUri, string contentType, string responseBody) {
			this.RegisterMockResponse(requestUri, requestUri, contentType, responseBody);
		}

		internal void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, string responseBody) {
			this.RegisterMockResponse(requestUri, responseUri, contentType, new WebHeaderCollection(), responseBody);
		}

		internal void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, WebHeaderCollection headers, string responseBody) {
			Contract.Requires<ArgumentNullException>(requestUri != null);
			Contract.Requires<ArgumentNullException>(responseUri != null);
			Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(contentType));

			// Set up the redirect if appropriate
			if (requestUri != responseUri) {
				this.RegisterMockRedirect(requestUri, responseUri);
			}

			string contentEncoding = null;
			MemoryStream stream = new MemoryStream();
			StreamWriter sw = new StreamWriter(stream);
			sw.Write(responseBody);
			sw.Flush();
			stream.Seek(0, SeekOrigin.Begin);
			this.RegisterMockResponse(new CachedDirectWebResponse(responseUri, responseUri, headers ?? new WebHeaderCollection(), HttpStatusCode.OK, contentType, contentEncoding, stream));
		}

		internal void RegisterMockXrdsResponses(IDictionary<string, string> requestUriAndResponseBody) {
			foreach (var pair in requestUriAndResponseBody) {
				this.RegisterMockResponse(new Uri(pair.Key), "text/xml; saml=false; https=false; charset=UTF-8", pair.Value);
			}
		}

		internal void RegisterMockXrdsResponse(IdentifierDiscoveryResult endpoint) {
			Contract.Requires<ArgumentNullException>(endpoint != null);

			string identityUri;
			if (endpoint.ClaimedIdentifier == endpoint.Protocol.ClaimedIdentifierForOPIdentifier) {
				identityUri = endpoint.UserSuppliedIdentifier;
			} else {
				identityUri = endpoint.UserSuppliedIdentifier ?? endpoint.ClaimedIdentifier;
			}
			this.RegisterMockXrdsResponse(new Uri(identityUri), new IdentifierDiscoveryResult[] { endpoint });
		}

		internal void RegisterMockXrdsResponse(Uri respondingUri, IEnumerable<IdentifierDiscoveryResult> endpoints) {
			Contract.Requires<ArgumentNullException>(endpoints != null);

			StringBuilder xrds = new StringBuilder();
			xrds.AppendLine(@"<xrds:XRDS xmlns:xrds='xri://$xrds' xmlns:openid='http://openid.net/xmlns/1.0' xmlns='xri://$xrd*($v*2.0)'>
	<XRD>");
			foreach (var endpoint in endpoints) {
				string template = @"
		<Service priority='10'>
			<Type>{0}</Type>
			<URI>{1}</URI>
			<LocalID>{2}</LocalID>
			<openid:Delegate xmlns:openid='http://openid.net/xmlns/1.0'>{2}</openid:Delegate>
		</Service>";
				string serviceTypeUri;
				if (endpoint.ClaimedIdentifier == endpoint.Protocol.ClaimedIdentifierForOPIdentifier) {
					serviceTypeUri = endpoint.Protocol.OPIdentifierServiceTypeURI;
				} else {
					serviceTypeUri = endpoint.Protocol.ClaimedIdentifierServiceTypeURI;
				}
				string xrd = string.Format(
					CultureInfo.InvariantCulture,
					template,
					HttpUtility.HtmlEncode(serviceTypeUri),
					HttpUtility.HtmlEncode(endpoint.ProviderEndpoint.AbsoluteUri),
					HttpUtility.HtmlEncode(endpoint.ProviderLocalIdentifier));
				xrds.Append(xrd);
			}
			xrds.Append(@"
	</XRD>
</xrds:XRDS>");

			this.RegisterMockResponse(respondingUri, ContentTypes.Xrds, xrds.ToString());
		}

		internal void RegisterMockXrdsResponse(UriIdentifier directedIdentityAssignedIdentifier, IdentifierDiscoveryResult providerEndpoint) {
			IdentifierDiscoveryResult identityEndpoint = IdentifierDiscoveryResult.CreateForClaimedIdentifier(
				directedIdentityAssignedIdentifier,
				directedIdentityAssignedIdentifier,
				providerEndpoint.ProviderLocalIdentifier,
				new ProviderEndpointDescription(providerEndpoint.ProviderEndpoint, providerEndpoint.Capabilities),
				10,
				10);
			this.RegisterMockXrdsResponse(identityEndpoint);
		}

		internal Identifier RegisterMockXrdsResponse(string embeddedResourcePath) {
			UriIdentifier id = new Uri(new Uri("http://localhost/"), embeddedResourcePath);
			this.RegisterMockResponse(id, "application/xrds+xml", OpenIdTestBase.LoadEmbeddedFile(embeddedResourcePath));
			return id;
		}

		internal void RegisterMockRPDiscovery() {
			string template = @"<xrds:XRDS xmlns:xrds='xri://$xrds' xmlns:openid='http://openid.net/xmlns/1.0' xmlns='xri://$xrd*($v*2.0)'>
	<XRD>
		<Service priority='10'>
			<Type>{0}</Type>
			<URI>{1}</URI>
			<URI>{2}</URI>
		</Service>
	</XRD>
</xrds:XRDS>";
			string xrds = string.Format(
				CultureInfo.InvariantCulture,
				template,
				HttpUtility.HtmlEncode(Protocol.V20.RPReturnToTypeURI),
				HttpUtility.HtmlEncode(OpenIdTestBase.RPRealmUri.AbsoluteUri),
				HttpUtility.HtmlEncode(OpenIdTestBase.RPRealmUriSsl.AbsoluteUri));

			this.RegisterMockResponse(OpenIdTestBase.RPRealmUri, ContentTypes.Xrds, xrds);
			this.RegisterMockResponse(OpenIdTestBase.RPRealmUriSsl, ContentTypes.Xrds, xrds);
		}

		internal void DeleteResponse(Uri requestUri) {
			this.registeredMockResponses.Remove(requestUri);
		}

		internal void RegisterMockRedirect(Uri origin, Uri redirectLocation) {
			var redirectionHeaders = new WebHeaderCollection {
				{ HttpResponseHeader.Location, redirectLocation.AbsoluteUri },
			};
			IncomingWebResponse response = new CachedDirectWebResponse(origin, origin, redirectionHeaders, HttpStatusCode.Redirect, null, null, new MemoryStream());
			this.RegisterMockResponse(response);
		}

		internal void RegisterMockNotFound(Uri requestUri) {
			CachedDirectWebResponse errorResponse = new CachedDirectWebResponse(
				requestUri,
				requestUri,
				new WebHeaderCollection(),
				HttpStatusCode.NotFound,
				"text/plain",
				Encoding.UTF8.WebName,
				new MemoryStream(Encoding.UTF8.GetBytes("Not found.")));
			this.RegisterMockResponse(errorResponse);
		}

		private IncomingWebResponse GetMockResponse(HttpWebRequest request) {
			IncomingWebResponse response;
			if (this.registeredMockResponses.TryGetValue(request.RequestUri, out response)) {
				// reset response stream position so this response can be reused on a subsequent request.
				response.ResponseStream.Seek(0, SeekOrigin.Begin);
				return response;
			} else {
				////Assert.Fail("Unexpected HTTP request: {0}", uri);
				Logger.Http.WarnFormat("Unexpected HTTP request: {0}", request.RequestUri);
				return new CachedDirectWebResponse(request.RequestUri, request.RequestUri, new WebHeaderCollection(), HttpStatusCode.NotFound, "text/html", null, new MemoryStream());
			}
		}
	}
}