summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs')
-rw-r--r--src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs168
1 files changed, 63 insertions, 105 deletions
diff --git a/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs b/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs
index d20671e..349be56 100644
--- a/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs
+++ b/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs
@@ -10,7 +10,9 @@ namespace DotNetOpenAuth.Test.Mocks {
using System.Globalization;
using System.IO;
using System.Net;
+ using System.Net.Http;
using System.Text;
+ using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
@@ -19,71 +21,8 @@ namespace DotNetOpenAuth.Test.Mocks {
using DotNetOpenAuth.Yadis;
using Validation;
- internal class MockHttpRequest {
- private readonly Dictionary<Uri, IncomingWebResponse> registeredMockResponses = new Dictionary<Uri, IncomingWebResponse>();
-
- private MockHttpRequest(IDirectWebRequestHandler mockHandler) {
- Requires.NotNull(mockHandler, "mockHandler");
- 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) {
- Requires.NotNull(response, "response");
- 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) {
- Requires.NotNull(requestUri, "requestUri");
- Requires.NotNull(responseUri, "responseUri");
- Requires.NotNullOrEmpty(contentType, "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) {
+ internal static class MockHttpRequest {
+ internal static void RegisterMockXrdsResponse(this TestBase test, IdentifierDiscoveryResult endpoint) {
Requires.NotNull(endpoint, "endpoint");
string identityUri;
@@ -92,13 +31,14 @@ namespace DotNetOpenAuth.Test.Mocks {
} else {
identityUri = endpoint.UserSuppliedIdentifier ?? endpoint.ClaimedIdentifier;
}
- this.RegisterMockXrdsResponse(new Uri(identityUri), new IdentifierDiscoveryResult[] { endpoint });
+
+ RegisterMockXrdsResponse(test, new Uri(identityUri), new IdentifierDiscoveryResult[] { endpoint });
}
- internal void RegisterMockXrdsResponse(Uri respondingUri, IEnumerable<IdentifierDiscoveryResult> endpoints) {
+ internal static void RegisterMockXrdsResponse(this TestBase test, Uri respondingUri, IEnumerable<IdentifierDiscoveryResult> endpoints) {
Requires.NotNull(endpoints, "endpoints");
- StringBuilder xrds = new StringBuilder();
+ var 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) {
@@ -127,10 +67,10 @@ namespace DotNetOpenAuth.Test.Mocks {
</XRD>
</xrds:XRDS>");
- this.RegisterMockResponse(respondingUri, ContentTypes.Xrds, xrds.ToString());
+ test.Handle(respondingUri).By(xrds.ToString(), ContentTypes.Xrds);
}
- internal void RegisterMockXrdsResponse(UriIdentifier directedIdentityAssignedIdentifier, IdentifierDiscoveryResult providerEndpoint) {
+ internal static void RegisterMockXrdsResponse(this TestBase test, UriIdentifier directedIdentityAssignedIdentifier, IdentifierDiscoveryResult providerEndpoint) {
IdentifierDiscoveryResult identityEndpoint = IdentifierDiscoveryResult.CreateForClaimedIdentifier(
directedIdentityAssignedIdentifier,
directedIdentityAssignedIdentifier,
@@ -138,16 +78,16 @@ namespace DotNetOpenAuth.Test.Mocks {
new ProviderEndpointDescription(providerEndpoint.ProviderEndpoint, providerEndpoint.Capabilities),
10,
10);
- this.RegisterMockXrdsResponse(identityEndpoint);
+ RegisterMockXrdsResponse(test, 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 static void RegisterMockXrdsResponse(this TestBase test, string embeddedResourcePath, out Identifier id) {
+ id = new Uri(new Uri("http://localhost/"), embeddedResourcePath);
+ test.Handle(new Uri(id))
+ .By(OpenIdTestBase.LoadEmbeddedFile(embeddedResourcePath), "application/xrds+xml");
}
- internal void RegisterMockRPDiscovery() {
+ internal static void RegisterMockRPDiscovery(this TestBase test, bool ssl) {
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'>
@@ -164,44 +104,62 @@ namespace DotNetOpenAuth.Test.Mocks {
HttpUtility.HtmlEncode(OpenIdTestBase.RPRealmUri.AbsoluteUri),
HttpUtility.HtmlEncode(OpenIdTestBase.RPRealmUriSsl.AbsoluteUri));
- this.RegisterMockResponse(OpenIdTestBase.RPRealmUri, ContentTypes.Xrds, xrds);
- this.RegisterMockResponse(OpenIdTestBase.RPRealmUriSsl, ContentTypes.Xrds, xrds);
+ test.Handle(ssl ? OpenIdTestBase.RPRealmUriSsl : OpenIdTestBase.RPRealmUri)
+ .By(xrds, ContentTypes.Xrds);
}
- internal void DeleteResponse(Uri requestUri) {
- this.registeredMockResponses.Remove(requestUri);
+ internal static void RegisterMockRedirect(this TestBase test, Uri origin, Uri redirectLocation) {
+ var response = new HttpResponseMessage(HttpStatusCode.Redirect);
+ response.Headers.Location = redirectLocation;
+ test.Handle(origin).By(req => response);
}
- 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 static void RegisterMockXrdsResponses(this TestBase test, IEnumerable<KeyValuePair<string, string>> urlXrdsPairs) {
+ Requires.NotNull(urlXrdsPairs, "urlXrdsPairs");
+
+ foreach (var keyValuePair in urlXrdsPairs) {
+ test.Handle(new Uri(keyValuePair.Key)).By(keyValuePair.Value, ContentTypes.Xrds);
+ }
}
- 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);
+ internal static void RegisterMockResponse(this TestBase test, Uri url, string contentType, string content) {
+ test.Handle(url).By(content, contentType);
}
- 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);
+ internal static void RegisterMockResponse(this TestBase test, Uri requestUri, Uri responseUri, string contentType, string content) {
+ RegisterMockResponse(test, requestUri, responseUri, contentType, null, content);
+ }
+
+ internal static void RegisterMockResponse(this TestBase test, Uri requestUri, Uri responseUri, string contentType, WebHeaderCollection headers, string content) {
+ Requires.NotNull(requestUri, "requestUri");
+ Requires.NotNull(responseUri, "responseUri");
+ Requires.NotNullOrEmpty(contentType, "contentType");
+
+ test.Handle(requestUri).By(req => {
+ var response = new HttpResponseMessage();
+ response.RequestMessage = req;
+
+ if (requestUri != responseUri) {
+ // Simulate having followed redirects to get the final response.
+ var clonedRequest = MessagingUtilities.Clone(req);
+ clonedRequest.RequestUri = responseUri;
+ response.RequestMessage = clonedRequest;
+ }
+
+ response.CopyHeadersFrom(headers);
+ response.Content = new StringContent(content, Encoding.Default, contentType);
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());
+ });
+ }
+
+ private static void CopyHeadersFrom(this HttpResponseMessage message, WebHeaderCollection headers) {
+ if (headers != null) {
+ foreach (string headerName in headers) {
+ string[] headerValues = headers.GetValues(headerName);
+ if (!message.Headers.TryAddWithoutValidation(headerName, headerValues)) {
+ message.Content.Headers.TryAddWithoutValidation(headerName, headerValues);
+ }
+ }
}
}
}