Setup integration test working

Added (very bad) cookie support to integration test lib
Workaround to resolving collections appears to have worked

--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-04-21 15:01:01 -07:00
parent 583138c7ed
commit 19e694834d
13 changed files with 170 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
@@ -11,6 +12,7 @@ using Orchard.Specs.Util;
namespace Orchard.Specs.Hosting {
public static class RequestExtensions {
public static RequestDetails SendRequest(this WebHost webHost, string urlPath, IDictionary<string, IEnumerable<string>> postData) {
var physicalPath = Bleroy.FluentPath.Path.Get(webHost.PhysicalDirectory);
var details = new RequestDetails {
@@ -20,20 +22,31 @@ namespace Orchard.Specs.Hosting {
.GetRelativePath(physicalPath),
};
if (!string.IsNullOrEmpty(webHost.Cookies)) {
details.RequestHeaders.Add("Cookie", webHost.Cookies);
}
if (postData != null) {
var requestBodyText = postData
.SelectMany(kv=>kv.Value.Select(v=>new{k=kv.Key, v}))
.Select((kv, n) => new {p = HttpUtility.UrlEncode(kv.k) + "=" + HttpUtility.UrlEncode(kv.v), n})
.SelectMany(kv => kv.Value.Select(v => new { k = kv.Key, v }))
.Select((kv, n) => new { p = HttpUtility.UrlEncode(kv.k) + "=" + HttpUtility.UrlEncode(kv.v), n })
.Aggregate("", (a, x) => a + (x.n == 0 ? "" : "&") + x.p);
details.PostData = Encoding.Default.GetBytes(requestBodyText);
}
webHost.Execute(() => {
var output = new StringWriter();
HttpRuntime.ProcessRequest(new Worker(details, output));
var worker = new Worker(details, output);
HttpRuntime.ProcessRequest(worker);
details.ResponseText = output.ToString();
});
string setCookie;
if (details.ResponseHeaders.TryGetValue("Set-Cookie", out setCookie)) {
Trace.WriteLine(string.Format("Set-Cookie: {0}", setCookie));
webHost.Cookies = (webHost.Cookies + ';' + setCookie.Split(';').FirstOrDefault()).Trim(';');
}
return details;
}
@@ -71,6 +84,11 @@ namespace Orchard.Specs.Hosting {
if (_details.PostData != null)
return PostContentType;
}
else if (index == HeaderCookie) {
string value;
if (_details.RequestHeaders.TryGetValue("Cookie", out value))
return value;
}
return base.GetKnownRequestHeader(index);
}
@@ -89,6 +107,21 @@ namespace Orchard.Specs.Hosting {
base.SendStatus(statusCode, statusDescription);
}
public override void SendKnownResponseHeader(int index, string value) {
if (index == HeaderSetCookie) {
_details.ResponseHeaders.Add("Set-Cookie", value);
}
else {
_details.ResponseHeaders.Add("known header #" + index, value);
}
base.SendKnownResponseHeader(index, value);
}
public override void SendUnknownResponseHeader(string name, string value) {
_details.ResponseHeaders.Add(name, value);
base.SendUnknownResponseHeader(name, value);
}
public override void SendResponseFromFile(string filename, long offset, long length) {
_output.Write(File.ReadAllText(filename));
}