pmilet.Playback 1.0.6

An Asp.Net Core middleware library that simplifies the recording and playback of HTTP requests and responses. Suitable for reproducing user interactions in automated tests or production issues in the development environment.

There is a newer version of this package available.
See the version list below for details.
Install-Package pmilet.Playback -Version 1.0.6
dotnet add package pmilet.Playback --version 1.0.6
<PackageReference Include="pmilet.Playback" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pmilet.Playback --version 1.0.6
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 that simplifies the recording and playback of HTTP requests and responses. Suitable for reproducing user interactions in automated tests or production issues in the development environment.

Two usage scenarios

  1. Record incoming Api requests and outgoing Api responses in order to reproduce for testing or troubleshooting.
  2. Fake Api responses in order to quickly design your rest api interface .

When the X-Playback-Mode header is set to Record the request is saved to a remote storage (remote blob or local file storage available for the moment) and then a X-Playback-Id reponse header is returned that should be used for replay.

To replay a recorded request set the X-Playback-Mode request header to Playback mode and X.Playback-Id request header to the value returned in the previous step.

When the X-Playback-Mode is set to Fake fake responses will be returned. The faked responses are codified in a fake factory class you should implement and explicitly register.

There is also the possibility to capture the responses of any outgoing Api request in order to test the Api in total isolation.
Use the IPlaybackContext interface into your outgoing service proxies ( by injecting the IPlaybackContext into the constructors ). This interface provides methods for saving and replaying the responses from outgoing calls ( and correlate to the Api playback-id).

Use case 1 : has a developer I want to record api requests in order to be able to replay them.

In you web api project install pmilet.Playback package: Install-Package pmilet.Playback -Version 1.0.6

Configure your Startup class

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            
            services.AddPlayback(Configuration);

            ...
            
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });
            });

        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            ...
            
            app.UseSwagger();
            app.UseSwaggerUI(c=> c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

            app.UsePlayback();
      
            ...
        }

Configure playback storage settings. The default storage service is Azure Blob Storage.
A Storage connection string and container name should be provided. Add this section to theappsettings.json file:

"PlaybackStorage": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=ijewels;AccountKey=gB0hTWJoD+QZ4Wmipn1cZjt9vKqZJ9bABy7z/zDBDT3Dgojr2sMzRgGDW/sGa5CG//Ah4O7saJClGSWH/7VgIg==;EndpointSuffix=core.windows.net",
"Name": "playback"
}

To view the playback headers in swagger decorate your api method with the PlaybackSwaggerFilter

       [HttpGet]
       [SwaggerOperation("Hello")]
       [SwaggerOperationFilter(typeof(PlaybackSwaggerFilter))]
       public async Task<string> Get()

Test it:

  1. Navigate to swagger UI
  2. Set the X-Playback-Mode request header to Record and try-out
  3. Copy the X-Playback-Id value
  4. Set the X-Playback-Mode to Playback and try-out.
  5. You should receive the same result has in step 2.

Use case 2 : has a developer I want to record my api requests and outgoing responses in order to be able to replay them.

Use the IPlaybackContext interface into your outgoing services proxy to record and replay outgoing responses:

   public class MyServiceProxy
   {
        IPlaybackContext _playbackContext;
        public MyServiceProxy(IPlaybackContext context )
        {
            _playbackContext = context;
        }

        public async Task<MyServiceResponse> Execute( MyServiceRequest command)
        {
            var result =  new MyServiceResponse() {  Output = $"MyService received input: {command.Input}" };
            if (_playbackContext.IsRecord)
            {
                await _playbackContext.RecordResult<MyServiceResponse>(result);
            }
            else if ( _playbackContext.IsPlayback )
            {
                return await _playbackContext.PlaybackResult<MyServiceResponse>();
            }
            return result;
        }
    }

Use case 3 : has a developer I want to fake my api responses in order to design my api contract quickly.

   public void ConfigureServices(IServiceCollection services)
   {
       ...

           services.AddPlayback(Configuration, fakeFactory: new MyPlaybackFakeFactory());

       ...
   }

Implement your fake factory for example like this...

   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:
                   break;
           }
       }
      
       private string HelloGet(string request)
       {
           return "Hello FAKE";
       }

