pmilet.Playback 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package pmilet.Playback --version 1.0.6
NuGet\Install-Package pmilet.Playback -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: pmilet.Playback, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install pmilet.Playback as a Cake Addin
#addin nuget:?package=pmilet.Playback&version=1.0.6

// Install pmilet.Playback as a Cake Tool
#tool nuget:?package=pmilet.Playback&version=1.0.6

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.
Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.2 1,590 4/1/2019
1.2.1 1,179 2/19/2018
1.2.0 1,028 2/19/2018
1.1.2 1,114 1/23/2018
1.1.1 1,203 1/23/2018
1.1.0 1,043 1/23/2018
1.0.10 1,983 10/27/2017
1.0.9 1,462 10/25/2017
1.0.8 1,105 10/25/2017
1.0.7 1,264 10/22/2017
1.0.6 1,041 10/21/2017
1.0.5 978 10/21/2017
1.0.4 982 10/21/2017
1.0.3 962 10/18/2017
1.0.2 953 10/18/2017
1.0.1 963 10/18/2017
1.0.0 840 10/15/2017

new file storage service