pmilet.Playback 1.0.10

An Asp.Net Core middleware library for recording http requests and replaying them by means of the recorded playback identifier.

There is a newer version of this package available.
See the version list below for details.
Install-Package pmilet.Playback -Version 1.0.10
dotnet add package pmilet.Playback --version 1.0.10
<PackageReference Include="pmilet.Playback" Version="1.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pmilet.Playback --version 1.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Asp.Net Core Playback

An Asp.Net Core middleware library for recording http requests and replaying them by means of the recorded playback identifier.

Purpose

Record your webapi http requests to be replayed anytime in any environment using a recorded playback id.
With a playback id we can debug issues recorded in our production environment and reproduce it in our development environment, we can use the playbackid as input to unit testing our api methods, we can record user interactions to obtain a sequence of playbackids to be used for a regression or load test.

How to record and playback Api requests?

Once your Asp.NetCore Api is configured for playback ( see quick start section below or refer to sample in github repo ) you can start recording your api requests by setting the X-Playback-Mode request header value to Record.

curl -X GET --header 'Accept: text/plain' --header 'X-Playback-Mode: Record' 'http://apigatewaysample.azurewebsites.net/api/Hello/hello'

then a x-playback-id response header will be returned.

Response Headers
{
  "date": "Wed, 25 Oct 2017 21:05:46 GMT",
  "content-encoding": "gzip",
  "server": "Kestrel",
  "x-powered-by": "ASP.NET",
  "vary": "Accept-Encoding",
  "content-type": "text/plain; charset=utf-8",
  "transfer-encoding": "chunked",
  "x-playback-id": "_ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046"
}

To replay set the X-Playback-Mode header to Playback and the X-Playback-Id header with the value received from the recording response.

curl -X GET --header 'Accept: text/plain' --header 'X-Playback-Id: _ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046' --header 'X-Playback-Mode: Playback' 'http://apigatewaysample.azurewebsites.net/api/Hello/bye'

When setting the x-playback-mode to None the playback functionality is bypassed.

PlaybackId format

The playback id is composed of differents parts each one carrying important context information.
The playback id parts are separated by an underscore :

PlaybackContextInfo_ApiName_PlaybackVersion_RequestPath_RequestMethod_RequestContextHash
 

  • The PlayContextInfo comes from the X-Playback-RequestContext header.
  • The ApiName is the web api Name.
  • The PlaybackVersion comes from the X-Playback-Version header.
  • The RequestPath is the request path url encoded
  • The RequestMethod is the request http verb
  • The RequestContextHash is a hash of the request payload in order to univoquely indentify each different request.

For example this playbackid DemoUser_ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046 can be descompsed as:

  • PlayContextInfo = DemoUser
  • AssemblyName = ApiGateway+Sample
  • PlaybackVersion = v1.0
  • RequestPath = Hello%252Fhello
  • RequestMethod = GET
  • RequestContextHash = 757602046

How to Quick Start

In your Startup class:

Configure Playback middleware.

using pmilet.Playback;

...

public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
        
...        

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ...
            
            services.AddPlayback(Configuration);
            
            ...
            
            //don't forget to return the service provider
            return services.BuildServiceProvider();

         }
 
 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            ...
            
            app.UsePlayback();
          
            ...
        }
      
 ...
            

In your appsetings.json file:

Add playback storage section

