pmilet.Playback 1.0.9

An Asp.Net Core middleware library that simplifies the recording and playback of api calls by means of a simple playback identifier.
Suitable for saving user interactions in production in order to be replayed locally, anytime and in isolation.

There is a newer version of this package available.
See the version list below for details.
Install-Package pmilet.Playback -Version 1.0.9
dotnet add package pmilet.Playback --version 1.0.9
<PackageReference Include="pmilet.Playback" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pmilet.Playback --version 1.0.9
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 api calls by means of a simple playback identifier.
Suitable for saving user interactions in production in order to be replayed locally, anytime and in isolation.

How to record incoming Api requests

Once your Asp.NetCore Api is configured for playback ( see sample in github repo ) you can start recording and replaying Api requests

When the X-Playback-Mode request header is set to Record the request will be saved.

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

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

When the X-Playback-Mode request header is set to Playback the request will be replayed; you should also set the x-playback-id request header with the value received during recording.

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'

Notice that the response is exactly the same has before.

When setting the x-playback-mode to None the request is not saved neither replayed.

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.

in this code excerpt we create a factory by inheriting from the FakeFactoryBase abstract class.

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

Note: this class should be registered in the Startup class IoC Container as IFakeFactory

Asp.Net Core Playback

An Asp.Net Core middleware library that simplifies the recording and playback of api calls by means of a simple playback identifier.
Suitable for saving user interactions in production in order to be replayed locally, anytime and in isolation.

How to record incoming Api requests

Once your Asp.NetCore Api is configured for playback ( see sample in github repo ) you can start recording and replaying Api requests

When the X-Playback-Mode request header is set to Record the request will be saved.

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

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

When the X-Playback-Mode request header is set to Playback the request will be replayed; you should also set the x-playback-id request header with the value received during recording.

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'

Notice that the response is exactly the same has before.

When setting the x-playback-mode to None the request is not saved neither replayed.

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.

in this code excerpt we create a factory by inheriting from the FakeFactoryBase abstract class.

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

Note: this class should be registered in the Startup class IoC Container as IFakeFactory

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