From 639c95d71d8f1ad4445afaf4ebd7812a4035d01b Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 14 Jul 2023 12:51:59 -0700 Subject: [PATCH] Add recording file location (#6517) * 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 <45376673+scbedd@users.noreply.github.com> --- .../Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs | 12 ++++++++---- .../Azure.Sdk.Tools.TestProxy.Tests/RecordTests.cs | 6 +++--- tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md | 2 ++ .../Azure.Sdk.Tools.TestProxy/RecordingHandler.cs | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs index 5022d3f6f4b..1b8fdd66406 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/PlaybackTests.cs @@ -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)); } @@ -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); } @@ -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); @@ -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); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordTests.cs index 5e08673fa99..82fdb96b679 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordTests.cs @@ -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)); } @@ -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()); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md index 307e0bd97fb..f77a1124a90 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md @@ -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. diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs index af8a23ca1c2..6c9f71d2ef5 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs @@ -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.");