{
  "PlaybackBlobStorage": {
    "ConnectionString": "UseDevelopmentStorage=true",
    "ContainerName": "playback"
  },

In your controllers:

if using swagger, decorate your controller for swagger to generate playback headers in swagger UI  

using pmilet.Playback;

  ...

  [HttpGet]
  [SwaggerOperationFilter(typeof(PlaybackSwaggerFilter))]
  public async Task<string> Get()
  
  ...
  

How to record responses received from outgoing requests

For recording responses from outgoing requests you should use the PlaybackContext class that can be injected in your api proxies.

this code excerpt show how you can save a response received from an outgoing api call

       var response = await httpClient.GetAsync(url);
       var result = await response.Content.ReadAsStringAsync();
       if (_playbackContext.IsRecord)
            {
                await _playbackContext.RecordResult<MyServiceResponse>(result);
            }
            else if ( _playbackContext.IsPlayback )
            {
                return await _playbackContext.PlaybackResult<MyServiceResponse>();
            }
     

How to fake api responses

For faking api call responses implement a class that inherits from IFakeFactory like in the following example:

public class MyPlaybackFakeFactory : FakeFactoryBase
    {
        public override void GenerateFakeResponse(HttpContext context)
        {
            switch (context.Request.Path.Value.ToLower())
            {
                case "/api/hello":
                    if (context.Request.Method == "POST")
                        GenerateFakeResponse<HelloRequest, string>(context, HelloPost);
                    else if (context.Request.Method == "GET")
                        GenerateFakeResponse<string, string>(context, HelloGet);
                    break;
                default:
                    throw new NotImplementedException("fake method not found");
            }
        }

Then don't forget to register your fake factory duting initialization:

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddControllersAsServices();
            
            services.AddPlayback(Configuration, fakeFactory: new MyPlaybackFakeFactory());

Asp.Net Core Playback

An Asp.Net Core middleware library for recording http requests and replaying them by means of the recorded playback identifier.

Purpose

Record your webapi http requests to be replayed anytime in any environment using a recorded playback id.
With a playback id we can debug issues recorded in our production environment and reproduce it in our development environment, we can use the playbackid as input to unit testing our api methods, we can record user interactions to obtain a sequence of playbackids to be used for a regression or load test.

How to record and playback Api requests?

Once your Asp.NetCore Api is configured for playback ( see quick start section below or refer to sample in github repo ) you can start recording your api requests by setting the X-Playback-Mode request header value to Record.

curl -X GET --header 'Accept: text/plain' --header 'X-Playback-Mode: Record' 'http://apigatewaysample.azurewebsites.net/api/Hello/hello'

then a x-playback-id response header will be returned.

Response Headers
{
  "date": "Wed, 25 Oct 2017 21:05:46 GMT",
  "content-encoding": "gzip",
  "server": "Kestrel",
  "x-powered-by": "ASP.NET",
  "vary": "Accept-Encoding",
  "content-type": "text/plain; charset=utf-8",
  "transfer-encoding": "chunked",
  "x-playback-id": "_ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046"
}

To replay set the X-Playback-Mode header to Playback and the X-Playback-Id header with the value received from the recording response.

curl -X GET --header 'Accept: text/plain' --header 'X-Playback-Id: _ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046' --header 'X-Playback-Mode: Playback' 'http://apigatewaysample.azurewebsites.net/api/Hello/bye'

When setting the x-playback-mode to None the playback functionality is bypassed.

PlaybackId format

The playback id is composed of differents parts each one carrying important context information.
The playback id parts are separated by an underscore :

PlaybackContextInfo_ApiName_PlaybackVersion_RequestPath_RequestMethod_RequestContextHash
 

  • The PlayContextInfo comes from the X-Playback-RequestContext header.
  • The ApiName is the web api Name.
  • The PlaybackVersion comes from the X-Playback-Version header.
  • The RequestPath is the request path url encoded
  • The RequestMethod is the request http verb
  • The RequestContextHash is a hash of the request payload in order to univoquely indentify each different request.

For example this playbackid DemoUser_ApiGateway+Sample_v1.0_Hello%252Fhello_GET_757602046 can be descompsed as:

  • PlayContextInfo = DemoUser
  • AssemblyName = ApiGateway+Sample
  • PlaybackVersion = v1.0
  • RequestPath = Hello%252Fhello
  • RequestMethod = GET
  • RequestContextHash = 757602046

How to Quick Start

In your Startup class:

Configure Playback middleware.

using pmilet.Playback;

...

public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
        
...        

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ...
            
            services.AddPlayback(Configuration);
            
            ...
            
            //don't forget to return the service provider
            return services.BuildServiceProvider();

         }
 
 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            ...
            
            app.UsePlayback();
          
            ...
        }
      
 ...
            

In your appsetings.json file:

Add playback storage section

{
  "PlaybackBlobStorage": {
    "ConnectionString": "UseDevelopmentStorage=true",
    "ContainerName": "playback"
  },

In your controllers:

if using swagger, decorate your controller for swagger to generate playback headers in swagger UI  

using pmilet.Playback;

  ...

  [HttpGet]
  [SwaggerOperationFilter(typeof(PlaybackSwaggerFilter))]
  public async Task<string> Get()
  
  ...
  

How to record responses received from outgoing requests

For recording responses from outgoing requests you should use the PlaybackContext class that can be injected in your api proxies.

this code excerpt show how you can save a response received from an outgoing api call

       var response = await httpClient.GetAsync(url);
       var result = await response.Content.ReadAsStringAsync();
       if (_playbackContext.IsRecord)
            {
                await _playbackContext.RecordResult<MyServiceResponse>(result);
            }
            else if ( _playbackContext.IsPlayback )
            {
                return await _playbackContext.PlaybackResult<MyServiceResponse>();
            }
     

How to fake api responses

For faking api call responses implement a class that inherits from IFakeFactory like in the following example:

public class MyPlaybackFakeFactory : FakeFactoryBase
    {
        public override void GenerateFakeResponse(HttpContext context)
        {
            switch (context.Request.Path.Value.ToLower())
            {
                case "/api/hello":
                    if (context.Request.Method == "POST")
                        GenerateFakeResponse<HelloRequest, string>(context, HelloPost);
                    else if (context.Request.Method == "GET")
                        GenerateFakeResponse<string, string>(context, HelloGet);
                    break;
                default:
                    throw new NotImplementedException("fake method not found");
            }
        }

Then don't forget to register your fake factory duting initialization:

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddControllersAsServices();
            
            services.AddPlayback(Configuration, fakeFactory: new MyPlaybackFakeFactory());

Release Notes

new file storage service

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.2 939 4/1/2019
1.2.1 469 2/19/2018
1.2.0 377 2/19/2018
1.1.2 424 1/23/2018
1.1.1 466 1/23/2018
1.1.0 371 1/23/2018
1.0.10 841 10/27/2017
1.0.9 584 10/25/2017
1.0.8 403 10/25/2017
1.0.7 487 10/22/2017
1.0.6 391 10/21/2017
1.0.5 334 10/21/2017
1.0.4 360 10/21/2017
1.0.3 349 10/18/2017
1.0.2 339 10/18/2017
1.0.1 359 10/18/2017
1.0.0 361 10/15/2017
Show less