An awesome Moq Rest Client project to jumpstart your projects!
Β·
Report Bug
Β·
Request Feature
Table of Contents
This is a lightweight mocking tool for .NET 10 plus. The goal is to stay light and easy to integrate into your project.
Here's why:
- Your time should be focused on creating something amazing. A project that solves a problem and helps others
- You shouldn't be doing the same tasks over and over like creating a service project to mock your HTTP requests.
- You should implement DRY principles to the rest of your life π
Of course, your needs may be different. So I'll be adding more features shortly. You may also suggest changes by forking this repo and creating a pull request or opening an issue.
This section should list any major frameworks/libraries used to bootstrap your project. Leave any add-ons/plugins for the acknowledgements section. Here are a few examples.
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
This is an example of how to list things you need to use the software and how to install them.
- nuget
Install-Package MoqHttp -Version 1.0.0
You should be able to download and run out of the box after a build.
[Fact]
public async void Get_Should_Work_Correctly()
{
//Arrange
_mockServer = new HttpServer(Port);
_mockServer.Run();
//Act
_mockServer.Config.Get("/test/123").Send("It Really Works!");
var responseGet = await _httpClient.GetAsync($"{_address}/test/123");
_mockServer.Config.Get("/testAction/123").Send(context =>
{
context.Response.StatusCode = 200;
const string response = "Action Test";
var buffer = System.Text.Encoding.UTF8.GetBytes(response);
context.Response.Body.WriteAsync(buffer, 0, buffer.Length);
});
var responseGetAction = await _httpClient.GetAsync($"{_address}/testAction/123");
_mockServer.Dispose();
//Assert
Assert.Equal("It Really Works!", await responseGet.Content.ReadAsStringAsync());
Assert.Equal(200, (int)responseGet.StatusCode);
Assert.Equal("Action Test", await responseGetAction.Content.ReadAsStringAsync());
Assert.Equal(200, (int)responseGetAction.StatusCode);
}MoqHttp now includes powerful VCR capabilities to record and replay HTTP interactions!
[Fact]
public async Task Record_Real_API_Interaction()
{
// Record real HTTP interactions to a cassette file
var mockServer = new HttpServer(5000);
mockServer.Config
.Record()
.ToFile("fixtures/api_response.json")
.ProxyTo("https://api.github.com");
mockServer.Run();
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://localhost:5000/users/octocat");
// Response comes from real API and is recorded
Assert.Equal(200, (int)response.StatusCode);
mockServer.Dispose(); // Cassette is saved automatically
} [Fact]
public async Task Playback_Recorded_Interaction()
{
// Replay from cassette file (no real API calls)
var mockServer = new HttpServer(5000);
mockServer.Config
.Playback()
.FromFile("fixtures/api_response.json");
mockServer.Run();
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://localhost:5000/users/octocat");
// Response comes from cassette - instant, no network call!
Assert.Equal(200, (int)response.StatusCode);
mockServer.Dispose();
} [Fact]
public async Task Auto_Mode_Smart_Recording()
{
// Automatically records if cassette doesn't exist, otherwise plays back
var mockServer = new HttpServer(5000);
mockServer.Config
.Auto()
.FromFile("fixtures/api_data.json")
.ProxyTo("https://api.example.com");
mockServer.Run();
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://localhost:5000/v1/data");
// First run: Records from real API
// Subsequent runs: Instant playback from cassette
Assert.Equal(200, (int)response.StatusCode);
mockServer.Dispose();
}Benefits of VCR:
- β Record real API responses once, replay infinitely
- β No API rate limits in tests
- β Tests run 100x faster (no network calls)
- β Works offline
- β Perfect for CI/CD pipelines
- β Realistic test data without manual mocking
See VCR_EXAMPLES.md for comprehensive documentation and real-world examples.
- Add Changelog
- Add back to top links
- VCR Phase 1: Core Recording/Playback π
- Record mode
- Playback mode
- Auto mode
- Cassette file management
- VCR Phase 2: Advanced Features
- Custom request matching
- Request filtering
- Response transformation
- VCR Phase 3: Security
- Data scrubbing
- Sensitive data redaction
- VCR Phase 4: Validation
- Diff detection
- Cassette expiration
- Clean up docs
- Add Additional Templates w/ Examples
- Add "components" document to easily copy & paste sections of the readme
- Return xml and json
- Json
- xml
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
If you want to improve the development of this project, you must, after changing or improving whatever, run the project's tests to prove that they are working.
Distributed under the MIT License. See LICENSE.txt for more information.
Cameron Young - @sigfualt - cameronyoung@cmmsolutions.io
Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!