[英]Testing a asp.net project IHttpHandler , issue with Request.Files
For testing purposes I need to pass over an instance of HttpFileCollection to do a simulated upload. 出于测试目的,我需要传递一个HttpFileCollection实例来进行模拟上传。 The thing is: I can't mock this because I need to download the document again to double check this as an integration test.
问题是:我不能嘲笑这个因为我需要再次下载文档以将其作为集成测试进行双重检查。
The biggest problem is that the constructor of HttpFileCollection is protected. 最大的问题是HttpFileCollection的构造函数受到保护。
Cann anybody help me with that? Cann任何人帮助我吗?
I am using a custom testHandler similar to this one: 我正在使用类似于这个的自定义testHandler:
http://blogs.cozi.com/techold/2008/05/a-way-to-unit-t.html http://blogs.cozi.com/techold/2008/05/a-way-to-unit-t.html
The code under test is the following: 测试中的代码如下:
public class DocumentUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var jArray = new JArray();
bool errorOccured = false;
var files = context.Request.Files;
var queryParams = context.Request.QueryString.AllKeys.ToDictionary(k => k.ToLowerInvariant(),
k => context.Request.QueryString[k]);
using (var session = Connection.SessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
try
{
int qsSessionId = 0;
if (!queryParams.ContainsKey("sessionid"))
{
throw new ArgumentException("Parameter missing.", "sessionid");
};
if (!int.TryParse(queryParams["sessionid"], out qsSessionId))
{
throw new ArgumentException("Parameter malformed.", "sessionid");
};
var activity = session.Query<Activity>().Fetch(x=>x.Contact).FirstOrDefault(x => x.Session == qsSessionId);
if (activity == null)
{
throw new Exception("Session not found.");
}
string qsFilename = "";
if (queryParams.ContainsKey("filename") && !string.IsNullOrWhiteSpace(queryParams["filename"]))
qsFilename = queryParams["filename"];
for (int i = 0; i < files.Count; i++)
{
if (files[i].ContentLength > 0)
{
var now = DateTime.Now;
var filename = "";
if (string.IsNullOrWhiteSpace(qsFilename))
{
filename = string.Format("{0}_{1}_{2}{3}",
Path.GetFileNameWithoutExtension(files[i].FileName),
activity.Contact.Login,
DateUtils.IsoDateTimeToString(now),
Path.GetExtension(files[i].FileName));
}
else
{
filename = string.Format("{0}_{1}_{2}{3}",
qsFilename,
activity.Contact.Login,
DateUtils.IsoDateTimeToString(now),
Path.GetExtension(files[i].FileName));
}
var document = new Document
{
Code = filename,
Filename = filename,
FileExtension = Path.GetExtension(files[i].FileName),
Client = session.Load<Client>(801),
DocumentType = session.Load<DocumentType>(430),
Imported = now,
Modified = now
};
var fileByteArray = new byte[files[i].ContentLength];
files[i].InputStream.Read(fileByteArray, 0, files[i].ContentLength);
document.FileData = ZlibStream.CompressBuffer(fileByteArray);
session.Save(document);
jArray.Add(document.Id);
}
}
tx.Commit();
}
catch (Exception exception)
{
tx.Rollback();
context.Response.Clear();
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Output.Write(JsonConvert.SerializeObject(exception, Formatting.Indented));
context.Response.Output.Close();
context.Response.Flush();
errorOccured = true;
}
}
if (errorOccured == false)
{
context.Response.Clear();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Output.Write(jArray.ToString());
context.Response.Output.Close();
context.Response.Flush();
}
}
Greetings 问候
Edit: To clarify this: How would I test this without sending an real Webrequest to the .asax side? 编辑:澄清一下:如果不向.asax端发送真正的Webrequest,我将如何测试? I don't want the server to run as a requirement for my tests
我不希望服务器作为我的测试的要求运行
Worst case you can create an instance by reflection accessing a protected constructor, then. 最糟糕的情况是,您可以通过反射访问受保护的构造函数来创建实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.