Easily prototyped HTTP client framework for C #

In the next project, I am going to write an application in C #, partially connected with an HTTP server. I really love writing my TDD-style code, and I would just like it if I could mock all the HTTP requests in my tests.

Does anyone here have a scanned HTTP client infrastructure?

Ps. I usually use Moq for ridicule. If you know some kind of free mocking framework that would be better at spoofing HTTP requests, that would be nice too.

+4
source share
4 answers

DotNetOpenId, an open source project from which you can reuse code, uses the HTTP wrapper classes through which all calls are made. During the test, a false HTTP handler is introduced so that responses can be programmatically set before the call. It has a different mode in which it hosts its own ASP.NET site so that the full actual stack can be implemented.

This works well, although it was not pulled out as a standalone solution. If you are interested in reuse, here are some relevant source links. And you can ask for help by integrating it into dotnetopenid@googlegroups.com.

Live: StandardWebRequestHandler.cs

Mocks: MockHttpRequest.cs , TestWebRequestHandler.cs

+6
source

I suggest you use the framework support for this, for example, System.Net.WebRequest.

Define a really simple interface and a simple wrapper for a web request. This way you get what you want and don’t add an external dependency for something that the framework already does.

+4
source

You can use WireMock.Net , which is a flexible library for drowning and bullying HTTP HTTP responses using query matching criteria.

And it can also be very easily used in unit-test projects. Check out the wiki for details.

NuGet is here here .

+1
source

I don’t think there really is any structure that handles the things you want to archive. After all, only you know what each Http request should do. So basically you have 2 options:

  • Making calls and using a dummy implementation on the other hand. It could be a simple console application that returns dummy data. If you need more logic, I would think about using a database of objects - in my opinion, they are ideal for these applications.
  • Use a mock implementation on the application side. If this implementation has a lot of logic, do not use any mocking structure - create your own mock class that has all the logic.

Hope this helps

0
source

All Articles