测试asp.net项目IHttpHandler,问题是Request.Files

本文关键字:问题是 Request Files IHttpHandler asp net 项目 测试 | 更新日期: 2023-09-27 18:10:52

出于测试目的,我需要传递HttpFileCollection的实例来进行模拟上传。问题是:我不能嘲笑这一点,因为我需要再次下载文档,作为集成测试来仔细检查这一点。

最大的问题是HttpFileCollection的构造函数是受保护的。

有谁能帮我一下吗?

我使用一个自定义的testandler类似于这个:

http://blogs.cozi.com/techold/2008/05/a-way-to-unit-t.html

测试代码如下:

 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();
        }

    }

的问候编辑:澄清一下:如果不发送一个真正的Webrequest到。asax端,我怎么测试这个?我不希望服务器作为测试的要求运行

测试asp.net项目IHttpHandler,问题是Request.Files

最坏的情况是,你可以通过反射访问受保护的构造函数来创建实例,那么

MSDN

类似的帖子在stackoverflow