[英]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.