diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-03-01 19:35:20 +0000 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-03-01 19:35:20 +0000 |
commit | 1965d9a01603da8b0599f05f8fbd076195e07021 (patch) | |
tree | 5ca6a8d96d8e75f54729bc56d7c939ca55ed45bb | |
parent | 2e6fddf39c6649710b5a874791dfcc133217516e (diff) | |
download | DotNetOpenAuth-1965d9a01603da8b0599f05f8fbd076195e07021.zip DotNetOpenAuth-1965d9a01603da8b0599f05f8fbd076195e07021.tar.gz DotNetOpenAuth-1965d9a01603da8b0599f05f8fbd076195e07021.tar.bz2 |
Added an HTTP hosted version of ASP.NET hosting.
This allows Consumer tests to run and internally call up the OpenID endpoint server and have that also be hosted by the test framework.
git-svn-id: https://dotnetopenid.googlecode.com/svn/trunk@257 01efa1a6-402a-0410-b0ae-47b76eba00f0
-rw-r--r-- | src/DotNetOpenId.Test/DotNetOpenId.Test.csproj | 5 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting.Tests/AspNetHostTest.cs | 36 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting.Tests/HttpHostTest.cs | 48 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/AspNetHost.cs (renamed from src/DotNetOpenId.Test/Hosting/Host.cs) | 12 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/HttpHost.cs | 55 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs | 15 | ||||
-rw-r--r-- | src/DotNetOpenId.Test/Provider/OpenIdProviderTest.cs | 18 |
7 files changed, 161 insertions, 28 deletions
diff --git a/src/DotNetOpenId.Test/DotNetOpenId.Test.csproj b/src/DotNetOpenId.Test/DotNetOpenId.Test.csproj index 98eb23f..d605512 100644 --- a/src/DotNetOpenId.Test/DotNetOpenId.Test.csproj +++ b/src/DotNetOpenId.Test/DotNetOpenId.Test.csproj @@ -43,7 +43,10 @@ <Compile Include="Consumer\OpenIdConsumerTest.cs" />
<Compile Include="DictionarySerializerTestSuite.cs" />
<Compile Include="DiffieHellmanTestSuite.cs" />
- <Compile Include="Hosting\Host.cs" />
+ <Compile Include="Hosting.Tests\AspNetHostTest.cs" />
+ <Compile Include="Hosting.Tests\HttpHostTest.cs" />
+ <Compile Include="Hosting\AspNetHost.cs" />
+ <Compile Include="Hosting\HttpHost.cs" />
<Compile Include="Hosting\TestingWorkerRequest.cs" />
<Compile Include="ProfileFieldValuesTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
diff --git a/src/DotNetOpenId.Test/Hosting.Tests/AspNetHostTest.cs b/src/DotNetOpenId.Test/Hosting.Tests/AspNetHostTest.cs new file mode 100644 index 0000000..f36d042 --- /dev/null +++ b/src/DotNetOpenId.Test/Hosting.Tests/AspNetHostTest.cs @@ -0,0 +1,36 @@ +using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using System.IO;
+using System.Diagnostics;
+using DotNetOpenId.Test.Hosting;
+using System.Text.RegularExpressions;
+
+namespace DotNetOpenId.Test.Hosting.Tests {
+ [TestFixture]
+ public class AspNetHostTest {
+ public static readonly string TestWebDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\src\DotNetOpenId.TestWeb"));
+ public const string HostTestPage = "HostTest.aspx";
+ AspNetHost host;
+
+ [SetUp]
+ public void SetUpHost() {
+ host = AspNetHost.CreateHost(TestWebDirectory);
+ }
+
+ [Test]
+ public void TestHost() {
+ StringWriter sw = new StringWriter();
+ string query = "a=b&c=d";
+ string body = "aa=bb&cc=dd";
+ Stream bodyStream = new MemoryStream(Encoding.ASCII.GetBytes(body));
+ host.ProcessRequest(HostTestPage,query, bodyStream, sw);
+ string resultHtml = sw.ToString();
+ Assert.IsFalse(string.IsNullOrEmpty(resultHtml));
+ Debug.WriteLine(resultHtml);
+ Assert.IsTrue(Regex.IsMatch(resultHtml, @"Query.*" + Regex.Escape(query)));
+ Assert.IsTrue(Regex.IsMatch(resultHtml, @"Body.*" + Regex.Escape(body)));
+ }
+ }
+}
diff --git a/src/DotNetOpenId.Test/Hosting.Tests/HttpHostTest.cs b/src/DotNetOpenId.Test/Hosting.Tests/HttpHostTest.cs new file mode 100644 index 0000000..3b8bee6 --- /dev/null +++ b/src/DotNetOpenId.Test/Hosting.Tests/HttpHostTest.cs @@ -0,0 +1,48 @@ +using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using System.Net;
+using System.Globalization;
+using System.IO;
+using System.Diagnostics;
+using System.Text.RegularExpressions;
+
+namespace DotNetOpenId.Test.Hosting.Tests {
+ [TestFixture]
+ public class HttpHostTest {
+ HttpHost host;
+ [SetUp]
+ public void HostSetUp() {
+ host = new HttpHost(AspNetHostTest.TestWebDirectory);
+ }
+
+ [TearDown]
+ public void HostTearDown() {
+ host.Dispose();
+ }
+
+ [Test]
+ public void TestHost() {
+ string query = "a=b&c=d";
+ string body = "aa=bb&cc=dd";
+
+ WebRequest request = WebRequest.Create(string.Format(CultureInfo.InvariantCulture,
+ "http://localhost:{0}/{1}?{2}", host.Port, AspNetHostTest.HostTestPage, query));
+ request.Method = "POST";
+ request.ContentLength = body.Length;
+ using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
+ sw.Write(body);
+
+ WebResponse response = request.GetResponse();
+ string resultHtml;
+ using (StreamReader sr = new StreamReader(response.GetResponseStream()))
+ resultHtml = sr.ReadToEnd();
+ Assert.IsFalse(string.IsNullOrEmpty(resultHtml));
+ Debug.WriteLine(resultHtml);
+ Assert.IsTrue(Regex.IsMatch(resultHtml, @"Query.*" + Regex.Escape(query)));
+ Assert.IsTrue(Regex.IsMatch(resultHtml, @"Body.*" + Regex.Escape(body)));
+ }
+
+ }
+}
diff --git a/src/DotNetOpenId.Test/Hosting/Host.cs b/src/DotNetOpenId.Test/Hosting/AspNetHost.cs index a1e97ad..5785016 100644 --- a/src/DotNetOpenId.Test/Hosting/Host.cs +++ b/src/DotNetOpenId.Test/Hosting/AspNetHost.cs @@ -10,19 +10,19 @@ namespace DotNetOpenId.Test.Hosting { /// Hosts a 'portable' version of the OpenIdProvider for testing itself and the
/// Consumer against it.
/// </summary>
- class Host : MarshalByRefObject {
- public static Host CreateHost(string webDirectory) {
- Host host = (Host)System.Web.Hosting.ApplicationHost.
- CreateApplicationHost(typeof(Host), "/", webDirectory);
+ class AspNetHost : MarshalByRefObject {
+ public static AspNetHost CreateHost(string webDirectory) {
+ AspNetHost host = (AspNetHost)System.Web.Hosting.ApplicationHost.
+ CreateApplicationHost(typeof(AspNetHost), "/", webDirectory);
return host;
}
- public void ProcessRequest(string page, string query, string body, TextWriter responseWriter) {
+ public void ProcessRequest(string page, string query, Stream body, TextWriter responseWriter) {
ProcessRequest(new TestingWorkerRequest(page, query, body, responseWriter));
}
public void ProcessRequest(HttpWorkerRequest wr) {
- System.Web.HttpRuntime.ProcessRequest(wr);
+ HttpRuntime.ProcessRequest(wr);
}
}
}
diff --git a/src/DotNetOpenId.Test/Hosting/HttpHost.cs b/src/DotNetOpenId.Test/Hosting/HttpHost.cs new file mode 100644 index 0000000..8dd45f4 --- /dev/null +++ b/src/DotNetOpenId.Test/Hosting/HttpHost.cs @@ -0,0 +1,55 @@ +using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Net;
+using System.Globalization;
+using System.Threading;
+using System.IO;
+
+namespace DotNetOpenId.Test.Hosting {
+ class HttpHost : IDisposable {
+ HttpListener listener;
+ public int Port { get; private set; }
+ Thread listenerThread;
+ AspNetHost aspNetHost;
+
+ public HttpHost(string webDirectory) {
+ aspNetHost = AspNetHost.CreateHost(webDirectory);
+
+ listener = new HttpListener();
+ Port = 59687;
+ listener.Prefixes.Add(string.Format(CultureInfo.InvariantCulture,
+ "http://localhost:{0}/", Port));
+ listener.Start();
+ listenerThread = new Thread(processRequests);
+ listenerThread.Start();
+ }
+
+ 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);
+ }
+ }
+ } catch (HttpListenerException) {
+ // the listener is probably being shut down
+ }
+ }
+
+ #region IDisposable Members
+
+ public void Dispose() {
+ listener.Close();
+ listenerThread.Join(1000);
+ listenerThread.Abort();
+ }
+
+ #endregion
+ }
+}
diff --git a/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs b/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs index 55d7532..1e83b0b 100644 --- a/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs +++ b/src/DotNetOpenId.Test/Hosting/TestingWorkerRequest.cs @@ -6,16 +6,19 @@ using System.IO; namespace DotNetOpenId.Test.Hosting {
class TestingWorkerRequest : SimpleWorkerRequest {
- public TestingWorkerRequest(string page, string query, string body, TextWriter output)
+ public TestingWorkerRequest(string page, string query, Stream entityStream, TextWriter output)
: base(page, query, output) {
- this.body = body;
+ this.entityStream = entityStream;
}
- string body;
+ Stream entityStream;
public override bool IsEntireEntityBodyIsPreloaded() {
- return true;
+ return false;
}
- public override byte[] GetPreloadedEntityBody() {
- return Encoding.ASCII.GetBytes(body);
+ public override int ReadEntityBody(byte[] buffer, int size) {
+ return entityStream.Read(buffer, 0, size);
+ }
+ public override int ReadEntityBody(byte[] buffer, int offset, int size) {
+ return entityStream.Read(buffer, offset, size);
}
}
}
diff --git a/src/DotNetOpenId.Test/Provider/OpenIdProviderTest.cs b/src/DotNetOpenId.Test/Provider/OpenIdProviderTest.cs index 15a3cfe..95d8cff 100644 --- a/src/DotNetOpenId.Test/Provider/OpenIdProviderTest.cs +++ b/src/DotNetOpenId.Test/Provider/OpenIdProviderTest.cs @@ -6,29 +6,17 @@ using System.IO; using System.Diagnostics;
using DotNetOpenId.Test.Hosting;
using System.Text.RegularExpressions;
+using DotNetOpenId.Test.Hosting.Tests;
namespace DotNetOpenId.Test.Provider {
[TestFixture]
public class OpenIdProviderTest {
- static readonly string webDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\src\DotNetOpenId.TestWeb"));
- Host host;
+ AspNetHost host;
[SetUp]
public void SetUpHost() {
- host = Host.CreateHost(webDirectory);
+ host = AspNetHost.CreateHost(AspNetHostTest.TestWebDirectory);
}
- [Test]
- public void TestHost() {
- StringWriter sw = new StringWriter();
- string query = "a=b&c=d";
- string body = "aa=bb&cc=dd";
- host.ProcessRequest("hosttest.aspx",query, body, sw);
- string resultHtml = sw.ToString();
- Assert.IsFalse(string.IsNullOrEmpty(resultHtml));
- Debug.WriteLine(resultHtml);
- Assert.IsTrue(Regex.IsMatch(resultHtml, @"Query.*" + Regex.Escape(query)));
- Assert.IsTrue(Regex.IsMatch(resultHtml, @"Body.*" + Regex.Escape(body)));
- }
}
}
|