Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easily mockable HTTP client framework for C#

Tags:

c#

http

tdd

mocking

In a upcoming project I'm going to write an application in C# which partly has to communicate with a HTTP server. I'm very fond of writing my code TDD-style, and I would just love it if I could mock all of the HTTP requests in my tests.

Does any one here know about an easly mockable HTTP client framework?

Ps. I usually use Moq for mocks. If you know of some free mocking framework that would be better to mock HTTP requests, that would be nice too.

like image 313
Presidenten Avatar asked Dec 22 '22 12:12

Presidenten


1 Answers

DotNetOpenId, an open source project from which you may reuse code, uses HTTP wrapper classes through which all calls are made. During testing, a mock HTTP handler is injected so that the responses can be programmatically set before the call is made. It has another mode where it hosts its own ASP.NET site so that the full actual stack can be exercised.

This works well, although it hasn't been pulled out as a standalone solution. If you're interested in reusing it, here are some relevant links to the source. And you can ask for help integrating it at [email protected].

Live one: StandardWebRequestHandler.cs

Mocks: MockHttpRequest.cs, TestWebRequestHandler.cs

like image 145
Andrew Arnott Avatar answered Dec 27 '22 05:12

Andrew Arnott