[英]Unit testing a method with HTTP post request by mocking HttpWebRequest
[英]Read the HTTP POST request of HttpWebRequest
我需要創建 Http POST 請求和一些 GET 請求作為我正在編寫的一些測試的字符串。 目前,我的測試使用 StringBuilder 和從 fiddler 中提取的硬編碼 POST 請求構建它們,如下所示:
var builder = new StringBuilder();
builder.Append("POST https://some.web.pg HTTP/1.1\r\n");
builder.Append("Content-Type: application/x-www-form-urlencoded\r\n");
builder.Append("Referer: https://some.referer.com\r\n");
builder.Append("Accept-Language: en-us\r\n");
builder.Append("Accept-Encoding: gzip, deflate\r\n");
builder.Append("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\n");
builder.Append("Host: login.yahoo.com\r\n");
// ... other header info
builder.Append("\r\n");
builder.Append("post body......\r\n");
var postData = builder.ToString();
這很快使我的測試變得混亂,我希望有一種更簡潔的方法來構建這些 POST 請求。 我一直在研究 HttpWebRequest 類,希望它可以為我創建這些。 我認為在背后一定有某種方法來構建我試圖以某種形式創建的這個確切請求。 但是,唉,GetRequestStream 是一個可寫的唯一流。
有沒有辦法讀取 HttpWebRequest 將生成的請求流(然后將其更改為字符串)? 甚至任何關於如何生成這些 POST 請求的想法都可以。
這是一個發出 Get 請求的 msdn 示例:
using System;
使用 System.Net; 使用 System.IO;
namespace MakeAGETRequest_charp { /// /// Class1 的摘要描述。 /// class Class1 { static void Main(string[] args) { string sURL; sURL = " http://www.microsoft.com ";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
WebProxy myProxy = new WebProxy("myproxy",80);
myProxy.BypassProxyOnLocal = true;
wrGETURL.Proxy = WebProxy.GetDefaultProxy();
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
}
}
} 和這里的 post 請求(來自帶有 post 的 HTTP 請求)
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create("http:\\domain.com\page.asp");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream newStream = httpWReq.GetRequestStream())
{
newStream.Write(data,0,data.Length);
}
我建議您使用模擬,因為它是單元測試的最佳實踐:請參閱堆棧單元測試 C# 中的 HTTP 請求上的此答案
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(yoururllink);
var c = HttpContext.Current;
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent from your url link
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.