0

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;
                 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?

RCM
  • 91
  • 3
  • 10

2 Answers2

0

With unit testing you often have to use dependency injection and rely on interfaces rather than concrete classes. If you don't want to mock the server, which doesn't work reliably on hosted build machines anyway, create and inject a mock HttpWebRequest. If you are not interested in NuGet packages you can do it yourself by making an interface that contains the methods you need to use in your code, creating a production implementation that simply wraps HttpWebRequest, and creating a second implementation for unit testing that simply hands out an expected response. This lets you unit test the client without setting up a server.

Here is a SO post that explains doing it with Moq.

Community
  • 1
  • 1
leetibbett
  • 843
  • 4
  • 16
-1

You need to specify what you want to test.

An example unit test for your method would look something like this:

[TestClass]
public class WebUnitTests
{
   [TestMethod]
   public void Can_Request_Employee_Id()
   {
       // Arrange
       YourHttpRequestClass c = new YourHttpRequestClass();
       var employeeId = c.GetEmployeeId();

       // Assert
       Assert.IsFalse(string.IsNullOrEmpty(employeeId));

   }
}

I recommend you have a look at some unit testing basics.

https://msdn.microsoft.com/en-us/library/hh694602.aspx

Joakim Hansson
  • 544
  • 3
  • 15