简体   繁体   English

如何测试附加文件的ASP NET ashx处理程序

[英]How to test ASP NET ashx Handler With File Attached

I have an c# ashx handler that detects files attached. 我有一个c#ashx处理程序,用于检测附加的文件。 It works fine. 工作正常。

However, I am relying on third party company to write software to send files to handler (long story). 但是,我依靠第三方公司编写软件来将文件发送给处理程序(长话短说)。 I need to test that my handler works, time difference between us and 3rd party company is becoming a nightmare. 我需要测试我的管理员是否可以正常工作,我们与第三方公司之间的时差正在变成一场噩梦。

The scenario is 3rd party software sends files every 30 seconds to handler and I need to test this works and without sounding stupid I thought I ask stackoverflow :) 场景是第3方软件每30秒将文件发送给处理程序,我需要测试一下它的工作原理,而不会听起来有些愚蠢,我想我要问一下stackoverflow :)

I just want to test my ashx handler using test unit or whatever but no idea where to start. 我只想使用测试单元或其他方法测试我的ashx处理程序,但不知道从哪里开始。 Typing in "handler.ashx?filename=12345.csv" is helpful but no actual file attached! 输入“ handler.ashx?filename = 12345.csv”会很有帮助,但没有附加实际文件!

Any suggestions would be great. 任何建议都很好。

From what I understand, you have an ashx handler that you can upload files to and you want to test it. 据我了解,您有一个ashx处理程序,可以将文件上传到该处理程序并进行测试。

I have attached a sample test that assumes an ashx handler that uses a POST request for file attachments. 我已附上了一个示例测试,该测试假定ashx处理程序对文件附件使用POST请求。

[TestMethod]
public void TestCallUploadHandler()
{
    const string FILE_PATH = "C:\\foo.txt";
    const string FILE_NAME = "foo.txt";
    string UPLOADER_URI =
        string.Format("http://www.foobar.com/handler.ashx?filename={0}", FILE_NAME);

    using (var stream = File.OpenRead(FILE_PATH))
    {
        var httpRequest = WebRequest.Create(UPLOADER_URI) as HttpWebRequest;
        httpRequest.Method = "POST";
        stream.Seek(0, SeekOrigin.Begin);
        stream.CopyTo(httpRequest.GetRequestStream());

        var httpResponse = httpRequest.GetResponse();
        StreamReader reader = new StreamReader(httpResponse.GetResponseStream());
        var responseString = reader.ReadToEnd();

        //Check the responsestring and see if all is ok
    }
}

Basically what you are doing is creating a WebRequest for POST and attaching the filestream to its request and filename to its query string. 基本上,您正在做的是为POST创建一个WebRequest并将文件流附加到其请求,并将文件名附加到其查询字符串。

To answer my question and many thanks to @parapura: 要回答我的问题,并感谢@parapura:

[TestMethod]
public void TestCallUploadHandler()
{
    const string FILE_PATH = "C:\\foo.txt";
    const string FILE_NAME = "foo.txt";
    string UPLOADER_URI = string.Format("http://www.foobar.com/handler.ashx?filename={0}", FILE_NAME);

    using (var stream = File.OpenRead(FILE_PATH))
    {
        var httpRequest = WebRequest.Create(UPLOADER_URI) as HttpWebRequest;
        httpRequest.Method = "POST";
        NetworkCredential networkCredential = new NetworkCredential("username", "pwd"); 
        httpRequest.Credentials = networkCredential;
        stream.Seek(0, SeekOrigin.Begin);
        stream.CopyTo(httpRequest.GetRequestStream());

        byte[] authBytes = Encoding.UTF8.GetBytes("username:pwd".ToCharArray());
        httpRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

        var httpResponse = httpRequest.GetResponse();
        StreamReader reader = new StreamReader(httpResponse.GetResponseStream());
        var responseString = reader.ReadToEnd();

        //Check the responsestring and see if all is ok
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET在ashx处理程序中保存图像 - ASP.NET Save Image in ashx handler 如何在我的控制器(ASP.NET MVC)中调用Http Handler文件(.ashx)中定义的方法? - How can I call a method that's defined in an Http Handler file (.ashx) in my controller (ASP.NET MVC)? 未为 *.aspx 文件触发 asp.net handler.ashx 文件中的 httphandler - httphandler in asp.net handler.ashx file not triggered for *.aspx file 从ASP.NET Web处理程序(.ashx)下载文件时的错误处理 - Error handling when downloading file from ASP.NET Web Handler (.ashx) 捕获由通用处理程序(ashx)文件导致的ASP.NET中的异常 - Catch exceptions in ASP.NET resulting from Generic Handler (ashx) file 图像未显示ASP.NET(Generic handler.ashx) - Image is not displayed ASP.NET (Generic handler.ashx) 通过ashx处理程序传递json值:asp.net: - passing json value through ashx handler: asp.net: '/'应用程序中的ASP.NET Server错误-ashx处理程序 - ASP.NET Server error in '/' application - ashx handler 如何将.ashx处理程序与asp:Image对象一起使用? - How do I use an .ashx handler with an asp:Image object? 如何使用ashx文件ASP.NET处理同一页面上的多个jqgrid实例 - how to handle multiple jqgrid instances on same page with ashx file ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM