I want to write a unit test for http web request and response method. Please find the below method,
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;
try
{
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);
}
I dont know how to write unit test for web request methods.Can anyone suggest how to write unit test for the above method?
Aucun commentaire:
Enregistrer un commentaire