Giter Site home page Giter Site logo

fossabot / httprequestinterceptor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from throw-if-null/scissors

0.0 1.0 0.0 22 KB

Highly configurable implementation of DelegatingHandler that can be used for mocking the behavior of requests sent to specific routes.

Home Page: https://consoleout.com/2019/07/09/httprequest-interceptor.html

License: MIT License

C# 100.00%

httprequestinterceptor's Introduction

HttpRequestInterceptor

Build status CodeFactor FOSSA Status

Highly configurable implementation of DelegatingHandler that can be used for mocking the behavior of requests sent to specific routes.

Usage

In order to use the interceptor all you need to do is register in using the DI of your choice, below example uses Microsoft built in DI:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<Collection<HttpInterceptorOptions>>(Configuration.GetSection("HttpInterceptorOptions"));
    services.AddTransient<InterceptingHandler>();
    
    services.AddHttpClient("GitHubClient", client =>
    {
        client.BaseAddress = new Uri("https://api.github.com/");
        client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
        client.DefaultRequestHeaders.Add("User-Agent", "HttpRequestInterceptor-Test");
    })
    .AddHttpMessageHandler<InterceptingHandler>();

    services.AddMvc();
}

Examples

Configuration for the interceptor is provider via appsetting.json through the usage of options pattern.

HttpInterceptorOptions

This class represent a configuration entry that belong to a specific route or path. Configuration entry consist of:

  • MethdoName: represents one of the standard HTTP methods and it is a required value
  • Path: represents a route or a specific path that's going to be intercepted. Allows usage of wildecards "*". The parameter is required.
  • ReturnStatusCode: represent response status code. The parameter is required and it must belong to one of standard status codes that can be found in HttpStatusCode enum.
  • ReturnJsonContent: represents the serializes response content. It's an optional parameter.

Examples

This simplest example is when we want to intercept a specific request for example: /api/product/2 Intercepting this request can be achieved by providing this configuration in appsettings.json

"HttpInterceptorOptions": [
{
    "MethodName": "GET",
    "Path": "api/product/2",
    "ReturnStatusCode": 200,
    "ReturnJsonContent": "{ "Id": 1, "Name": "Product_1" }"
}

The above configuration will intercept only GET request that belong to "/api/product/2"

"HttpInterceptorOptions": [
{
    "MethodName": "GET",
    "Path": "api/product/59",
    "ReturnStatusCode": 404,
    "ReturnJsonContent": "{}"
}

The above configuration will return not found for the path: "api/product/59"

If we want to make the above example more generic, so that all the GET requests that belong to a route: /api/product got intercepted than we ca use this:

"HttpInterceptorOptions": [
{
    "MethodName": "GET",
    "Path": "api/product/*",
    "ReturnStatusCode": 200,
    "ReturnJsonContent": "{ "Id": 1, "Name": "Product_1" }"
}

The above configuration will intercept any GET request that is submitted on "/api/product/{id}", so "api/product/2" and "api/product/3" will get intercepted.

You can use more than one "*" sign if you need to:

"HttpInterceptorOptions": [
{
    "MethodName": "GET",
    "Path": "api/product/*/locations/*",
    "ReturnStatusCode": 200,
    "ReturnJsonContent": "{ "Id": 1, "Name": "Product_1" }"
}

The above configuratio will intercept GET request that belong to routes similar to "api/product/2/locations/london" or "api/product/2/locations/42"

License

FOSSA Status

httprequestinterceptor's People

Contributors

fossabot avatar mirzamerdovic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.