Test it:

  1. Navigate to swagger UI
  2. Set X-Playback-Mode header to Fake and try-out
  3. You should receive the faked response.

Asp.Net Core Playback

An Asp.Net Core middleware library that simplifies the recording and playback of HTTP requests and responses. Suitable for reproducing user interactions in automated tests or production issues in the development environment.

Two usage scenarios

  1. Record incoming Api requests and outgoing Api responses in order to reproduce for testing or troubleshooting.
  2. Fake Api responses in order to quickly design your rest api interface .

When the X-Playback-Mode header is set to Record the request is saved to a remote storage (remote blob or local file storage available for the moment) and then a X-Playback-Id reponse header is returned that should be used for replay.

To replay a recorded request set the X-Playback-Mode request header to Playback mode and X.Playback-Id request header to the value returned in the previous step.

When the X-Playback-Mode is set to Fake fake responses will be returned. The faked responses are codified in a fake factory class you should implement and explicitly register.

There is also the possibility to capture the responses of any outgoing Api request in order to test the Api in total isolation.
Use the IPlaybackContext interface into your outgoing service proxies ( by injecting the IPlaybackContext into the constructors ). This interface provides methods for saving and replaying the responses from outgoing calls ( and correlate to the Api playback-id).

Use case 1 : has a developer I want to record api requests in order to be able to replay them.

In you web api project install pmilet.Playback package: Install-Package pmilet.Playback -Version 1.0.6

Configure your Startup class

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            
            services.AddPlayback(Configuration);

            ...
            
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });
            });

        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            ...
            
            app.UseSwagger();
            app.UseSwaggerUI(c=> c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

            app.UsePlayback();
      
            ...
        }

Configure playback storage settings. The default storage service is Azure Blob Storage.
A Storage connection string and container name should be provided. Add this section to theappsettings.json file:

"PlaybackStorage": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=ijewels;AccountKey=gB0hTWJoD+QZ4Wmipn1cZjt9vKqZJ9bABy7z/zDBDT3Dgojr2sMzRgGDW/sGa5CG//Ah4O7saJClGSWH/7VgIg==;EndpointSuffix=core.windows.net",
"Name": "playback"
}

To view the playback headers in swagger decorate your api method with the PlaybackSwaggerFilter

       [HttpGet]
       [SwaggerOperation("Hello")]
       [SwaggerOperationFilter(typeof(PlaybackSwaggerFilter))]
       public async Task<string> Get()

Test it:

  1. Navigate to swagger UI
  2. Set the X-Playback-Mode request header to Record and try-out
  3. Copy the X-Playback-Id value
  4. Set the X-Playback-Mode to Playback and try-out.
  5. You should receive the same result has in step 2.

Use case 2 : has a developer I want to record my api requests and outgoing responses in order to be able to replay them.

Use the IPlaybackContext interface into your outgoing services proxy to record and replay outgoing responses:

   public class MyServiceProxy
   {
        IPlaybackContext _playbackContext;
        public MyServiceProxy(IPlaybackContext context )
        {
            _playbackContext = context;
        }

        public async Task<MyServiceResponse> Execute( MyServiceRequest command)
        {
            var result =  new MyServiceResponse() {  Output = $"MyService received input: {command.Input}" };
            if (_playbackContext.IsRecord)
            {
                await _playbackContext.RecordResult<MyServiceResponse>(result);
            }
            else if ( _playbackContext.IsPlayback )
            {
                return await _playbackContext.PlaybackResult<MyServiceResponse>();
            }
            return result;
        }
    }

Use case 3 : has a developer I want to fake my api responses in order to design my api contract quickly.

   public void ConfigureServices(IServiceCollection services)
   {
       ...

           services.AddPlayback(Configuration, fakeFactory: new MyPlaybackFakeFactory());

       ...
   }

Implement your fake factory for example like this...

   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:
                   break;
           }
       }
      
       private string HelloGet(string request)
       {
           return "Hello FAKE";
       }

Test it:

  1. Navigate to swagger UI
  2. Set X-Playback-Mode header to Fake and try-out
  3. You should receive the faked response.

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 470 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 360 10/18/2017
1.0.0 361 10/15/2017
Show less