pmilet.Playback
1.0.7
See the version list below for details.
dotnet add package pmilet.Playback --version 1.0.7
NuGet\Install-Package pmilet.Playback -Version 1.0.7
<PackageReference Include="pmilet.Playback" Version="1.0.7" />
paket add pmilet.Playback --version 1.0.7
#r "nuget: pmilet.Playback, 1.0.7"
// Install pmilet.Playback as a Cake Addin #addin nuget:?package=pmilet.Playback&version=1.0.7 // Install pmilet.Playback as a Cake Tool #tool nuget:?package=pmilet.Playback&version=1.0.7
Asp.Net Core Playback
An Asp.Net Core middleware library that simplifies the recording and playback of WebApi incoming requests and incoming responses from outgoing requests. Suitable for saving user interactions in production to be replayed in development.
Three usage scenarios
- Record incoming Api requestsin order to reproduce for testing or troubleshooting.
- Fake Api responses in order to quickly design your rest api interface .
- Prepare your service proxy to record incoming responses from outgoing calls (to external systems or services).
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:
"PlaybackBlobStorage": { "ConnectionString": "UseDevelopmentStorage=true", "ContainerName": "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:
- Navigate to swagger UI
- Set the X-Playback-Mode request header to Record and try-out
- Copy the X-Playback-Id value
- Set the X-Playback-Mode to Playback and try-out.
- You should receive the same result has in step 2.
Use case 2 : 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 in this example the when requesting the uri: /api/hello with GET verb a Hello Fake string is returned
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:
- Navigate to swagger UI
- Set X-Playback-Mode header to Fake and try-out
- You should receive the faked response.
Use case 3 : 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;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.1 is compatible. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.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. |
-
- Microsoft.AspNetCore (>= 1.1.3)
- Microsoft.AspNetCore.WebUtilities (>= 1.1.2)
- Newtonsoft.Json (>= 10.0.3)
- Swashbuckle.AspNetCore.Swagger (>= 1.0.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 1.0.0)
- WindowsAzure.Storage (>= 8.1.4)
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,647 | 4/1/2019 |
1.2.1 | 1,215 | 2/19/2018 |
1.2.0 | 1,056 | 2/19/2018 |
1.1.2 | 1,146 | 1/23/2018 |
1.1.1 | 1,219 | 1/23/2018 |
1.1.0 | 1,072 | 1/23/2018 |
1.0.10 | 2,023 | 10/27/2017 |
1.0.9 | 1,488 | 10/25/2017 |
1.0.8 | 1,143 | 10/25/2017 |
1.0.7 | 1,296 | 10/22/2017 |
1.0.6 | 1,073 | 10/21/2017 |
1.0.5 | 989 | 10/21/2017 |
1.0.4 | 1,010 | 10/21/2017 |
1.0.3 | 990 | 10/18/2017 |
1.0.2 | 987 | 10/18/2017 |
1.0.1 | 986 | 10/18/2017 |
1.0.0 | 862 | 10/15/2017 |
netstandard 2.0