Skip to content

Commit

Permalink
[Test-Proxy] During recording, re-use existing request body (#5304)
Browse files Browse the repository at this point in the history
* during recording, replay the body as it is exactly
* add test to ensure CompressionUtilities.DecompressBinary doesn't have a secondary affect on the input array

Co-authored-by: JoshLove-msft <[email protected]>
  • Loading branch information
scbedd and JoshLove-msft authored Feb 2, 2023
1 parent 861e768 commit 684d78b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Models;
using Azure.Sdk.Tools.TestProxy.Sanitizers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Azure.Sdk.Tools.TestProxy.Tests
{
public class CompressionUtilityTests
{
[Fact]
public void EnsureDecompressionPristineBytes()
{
// generate
byte[] uncompressedBody = Encoding.UTF8.GetBytes("\"{\\u0022TableName\\u0022: \\u0022listtable09bf2a3d\\u0022}\"");
byte[] compressedBody = CompressionUtilities.CompressBodyCore(uncompressedBody, new string[] { "gzip" });

byte[] savedCompressedBody = new byte[compressedBody.Length];
compressedBody.CopyTo(savedCompressedBody, 0);

var headerDict = new HeaderDictionary();
headerDict.Add("Content-Encoding", new string[1] { "gzip" });

// intentionally testing DecompressBody vs DecompressBodyCore, as that is where the header values are intercepted and treated differently
byte[] decompressedResult = CompressionUtilities.DecompressBody(compressedBody, headerDict);


Assert.Equal(compressedBody, savedCompressedBody);
Assert.NotEqual(decompressedResult, compressedBody);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public async Task CreateEntryUsesAbsoluteUri()
request.Path = uri.PathAndQuery;
request.Headers["x-recording-upstream-base-uri"] = uri.AbsoluteUri;

var entry = await RecordingHandler.CreateEntryAsync(request);
var entry = (await RecordingHandler.CreateEntryAsync(request)).Item1;
Assert.Equal(uri.AbsoluteUri, entry.RequestUri);
}

Expand Down
10 changes: 5 additions & 5 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ public async Task HandleRecordRequestAsync(string recordingId, HttpRequest incom
throw new HttpException(HttpStatusCode.BadRequest, $"There is no active recording session under id {recordingId}.");
}

var entry = await CreateEntryAsync(incomingRequest).ConfigureAwait(false);
(RecordEntry entry, byte[] requestBody) = await CreateEntryAsync(incomingRequest).ConfigureAwait(false);

var upstreamRequest = CreateUpstreamRequest(incomingRequest, CompressionUtilities.CompressBody(entry.Request.Body, entry.Request.Headers));
var upstreamRequest = CreateUpstreamRequest(incomingRequest, requestBody);

HttpResponseMessage upstreamResponse = null;

Expand Down Expand Up @@ -432,7 +432,7 @@ public async Task HandlePlaybackRequest(string recordingId, HttpRequest incoming
throw new HttpException(HttpStatusCode.BadRequest, $"There is no active playback session under recording id {recordingId}.");
}

var entry = await CreateEntryAsync(incomingRequest).ConfigureAwait(false);
var entry = (await CreateEntryAsync(incomingRequest).ConfigureAwait(false)).Item1;

// If request contains "x-recording-remove: false", then request is not removed from session after playback.
// Used by perf tests to play back the same request multiple times.
Expand Down Expand Up @@ -470,7 +470,7 @@ public async Task HandlePlaybackRequest(string recordingId, HttpRequest incoming
}
}

public static async Task<RecordEntry> CreateEntryAsync(HttpRequest request)
public static async Task<(RecordEntry, byte[])> CreateEntryAsync(HttpRequest request)
{
var entry = new RecordEntry();
entry.RequestUri = GetRequestUri(request).AbsoluteUri;
Expand All @@ -487,7 +487,7 @@ public static async Task<RecordEntry> CreateEntryAsync(HttpRequest request)
byte[] bytes = await ReadAllBytes(request.Body).ConfigureAwait(false);

entry.Request.Body = CompressionUtilities.DecompressBody(bytes, request.Headers);
return entry;
return (entry, bytes);
}

#endregion
Expand Down

0 comments on commit 684d78b

Please sign in to comment.