Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
improved formatting
Source Link

In your code you can not intercept the calls to HttpWebRequestHttpWebRequest because you create the object in the same method. If you let another object create the HttpWebRequestHttpWebRequest, you can pass in a mock object and use that to test.

So instead of this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); 

Use this:

IHttpWebRequest request = this.WebRequestFactory.Create(_url); 

In your unit test, you can pass in a WebRequestFactoryWebRequestFactory which creates a mock object.

Furthermore, you can split of your stream reading code in a seperateseparate function:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { byte[] data = ReadStream(response.GetResponseStream()); return ExtractResponse(Encoding.UTF8.GetString(data)); } 

This makes it possible to test ReadStream() seperatelyReadStream() separately.

To do more of an integration test, you can set up your own HTTP server which returns test data, and pass the URL of that server to your method.

In your code you can not intercept the calls to HttpWebRequest because you create the object in the same method. If you let another object create the HttpWebRequest, you can pass in a mock object and use that to test.

So instead of this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); 

Use this:

IHttpWebRequest request = this.WebRequestFactory.Create(_url); 

In your unit test, you can pass in a WebRequestFactory which creates a mock object.

Furthermore, you can split of your stream reading code in a seperate function:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { byte[] data = ReadStream(response.GetResponseStream()); return ExtractResponse(Encoding.UTF8.GetString(data)); } 

This makes it possible to test ReadStream() seperately.

To do more of an integration test, you can set up your own HTTP server which returns test data, and pass the URL of that server to your method.

In your code you can not intercept the calls to HttpWebRequest because you create the object in the same method. If you let another object create the HttpWebRequest, you can pass in a mock object and use that to test.

So instead of this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); 

Use this:

IHttpWebRequest request = this.WebRequestFactory.Create(_url); 

In your unit test, you can pass in a WebRequestFactory which creates a mock object.

Furthermore, you can split of your stream reading code in a separate function:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { byte[] data = ReadStream(response.GetResponseStream()); return ExtractResponse(Encoding.UTF8.GetString(data)); } 

This makes it possible to test ReadStream() separately.

To do more of an integration test, you can set up your own HTTP server which returns test data, and pass the URL of that server to your method.

Source Link
Sjoerd
  • 75.9k
  • 16
  • 140
  • 180

In your code you can not intercept the calls to HttpWebRequest because you create the object in the same method. If you let another object create the HttpWebRequest, you can pass in a mock object and use that to test.

So instead of this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); 

Use this:

IHttpWebRequest request = this.WebRequestFactory.Create(_url); 

In your unit test, you can pass in a WebRequestFactory which creates a mock object.

Furthermore, you can split of your stream reading code in a seperate function:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { byte[] data = ReadStream(response.GetResponseStream()); return ExtractResponse(Encoding.UTF8.GetString(data)); } 

This makes it possible to test ReadStream() seperately.

To do more of an integration test, you can set up your own HTTP server which returns test data, and pass the URL of that server to your method.