Skip to content

Commit

Permalink
Add recording file location (#6517)
Browse files Browse the repository at this point in the history
* Return record file location when playback starts

Some tests have a need to parse data out of their recordings. When playback is started for a disk based recording, include a new header x-recording-file-location on the response which points to the recording.

* update readme

* Add test to make sure in-memory recordings aren't returning the location header

* `IHeaderDictionary` returns an empty string when the header is missing, so `Assert.NotNull` will not catch bugs.

* Update tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs

Co-authored-by: Scott Beddall <[email protected]>
  • Loading branch information
billwert and scbedd authored Jul 14, 2023
1 parent 1ca0861 commit 639c95d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public async void TestStartPlaybackSimple()
await controller.Start();

var value = httpContext.Response.Headers["x-recording-id"].ToString();
Assert.NotNull(value);
var recordLocation = httpContext.Response.Headers["x-recording-file-location"].ToString();
Assert.False(String.IsNullOrEmpty(value));
Assert.False(String.IsNullOrEmpty(recordLocation));
Assert.True(testRecordingHandler.PlaybackSessions.ContainsKey(value));
}

Expand Down Expand Up @@ -74,7 +76,9 @@ public async void TestStartPlaybackInMemory()
await playbackController.Start();

var value = playbackContext.Response.Headers["x-recording-id"].ToString();
Assert.NotNull(value);
var recordLocation = playbackContext.Response.Headers["x-recording-file-location"].ToString();
Assert.False(String.IsNullOrEmpty(value));
Assert.True(String.IsNullOrEmpty(recordLocation));
Assert.True(testRecordingHandler.PlaybackSessions.ContainsKey(value));
Assert.True(testRecordingHandler.InMemorySessions.Count() == 1);
}
Expand Down Expand Up @@ -206,7 +210,7 @@ public async Task TestPlaybackSetsRetryAfterToZero()
await controller.Start();

var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();
Assert.NotNull(recordingId);
Assert.False(String.IsNullOrEmpty(recordingId));
Assert.True(testRecordingHandler.PlaybackSessions.ContainsKey(recordingId));
var entry = testRecordingHandler.PlaybackSessions[recordingId].Session.Entries[0];
HttpRequest request = TestHelpers.CreateRequestFromEntry(entry);
Expand Down Expand Up @@ -241,7 +245,7 @@ public async Task TestPlaybackWithGZippedContentPlayback()
await controller.Start();

var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();
Assert.NotNull(recordingId);
Assert.False(String.IsNullOrEmpty(recordingId));
Assert.True(testRecordingHandler.PlaybackSessions.ContainsKey(recordingId));
var entry = testRecordingHandler.PlaybackSessions[recordingId].Session.Entries[0];
HttpRequest request = TestHelpers.CreateRequestFromEntry(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task TestStartRecordSimple()
};
await controller.Start();
var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();
Assert.NotNull(recordingId);
Assert.False(string.IsNullOrEmpty(recordingId));
Assert.True(testRecordingHandler.RecordingSessions.ContainsKey(recordingId));
}

Expand All @@ -62,9 +62,9 @@ public async Task TestStartRecordInMemory()

[Theory]
[InlineData("recordings/TestStartRecordSimple_nosave.json", "request-response")]
[InlineData("recordings/TestStartRecordSimplé_nosave.json", "request-response")]
[InlineData("recordings/TestStartRecordSimplé_nosave.json", "request-response")]
[InlineData("recordings/TestStartRecordSimple.json", "")]
[InlineData("recordings/TestStartRecordSimplé.json", "")]
[InlineData("recordings/TestStartRecordSimplé.json", "")]
public async Task TestStopRecordingSimple(string targetFile, string additionalEntryModeHeader)
{
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
Expand Down
2 changes: 2 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ To start an individual test recording or playback, users will POST to a route on

You will receive a recordingId in the reponse under header `x-recording-id`. This value should be included under header `x-recording-id` in all further requests.

For playback, you will receive the location of the recording file (if it is on disk) in the header `x-recording-file-location`.

### Run your tests

The implicit assumption about this proxy is that you as a dev have _some way_ to reroute your existing requests (with some header additions) to the test-proxy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public async Task StartPlaybackAsync(string sessionId, HttpResponse outgoingResp
{
await RestoreAssetsJson(assetsPath, true);
var path = await GetRecordingPath(sessionId, assetsPath);

outgoingResponse.Headers.Add("x-recording-file-location", path);
if (!File.Exists(path))
{
throw new TestRecordingMismatchException($"Recording file path {path} does not exist.");
Expand Down

0 comments on commit 639c95d

Please sign in to comment.