Skip to content

KamRon-67/MoqHttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MoqHttp

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

MoqHttp

An awesome Moq Rest Client project to jumpstart your projects!

Β· Report Bug Β· Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

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.

(back to top)

Built With

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.

(back to top)

Getting Started

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.

Prerequisites

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

Installation

You should be able to download and run out of the box after a build.

(back to top)

Usage

Traditional HTTP Mocking

    [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);
        }

πŸŽ₯ NEW: VCR (Video Cassette Recorder) Functionality

MoqHttp now includes powerful VCR capabilities to record and replay HTTP interactions!

Recording Mode - Capture Real API Responses

    [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
    }

Playback Mode - Replay Recorded Responses

    [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();
    }

Auto Mode - Record Once, Replay Forever

    [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.

Roadmap

  • 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).

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

πŸ’‰ Project tests

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.

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Cameron Young - @sigfualt - cameronyoung@cmmsolutions.io

(back to top)

Acknowledgments

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!

(back to top)

About

TDD Tools

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages