diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-03-02 07:00:34 +0000 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-03-02 07:00:34 +0000 |
commit | 66ff8faccb88d91f08450b07568f6a8a6ea3926e (patch) | |
tree | 1f6673131364dcbf26d1991349113804dfaedcfc | |
parent | 148628ded40775ff8f34175a6f5bad620211d064 (diff) | |
download | DotNetOpenAuth-66ff8faccb88d91f08450b07568f6a8a6ea3926e.zip DotNetOpenAuth-66ff8faccb88d91f08450b07568f6a8a6ea3926e.tar.gz DotNetOpenAuth-66ff8faccb88d91f08450b07568f6a8a6ea3926e.tar.bz2 |
Added a unit test for an end-to-end test of both consumer and provider!
git-svn-id: https://dotnetopenid.googlecode.com/svn/trunk@262 01efa1a6-402a-0410-b0ae-47b76eba00f0
-rw-r--r-- | src/DotNetOpenId.Test/Consumer/OpenIdConsumerTest.cs | 33 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Consumer/OpenIdTextBoxTest.cs | 2 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/AspNetHost.cs | 36 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/HttpHost.cs | 18 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs | 68 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Provider/IdentityEndpointTest.cs | 4 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/TestSupport.cs | 18 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/TestSupportSanityTest.cs | 7 |
8 files changed, 138 insertions, 48 deletions
diff --git a/src/DotNetOpenId.Test/Consumer/OpenIdConsumerTest.cs b/src/DotNetOpenId.Test/Consumer/OpenIdConsumerTest.cs index a80f8d6..1d31b4d 100644 --- a/src/DotNetOpenId.Test/Consumer/OpenIdConsumerTest.cs +++ b/src/DotNetOpenId.Test/Consumer/OpenIdConsumerTest.cs @@ -4,6 +4,8 @@ using System.Text; using NUnit.Framework;
using DotNetOpenId.Consumer;
using System.Collections.Specialized;
+using System.Web;
+using System.Net;
namespace DotNetOpenId.Test.Consumer {
[TestFixture]
@@ -52,19 +54,36 @@ namespace DotNetOpenId.Test.Consumer { }
[Test]
- [Ignore("Not finished")]
public void BasicSimulation() {
- var query = new NameValueCollection();
- var consumer = new OpenIdConsumer(query, store);
-
+ var consumer = new OpenIdConsumer(new NameValueCollection(), store);
+ var trustRoot = new TrustRoot(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri);
+ var returnTo = TestSupport.GetFullUrl(TestSupport.ConsumerPage);
+
Assert.IsNull(consumer.Response);
- var request = consumer.CreateRequest(TestSupport.IdentityUrl, simpleTrustRoot, simpleReturnTo);
+ var request = consumer.CreateRequest(TestSupport.IdentityUrl, trustRoot, returnTo);
// Test defaults
Assert.AreEqual(AuthenticationRequestMode.Setup, request.Mode);
- Assert.AreEqual(simpleReturnTo, request.ReturnToUrl);
- Assert.AreEqual(simpleTrustRoot, request.TrustRootUrl);
+ Assert.AreEqual(returnTo, request.ReturnToUrl);
+ Assert.AreEqual(trustRoot.Url, request.TrustRootUrl.Url);
+
+ // Verify the redirect URL
Assert.IsNotNull(request.RedirectToProviderUrl);
+ var consumerToProviderQuery = HttpUtility.ParseQueryString(request.RedirectToProviderUrl.Query);
+ Assert.IsTrue(consumerToProviderQuery[QueryStringArgs.openid.return_to].StartsWith(returnTo.AbsoluteUri, StringComparison.Ordinal));
+ Assert.AreEqual(trustRoot.Url, consumerToProviderQuery[QueryStringArgs.openid.trust_root]);
+
+ HttpWebRequest providerRequest = (HttpWebRequest)WebRequest.Create(request.RedirectToProviderUrl);
+ providerRequest.AllowAutoRedirect = false;
+ Uri redirectUrl;
+ using (HttpWebResponse providerResponse = (HttpWebResponse)providerRequest.GetResponse()) {
+ Assert.AreEqual(HttpStatusCode.Redirect, providerResponse.StatusCode);
+ redirectUrl = new Uri(providerResponse.Headers[HttpResponseHeader.Location]);
+ }
+ var providerToConsumerQuery = HttpUtility.ParseQueryString(redirectUrl.Query);
+ var consumer2 = new OpenIdConsumer(providerToConsumerQuery, store);
+ Assert.AreEqual(AuthenticationStatus.Authenticated, consumer2.Response.Status);
+ Assert.AreEqual(TestSupport.IdentityUrl, consumer2.Response.IdentityUrl);
}
}
}
diff --git a/src/DotNetOpenId.Test/Consumer/OpenIdTextBoxTest.cs b/src/DotNetOpenId.Test/Consumer/OpenIdTextBoxTest.cs index 0c70d78..3b7276f 100644 --- a/src/DotNetOpenId.Test/Consumer/OpenIdTextBoxTest.cs +++ b/src/DotNetOpenId.Test/Consumer/OpenIdTextBoxTest.cs @@ -10,7 +10,7 @@ namespace DotNetOpenId.Test.Consumer { public class OpenIdTextBoxTest {
[Test]
public void TextBoxAppears() {
- string html = TestSupport.HttpHost.ProcessRequest(TestSupport.ConsumerPage);
+ string html = TestSupport.Host.ProcessRequest(TestSupport.ConsumerPage);
Assert.IsTrue(html.Contains("<input "));
}
}
diff --git a/src/DotNetOpenId.Test/Hosting/AspNetHost.cs b/src/DotNetOpenId.Test/Hosting/AspNetHost.cs index 86ce4cd..aa5f700 100644 --- a/src/DotNetOpenId.Test/Hosting/AspNetHost.cs +++ b/src/DotNetOpenId.Test/Hosting/AspNetHost.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Hosting;
using System.IO;
using System.Diagnostics;
+using System.Net;
namespace DotNetOpenId.Test.Hosting {
/// <summary>
@@ -12,37 +13,36 @@ namespace DotNetOpenId.Test.Hosting { /// Consumer against it.
/// </summary>
class AspNetHost : MarshalByRefObject {
+ HttpHost httpHost;
+
+ public AspNetHost() {
+ httpHost = HttpHost.CreateHost(this);
+ }
+
+ public Uri BaseUri { get { return httpHost.BaseUri; } }
+
public static AspNetHost CreateHost(string webDirectory) {
AspNetHost host = (AspNetHost)ApplicationHost.
CreateApplicationHost(typeof(AspNetHost), "/", webDirectory);
return host;
}
- public string ProcessRequest(string page) {
- return ProcessRequest(page, string.Empty);
+ public string ProcessRequest(string url) {
+ return httpHost.ProcessRequest(url);
}
- public string ProcessRequest(string page, string query) {
- return ProcessRequest(page, query, null);
+ public string ProcessRequest(string url, string body) {
+ return httpHost.ProcessRequest(url, body);
}
- public string ProcessRequest(string page, string query, string body) {
- Trace.TraceInformation("Submitting ASP.NET request: {0}?{1}{2}{3}",
- page, query, Environment.NewLine, body);
- using (TextWriter tw = new StringWriter()) {
- Stream bodyStream = body != null ? new MemoryStream(Encoding.ASCII.GetBytes(body)) : null;
- ProcessRequest(page, query, bodyStream, tw);
- Trace.TraceInformation("Response:{0}{1}", Environment.NewLine, tw);
- return tw.ToString();
+ public void ProcessRequest(HttpListenerContext context) {
+ using (TextWriter tw = new StreamWriter(context.Response.OutputStream)) {
+ HttpRuntime.ProcessRequest(new TestingWorkerRequest(context, tw));
}
}
- public void ProcessRequest(string page, string query, Stream body, TextWriter response) {
- ProcessRequest(new TestingWorkerRequest(page, query, body, response));
- }
-
- public void ProcessRequest(HttpWorkerRequest wr) {
- HttpRuntime.ProcessRequest(wr);
+ public void CloseHttp() {
+ httpHost.Dispose();
}
}
}
diff --git a/src/DotNetOpenId.Test/Hosting/HttpHost.cs b/src/DotNetOpenId.Test/Hosting/HttpHost.cs index f7e0082..49695b1 100644 --- a/src/DotNetOpenId.Test/Hosting/HttpHost.cs +++ b/src/DotNetOpenId.Test/Hosting/HttpHost.cs @@ -13,8 +13,8 @@ namespace DotNetOpenId.Test.Hosting { Thread listenerThread;
AspNetHost aspNetHost;
- HttpHost(string webDirectory) {
- aspNetHost = AspNetHost.CreateHost(webDirectory);
+ HttpHost(AspNetHost aspNetHost) {
+ this.aspNetHost = aspNetHost;
Port = 59687;
Random r = new Random();
@@ -35,20 +35,18 @@ namespace DotNetOpenId.Test.Hosting { listenerThread.Start();
}
+ public static HttpHost CreateHost(AspNetHost aspNetHost) {
+ return new HttpHost(aspNetHost);
+ }
+
public static HttpHost CreateHost(string webDirectory) {
- return new HttpHost(webDirectory);
+ return new HttpHost(AspNetHost.CreateHost(webDirectory));
}
void processRequests() {
try {
while (true) {
var context = listener.GetContext();
- using (TextWriter writer = new StreamWriter(context.Response.OutputStream)) {
- aspNetHost.ProcessRequest(
- context.Request.Url.LocalPath.TrimStart('/'),
- context.Request.Url.Query,
- context.Request.InputStream,
- writer);
- }
+ aspNetHost.ProcessRequest(context);
}
} catch (HttpListenerException) {
// the listener is probably being shut down
diff --git a/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs b/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs index 1e83b0b..c6f3dff 100644 --- a/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs +++ b/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs @@ -3,14 +3,59 @@ using System.Collections.Generic; using System.Text;
using System.Web.Hosting;
using System.IO;
+using System.Diagnostics;
+using System.Net;
namespace DotNetOpenId.Test.Hosting {
class TestingWorkerRequest : SimpleWorkerRequest {
- public TestingWorkerRequest(string page, string query, Stream entityStream, TextWriter output)
- : base(page, query, output) {
+ public TestingWorkerRequest(string page, string query, Stream entityStream, TextWriter writer)
+ : base(page, query, writer) {
this.entityStream = entityStream;
+ this.writer = writer;
}
+ public TestingWorkerRequest(HttpListenerContext context, TextWriter output)
+ : base(context.Request.Url.LocalPath.TrimStart('/'), context.Request.Url.Query, output) {
+ this.entityStream = context.Request.InputStream;
+ this.context = context;
+ this.writer = output;
+ }
+
Stream entityStream;
+ HttpListenerContext context;
+ TextWriter writer;
+ public override int GetLocalPort() {
+ return context.Request.Url.Port;
+ }
+ public override string GetServerName() {
+ return context.Request.Url.Host;
+ }
+ public override string GetQueryString() {
+ return context.Request.Url.Query.TrimStart('?');
+ }
+ public override string GetHttpVerbName() {
+ return context.Request.HttpMethod;
+ }
+ public override string GetLocalAddress() {
+ return context.Request.LocalEndPoint.Address.ToString();
+ }
+ public override string GetHttpVersion() {
+ return "HTTP/1.1";
+ }
+ public override string GetProtocol() {
+ return context.Request.Url.Scheme;
+ }
+ public override string GetRawUrl() {
+ return context.Request.RawUrl;
+ }
+ public override int GetTotalEntityBodyLength() {
+ return (int)context.Request.ContentLength64;
+ }
+ public override string GetKnownRequestHeader(int index) {
+ return context.Request.Headers[GetKnownRequestHeaderName(index)];
+ }
+ public override string GetUnknownRequestHeader(string name) {
+ return context.Request.Headers[name];
+ }
public override bool IsEntireEntityBodyIsPreloaded() {
return false;
}
@@ -20,5 +65,24 @@ namespace DotNetOpenId.Test.Hosting { public override int ReadEntityBody(byte[] buffer, int offset, int size) {
return entityStream.Read(buffer, offset, size);
}
+ public override void SendCalculatedContentLength(int contentLength) {
+ context.Response.ContentLength64 = contentLength;
+ }
+ public override void SendStatus(int statusCode, string statusDescription) {
+ if (context != null) {
+ context.Response.StatusCode = statusCode;
+ context.Response.StatusDescription = statusDescription;
+ }
+ }
+ public override void SendKnownResponseHeader(int index, string value) {
+ if (context != null) {
+ context.Response.Headers[(HttpResponseHeader)index] = value;
+ }
+ }
+ public override void SendUnknownResponseHeader(string name, string value) {
+ if (context != null) {
+ context.Response.Headers[name] = value;
+ }
+ }
}
}
diff --git a/src/DotNetOpenId.Test/Provider/IdentityEndpointTest.cs b/src/DotNetOpenId.Test/Provider/IdentityEndpointTest.cs index 61aac8d..d90b487 100644 --- a/src/DotNetOpenId.Test/Provider/IdentityEndpointTest.cs +++ b/src/DotNetOpenId.Test/Provider/IdentityEndpointTest.cs @@ -14,14 +14,14 @@ namespace DotNetOpenId.Test.Provider { public class IdentityEndpointTest {
[Test]
public void IdentityEndpointPage() {
- string html = TestSupport.HttpHost.ProcessRequest(TestSupport.IdentityPage);
+ string html = TestSupport.Host.ProcessRequest(TestSupport.IdentityPage);
Trace.TraceInformation("{0} response:{1}{2}", TestSupport.IdentityPage, Environment.NewLine, html);
Assert.IsTrue(Regex.IsMatch(html, string.Format(CultureInfo.InvariantCulture,
@"\<link rel=""openid.server"" href=""http://[^/]+/{0}""\>\</link\>",
TestSupport.ProviderPage)));
Assert.IsTrue(Regex.IsMatch(html, string.Format(CultureInfo.InvariantCulture,
@"\<link rel=""openid.delegate"" href=""http://[^/]+{0}""\>\</link\>",
- Regex.Escape(TestSupport.IdentityUrl.AbsolutePath))));
+ Regex.Escape(TestSupport.DelegateUrl.AbsolutePath))));
}
}
}
diff --git a/src/DotNetOpenId.Test/TestSupport.cs b/src/DotNetOpenId.Test/TestSupport.cs index f65d353..3f4af77 100644 --- a/src/DotNetOpenId.Test/TestSupport.cs +++ b/src/DotNetOpenId.Test/TestSupport.cs @@ -14,21 +14,25 @@ public class TestSupport { public const string ProviderPage = "ProviderEndpoint.aspx";
public const string ConsumerPage = "Consumer.aspx";
public static Uri IdentityUrl {
- get {
- return new Uri(HttpHost.BaseUri, "/bob");
- }
+ get { return new Uri(Host.BaseUri, "/IdentityEndpoint.aspx?user=bob"); }
+ }
+ public static Uri DelegateUrl {
+ get { return new Uri(Host.BaseUri, "/bob"); }
+ }
+ public static Uri GetFullUrl(string url) {
+ return new Uri(Host.BaseUri, url);
}
- internal static HttpHost HttpHost { get; private set; }
+ internal static AspNetHost Host { get; private set; }
[SetUp]
public void SetUp() {
- HttpHost = HttpHost.CreateHost(TestSupport.TestWebDirectory);
+ Host = AspNetHost.CreateHost(TestSupport.TestWebDirectory);
}
[TearDown]
public void TearDown() {
- HttpHost.Dispose();
- HttpHost = null;
+ Host.CloseHttp();
+ Host = null;
}
}
diff --git a/src/DotNetOpenId.Test/TestSupportSanityTest.cs b/src/DotNetOpenId.Test/TestSupportSanityTest.cs index 074cfd0..df78dd5 100644 --- a/src/DotNetOpenId.Test/TestSupportSanityTest.cs +++ b/src/DotNetOpenId.Test/TestSupportSanityTest.cs @@ -15,7 +15,7 @@ namespace DotNetOpenId.Test { public void TestHost() {
string query = "a=b&c=d";
string body = "aa=bb&cc=dd";
- string resultHtml = TestSupport.HttpHost.ProcessRequest(TestSupport.HostTestPage + "?" + query, body);
+ string resultHtml = TestSupport.Host.ProcessRequest(TestSupport.HostTestPage + "?" + query, body);
Assert.IsFalse(string.IsNullOrEmpty(resultHtml));
Debug.WriteLine(resultHtml);
@@ -23,5 +23,10 @@ namespace DotNetOpenId.Test { Assert.IsTrue(Regex.IsMatch(resultHtml, @"Body.*" + Regex.Escape(body)));
}
+ [Test]
+ public void TestProviderPage() {
+ string html = TestSupport.Host.ProcessRequest(TestSupport.ProviderPage);
+ Assert.IsFalse(string.IsNullOrEmpty(html));
+ }
}
}
|