diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-11-24 19:44:57 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-11-24 19:44:57 -0800 |
commit | 143d80b2ce76ef6eee4bddda9039a0b0f9673356 (patch) | |
tree | bcbf7ee6e089d85a3e4ee834871b5edb1a8a43fa /src/DotNetOpenAuth.Test/Hosting/HttpHost.cs | |
parent | e81621138e95624e12db5667c5720cde1f0475d9 (diff) | |
download | DotNetOpenAuth-143d80b2ce76ef6eee4bddda9039a0b0f9673356.zip DotNetOpenAuth-143d80b2ce76ef6eee4bddda9039a0b0f9673356.tar.gz DotNetOpenAuth-143d80b2ce76ef6eee4bddda9039a0b0f9673356.tar.bz2 |
Added discovery and around 60 tests. The discovery and a few other tests still fail, but we're making progress.
Diffstat (limited to 'src/DotNetOpenAuth.Test/Hosting/HttpHost.cs')
-rw-r--r-- | src/DotNetOpenAuth.Test/Hosting/HttpHost.cs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/Hosting/HttpHost.cs b/src/DotNetOpenAuth.Test/Hosting/HttpHost.cs new file mode 100644 index 0000000..9606cb7 --- /dev/null +++ b/src/DotNetOpenAuth.Test/Hosting/HttpHost.cs @@ -0,0 +1,93 @@ +namespace DotNetOpenAuth.Test.Hosting { + using System; + using System.Globalization; + using System.IO; + using System.Net; + using System.Threading; + + class HttpHost : IDisposable { + HttpListener listener; + public int Port { get; private set; } + Thread listenerThread; + AspNetHost aspNetHost; + + HttpHost(AspNetHost aspNetHost) { + this.aspNetHost = aspNetHost; + + Port = 59687; + Random r = new Random(); + tryAgain: + try { + listener = new HttpListener(); + listener.Prefixes.Add(string.Format(CultureInfo.InvariantCulture, + "http://localhost:{0}/", Port)); + listener.Start(); + } catch (HttpListenerException ex) { + if (ex.Message.Contains("conflicts")) { + Port += r.Next(1, 20); + goto tryAgain; + } + throw; + } + listenerThread = new Thread(processRequests); + listenerThread.Start(); + } + + public static HttpHost CreateHost(AspNetHost aspNetHost) { + return new HttpHost(aspNetHost); + } + + public static HttpHost CreateHost(string webDirectory) { + return new HttpHost(AspNetHost.CreateHost(webDirectory)); + } + void processRequests() { + try { + while (true) { + var context = listener.GetContext(); + aspNetHost.BeginProcessRequest(context); + } + } catch (HttpListenerException) { + // the listener is probably being shut down + } + } + + public Uri BaseUri { + get { return new Uri("http://localhost:" + Port.ToString() + "/"); } + } + public string ProcessRequest(string url) { + return ProcessRequest(url, null); + } + public string ProcessRequest(string url, string body) { + WebRequest request = WebRequest.Create(new Uri(BaseUri, url)); + if (body != null) { + request.Method = "POST"; + request.ContentLength = body.Length; + using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) + sw.Write(body); + } + try { + using (WebResponse response = request.GetResponse()) { + using (StreamReader sr = new StreamReader(response.GetResponseStream())) + return sr.ReadToEnd(); + } + } catch (WebException ex) { + Logger.Error("Exception in HttpHost", ex); + using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream())) { + string streamContent = sr.ReadToEnd(); + Logger.ErrorFormat("Error content stream follows: {0}", streamContent); + } + throw; + } + } + + #region IDisposable Members + + public void Dispose() { + listener.Close(); + listenerThread.Join(1000); + listenerThread.Abort(); + } + + #endregion + } +} |