2010-04-14 10:28:33 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Hosting;
|
2010-04-20 14:13:14 -07:00
|
|
|
|
using HtmlAgilityPack;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
using Orchard.Specs.Util;
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
namespace Orchard.Specs.Hosting {
|
|
|
|
|
public static class RequestExtensions {
|
|
|
|
|
public static RequestDetails SendRequest(this WebHost webHost, string urlPath, IDictionary<string, IEnumerable<string>> postData) {
|
2010-04-14 12:50:00 -07:00
|
|
|
|
var physicalPath = Bleroy.FluentPath.Path.Get(webHost.PhysicalDirectory);
|
2010-04-14 10:28:33 -07:00
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
var details = new RequestDetails {
|
|
|
|
|
UrlPath = urlPath,
|
2010-04-14 10:28:33 -07:00
|
|
|
|
Page = physicalPath
|
|
|
|
|
.Combine(urlPath.TrimStart('/', '\\'))
|
2010-04-20 14:13:14 -07:00
|
|
|
|
.GetRelativePath(physicalPath),
|
2010-04-14 10:28:33 -07:00
|
|
|
|
};
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
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})
|
|
|
|
|
.Aggregate("", (a, x) => a + (x.n == 0 ? "" : "&") + x.p);
|
|
|
|
|
details.PostData = Encoding.Default.GetBytes(requestBodyText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webHost.Execute(() => {
|
2010-04-14 10:28:33 -07:00
|
|
|
|
var output = new StringWriter();
|
2010-04-14 18:22:23 -07:00
|
|
|
|
HttpRuntime.ProcessRequest(new Worker(details, output));
|
2010-04-14 10:28:33 -07:00
|
|
|
|
details.ResponseText = output.ToString();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return details;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
public static RequestDetails SendRequest(this WebHost webHost, string urlPath) {
|
|
|
|
|
return webHost.SendRequest(urlPath, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Worker : SimpleWorkerRequest {
|
2010-04-14 18:22:23 -07:00
|
|
|
|
private readonly RequestDetails _details;
|
|
|
|
|
private readonly TextWriter _output;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
|
|
|
|
|
public Worker(RequestDetails details, TextWriter output)
|
2010-04-20 14:13:14 -07:00
|
|
|
|
: base(details.Page, details.Query, output) {
|
2010-04-14 10:28:33 -07:00
|
|
|
|
_details = details;
|
|
|
|
|
_output = output;
|
2010-04-20 14:13:14 -07:00
|
|
|
|
PostContentType = "application/x-www-form-urlencoded";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string PostContentType { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override String GetHttpVerbName() {
|
|
|
|
|
if (_details.PostData == null)
|
|
|
|
|
return base.GetHttpVerbName();
|
|
|
|
|
|
|
|
|
|
return "POST";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string GetKnownRequestHeader(int index) {
|
|
|
|
|
if (index == HeaderContentLength) {
|
|
|
|
|
if (_details.PostData != null)
|
|
|
|
|
return _details.PostData.Length.ToString();
|
|
|
|
|
}
|
|
|
|
|
else if (index == HeaderContentType) {
|
|
|
|
|
if (_details.PostData != null)
|
|
|
|
|
return PostContentType;
|
|
|
|
|
}
|
|
|
|
|
return base.GetKnownRequestHeader(index);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] GetPreloadedEntityBody() {
|
|
|
|
|
if (_details.PostData != null)
|
|
|
|
|
return _details.PostData;
|
|
|
|
|
|
|
|
|
|
return base.GetPreloadedEntityBody();
|
2010-04-14 10:28:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
public override void SendStatus(int statusCode, string statusDescription) {
|
2010-04-14 10:28:33 -07:00
|
|
|
|
_details.StatusCode = statusCode;
|
|
|
|
|
_details.StatusDescription = statusDescription;
|
|
|
|
|
|
|
|
|
|
base.SendStatus(statusCode, statusDescription);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
public override void SendResponseFromFile(string filename, long offset, long length) {
|
2010-04-14 10:28:33 -07:00
|
|
|
|
_output.Write(File.ReadAllText(filename));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-14 18:22:23 -07:00
|
|
|
|
|