Ich möchte einen Komponententest für HTTP-Web-Anfrage und Antwort-Methode schreiben. Sie finden die folgenden Verfahren,Komponententest für HTTP-Web-Anfrage und Antwort in C#
public string GetEmployeeId()
{
var tokenRequest = (HttpWebRequest)WebRequest.Create("http://www.goggle.com");
tokenRequest.Method = "POST";
tokenRequest.ContentType = "application/x-www-form-urlencoded";
var bytes = Encoding.UTF8.GetBytes(GetKeys(credentials));
tokenRequest.ContentLength = bytes.Length;
Response response;
using (var stream = tokenRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
using (var webResponse = request.GetResponse())
{
Stream receiveStream = webResponse.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(readStream.ReadToEnd()));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
response = ser.ReadObject(ms) as Response;
ms.Close();
readStream.Close();
}
}
}
return response.Id;
}
private string GetKeys(Credentials credentials)
{
return String.Format(@"client_id={0}&client_secret={1}&grant_type=client_credentials",
credentials.Id, credentials.Secret);
}
Ich weiß nicht, wie Unit-Test für Web-Anfrage zu schreiben methods.Can jemand empfehlen, wie Unit-Test für das obige Verfahren zu schreiben?