summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/Mocks
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2009-12-15 22:17:20 -0800
committerAndrew Arnott <andrewarnott@gmail.com>2009-12-15 22:17:20 -0800
commite12782c1a6727390b2107ff2e39d4ac6173d86fc (patch)
tree3be0ccda0a9425927263f5b6b9616ef8ba11ac08 /src/DotNetOpenId.Test/Mocks
parent078b1f350eb40ceee7423c25b1d833dd1f242da4 (diff)
parenta545f7be2693596fa14540c359e43150a6a7cf88 (diff)
downloadDotNetOpenAuth-origin/mono.zip
DotNetOpenAuth-origin/mono.tar.gz
DotNetOpenAuth-origin/mono.tar.bz2
Merge branch 'v2.5' into monoorigin/mono
Conflicts: src/DotNetOpenId/Properties/AssemblyInfo.cs src/DotNetOpenId/RelyingParty/AuthenticationResponse.cs
Diffstat (limited to 'src/DotNetOpenId.Test/Mocks')
-rw-r--r--src/DotNetOpenId.Test/Mocks/DirectMessageSniffWrapper.cs41
-rw-r--r--src/DotNetOpenId.Test/Mocks/DirectMessageTestRedirector.cs31
-rw-r--r--src/DotNetOpenId.Test/Mocks/MockHttpRequest.cs175
-rw-r--r--src/DotNetOpenId.Test/Mocks/MockIdentifier.cs58
4 files changed, 305 insertions, 0 deletions
diff --git a/src/DotNetOpenId.Test/Mocks/DirectMessageSniffWrapper.cs b/src/DotNetOpenId.Test/Mocks/DirectMessageSniffWrapper.cs
new file mode 100644
index 0000000..1bcbfb9
--- /dev/null
+++ b/src/DotNetOpenId.Test/Mocks/DirectMessageSniffWrapper.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using DotNetOpenId.RelyingParty;
+
+namespace DotNetOpenId.Test.Mocks {
+ class DirectMessageSniffWrapper : IDirectMessageChannel {
+ IDirectMessageChannel channel;
+
+ internal DirectMessageSniffWrapper(IDirectMessageChannel channel) {
+ this.channel = channel;
+ }
+
+ internal event Action<ServiceEndpoint, IDictionary<string, string>> Sending;
+ internal event Action<ServiceEndpoint, IDictionary<string, string>> Receiving;
+
+ protected virtual void OnSending(ServiceEndpoint provider, IDictionary<string, string> fields) {
+ var sending = Sending;
+ if (sending != null) {
+ sending(provider, fields);
+ }
+ }
+
+ protected virtual void OnReceiving(ServiceEndpoint provider, IDictionary<string, string> fields) {
+ var receiving = Receiving;
+ if (receiving != null) {
+ receiving(provider, fields);
+ }
+ }
+
+ #region IDirectMessageChannel Members
+
+ public IDictionary<string, string> SendDirectMessageAndGetResponse(ServiceEndpoint provider, IDictionary<string, string> fields) {
+ OnSending(provider, fields);
+ var results = channel.SendDirectMessageAndGetResponse(provider, fields);
+ OnReceiving(provider, results);
+ return results;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenId.Test/Mocks/DirectMessageTestRedirector.cs b/src/DotNetOpenId.Test/Mocks/DirectMessageTestRedirector.cs
new file mode 100644
index 0000000..c63e2de
--- /dev/null
+++ b/src/DotNetOpenId.Test/Mocks/DirectMessageTestRedirector.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using DotNetOpenId.Provider;
+using DotNetOpenId.RelyingParty;
+using IProviderAssociationStore = DotNetOpenId.IAssociationStore<DotNetOpenId.AssociationRelyingPartyType>;
+
+namespace DotNetOpenId.Test.Mocks {
+ class DirectMessageTestRedirector : IDirectMessageChannel {
+
+ IProviderAssociationStore providerStore;
+
+ public DirectMessageTestRedirector(IProviderAssociationStore providerStore) {
+ if (providerStore == null) throw new ArgumentNullException("providerStore");
+ this.providerStore = providerStore;
+ }
+
+ #region IDirectMessageChannel Members
+
+ public IDictionary<string, string> SendDirectMessageAndGetResponse(ServiceEndpoint providerEndpoint, IDictionary<string, string> fields) {
+ OpenIdProvider provider = new OpenIdProvider(providerStore, providerEndpoint.ProviderEndpoint,
+ providerEndpoint.ProviderEndpoint, fields.ToNameValueCollection());
+ Debug.Assert(provider.Request.IsResponseReady, "Direct messages should always have an immediately available response.");
+ Response webResponse = (Response)provider.Request.Response;
+ EncodableResponse opAuthResponse = (EncodableResponse)webResponse.EncodableMessage;
+ return opAuthResponse.EncodedFields;
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenId.Test/Mocks/MockHttpRequest.cs b/src/DotNetOpenId.Test/Mocks/MockHttpRequest.cs
new file mode 100644
index 0000000..1a0547c
--- /dev/null
+++ b/src/DotNetOpenId.Test/Mocks/MockHttpRequest.cs
@@ -0,0 +1,175 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Net;
+using DotNetOpenId.RelyingParty;
+using DotNetOpenId.Yadis;
+using NUnit.Framework;
+using System.Diagnostics;
+using System.Web;
+using System.Text;
+
+namespace DotNetOpenId.Test.Mocks {
+ class MockHttpRequest {
+ static Dictionary<Uri, UntrustedWebResponse> registeredMockResponses = new Dictionary<Uri, UntrustedWebResponse>();
+
+ static UntrustedWebResponse MockRequestResponse(Uri uri, byte[] body, string[] acceptTypes) {
+ UntrustedWebResponse response;
+ if (registeredMockResponses.TryGetValue(uri, 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);
+ return new UntrustedWebResponse(uri, uri, new WebHeaderCollection(), HttpStatusCode.NotFound,
+ "text/html", null, new MemoryStream());
+ }
+ }
+
+ /// <summary>
+ /// Clears all all mock HTTP responses and deactivates HTTP mocking.
+ /// </summary>
+ internal static void Reset() {
+ UntrustedWebRequest.MockRequests = null;
+ registeredMockResponses.Clear();
+ }
+
+ internal static void RegisterMockResponse(UntrustedWebResponse response) {
+ if (response == null) throw new ArgumentNullException("response");
+ UntrustedWebRequest.MockRequests = MockRequestResponse;
+ if (registeredMockResponses.ContainsKey(response.RequestUri)) {
+ TestSupport.Logger.WarnFormat("Mock HTTP response already registered for {0}.", response.RequestUri);
+ } else {
+ registeredMockResponses.Add(response.RequestUri, response);
+ }
+ }
+
+ internal static void RegisterMockResponse(Uri requestUri, string contentType, string responseBody) {
+ RegisterMockResponse(requestUri, requestUri, contentType, responseBody);
+ }
+
+ internal static void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, string responseBody) {
+ RegisterMockResponse(requestUri, responseUri, contentType, new WebHeaderCollection(), responseBody);
+ }
+
+ internal static void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, WebHeaderCollection headers, string responseBody) {
+ if (requestUri == null) throw new ArgumentNullException("requestUri");
+ if (responseUri == null) throw new ArgumentNullException("responseUri");
+ if (String.IsNullOrEmpty(contentType)) throw new ArgumentNullException("contentType");
+
+ // Set up the redirect if appropriate
+ if (requestUri != responseUri) {
+ 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);
+ RegisterMockResponse(new UntrustedWebResponse(responseUri, responseUri, headers ?? new WebHeaderCollection(),
+ HttpStatusCode.OK, contentType, contentEncoding, stream));
+ }
+
+ internal static void RegisterMockXrdsResponses(IDictionary<string, string> requestUriAndResponseBody) {
+ foreach (var pair in requestUriAndResponseBody) {
+ RegisterMockResponse(new Uri(pair.Key), "text/xml; saml=false; https=false; charset=UTF-8", pair.Value);
+ }
+ }
+
+ internal static void RegisterMockXrdsResponse(ServiceEndpoint endpoint) {
+ if (endpoint == null) throw new ArgumentNullException("endpoint");
+
+ string identityUri;
+ if (endpoint.ClaimedIdentifier == endpoint.Protocol.ClaimedIdentifierForOPIdentifier) {
+ identityUri = endpoint.UserSuppliedIdentifier;
+ } else {
+ identityUri = endpoint.UserSuppliedIdentifier ?? endpoint.ClaimedIdentifier;
+ }
+ RegisterMockXrdsResponse(new Uri(identityUri), new ServiceEndpoint[] { endpoint });
+ }
+
+ internal static void RegisterMockXrdsResponse(Uri respondingUri, IEnumerable<ServiceEndpoint> endpoints) {
+ if (endpoints == null) throw new ArgumentNullException("endpoints");
+
+ 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>");
+
+ RegisterMockResponse(respondingUri, ContentTypes.Xrds, xrds.ToString());
+ }
+ internal static void RegisterMockXrdsResponse(UriIdentifier directedIdentityAssignedIdentifier, ServiceEndpoint providerEndpoint) {
+ ServiceEndpoint identityEndpoint = ServiceEndpoint.CreateForClaimedIdentifier(
+ directedIdentityAssignedIdentifier,
+ directedIdentityAssignedIdentifier,
+ providerEndpoint.ProviderEndpoint,
+ new string[] { providerEndpoint.Protocol.ClaimedIdentifierServiceTypeURI },
+ 10,
+ 10
+ );
+ RegisterMockXrdsResponse(identityEndpoint);
+ }
+ internal static Identifier RegisterMockXrdsResponse(string embeddedResourcePath) {
+ UriIdentifier id = TestSupport.GetFullUrl(embeddedResourcePath);
+ RegisterMockResponse(id, "application/xrds+xml", TestSupport.LoadEmbeddedFile(embeddedResourcePath));
+ return id;
+ }
+ internal static void RegisterMockRPDiscovery() {
+ Uri rpRealmUri = TestSupport.Realm.UriWithWildcardChangedToWww;
+
+ 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>
+ </Service>
+ </XRD>
+</xrds:XRDS>";
+ string xrds = string.Format(CultureInfo.InvariantCulture, template,
+ HttpUtility.HtmlEncode(Protocol.v20.RPReturnToTypeURI),
+ HttpUtility.HtmlEncode(rpRealmUri.AbsoluteUri)
+ );
+
+ RegisterMockResponse(rpRealmUri, ContentTypes.Xrds, xrds);
+ }
+
+ internal static void DeleteResponse(Uri requestUri) {
+ registeredMockResponses.Remove(requestUri);
+ }
+
+ internal static void RegisterMockRedirect(Uri origin, Uri redirectLocation) {
+ var redirectionHeaders = new WebHeaderCollection {
+ { HttpResponseHeader.Location, redirectLocation.AbsoluteUri },
+ };
+ UntrustedWebResponse response = new UntrustedWebResponse(origin, origin,
+ redirectionHeaders, HttpStatusCode.Redirect, null, null, new MemoryStream());
+ RegisterMockResponse(response);
+ }
+ }
+}
diff --git a/src/DotNetOpenId.Test/Mocks/MockIdentifier.cs b/src/DotNetOpenId.Test/Mocks/MockIdentifier.cs
new file mode 100644
index 0000000..7ab2d74
--- /dev/null
+++ b/src/DotNetOpenId.Test/Mocks/MockIdentifier.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using DotNetOpenId.RelyingParty;
+
+namespace DotNetOpenId.Test.Mocks {
+ /// <summary>
+ /// Performs similar to an ordinary <see cref="Identifier"/>, but when called upon
+ /// to perform discovery, it returns a preset list of sevice endpoints to avoid
+ /// having a dependency on a hosted web site to actually perform discovery on.
+ /// </summary>
+ class MockIdentifier : Identifier {
+ IEnumerable<ServiceEndpoint> endpoints;
+ Identifier wrappedIdentifier;
+
+ public MockIdentifier(Identifier wrappedIdentifier, IEnumerable<ServiceEndpoint> endpoints)
+ : base(false) {
+ if (wrappedIdentifier == null) throw new ArgumentNullException("wrappedIdentifier");
+ if (endpoints == null) throw new ArgumentNullException("endpoints");
+ this.wrappedIdentifier = wrappedIdentifier;
+ this.endpoints = endpoints;
+
+ // Register a mock HTTP response to enable discovery of this identifier within the RP
+ // without having to host an ASP.NET site within the test.
+ MockHttpRequest.RegisterMockXrdsResponse(new Uri(wrappedIdentifier.ToString()), endpoints);
+ }
+
+ internal override IEnumerable<ServiceEndpoint> Discover() {
+ return endpoints;
+ }
+
+ internal override Identifier TrimFragment() {
+ return this;
+ }
+
+ internal override bool TryRequireSsl(out Identifier secureIdentifier) {
+ // We take special care to make our wrapped identifier secure, but still
+ // return a mocked (secure) identifier.
+ Identifier secureWrappedIdentifier;
+ bool result = wrappedIdentifier.TryRequireSsl(out secureWrappedIdentifier);
+ secureIdentifier = new MockIdentifier(secureWrappedIdentifier, endpoints);
+ return result;
+ }
+
+ public override string ToString() {
+ return wrappedIdentifier.ToString();
+ }
+
+ public override bool Equals(object obj) {
+ return wrappedIdentifier.Equals(obj);
+ }
+
+ public override int GetHashCode() {
+ return wrappedIdentifier.GetHashCode();
+ }
+ }
+}