I have a requirement of export data as xml through HTTP POST request. It is done by as in below code now problem is I want to create unit test with by mock http request. Can some body help me out on unit test of this?
private string WriteBytesToHttp(ExportConfiguration exportConfiguration, byte[] bytes)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(exportConfiguration.Endpoint.Address);
Encoding encoding = exportConfiguration.Format.EncodingCodePage.ToEncoding();
var contentType = string.IsNullOrEmpty(exportConfiguration.Format.ContentType)
? "text/plain"
: exportConfiguration.Format.ContentType;
request.ContentType = contentType;
//request.ContentType = string.Format("{0}; charset={1}", contentType, encoding.HeaderName);
request.ContentLength = bytes.Length;
request.Method = "POST";
if (!(string.IsNullOrEmpty(exportConfiguration.Endpoint.Username) ||
string.IsNullOrEmpty(exportConfiguration.Endpoint.Password)))
{
var creditionStr = string.Format("{0}:{1}", exportConfiguration.Endpoint.Username,
exportConfiguration.Endpoint.Password);
request.Headers.Add("Authorization",
"Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(creditionStr)));
}
Stream newStream = request.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
var streamReader = string.IsNullOrEmpty(response.CharacterSet)
? new StreamReader(responseStream)
: new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
string responseStr = streamReader.ReadToEnd();
return String.IsNullOrEmpty(responseStr) ? response.StatusCode.ToString() : responseStr;
}