Skip to content

Commit

Permalink
[Test-Proxy] Remove Explicit List of Content-Headers (#2914)
Browse files Browse the repository at this point in the history
* remove explicit list of content headers
* adding tests to reflect logic updates of targeted content headers

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
  • Loading branch information
scbedd and JoshLove-msft authored Mar 15, 2022
1 parent 88e4798 commit 80fc220
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ namespace Azure.Sdk.Tools.TestProxy.Tests
{
public class RecordingHandlerTests
{
private HttpContext GenerateHttpRequestContext(string[] headerValueStrings)
{
HttpContext context = new DefaultHttpContext();

foreach(var hTuple in GenerateHeaderValuesTuples(headerValueStrings))
{

context.Request.Headers.TryAdd(hTuple.Item1, hTuple.Item2);
}

context.Request.Headers.TryAdd("x-recording-upstream-base-uri", new string[] { "https://hello-world" });
context.Request.Method = "POST";

return context;
}

private IEnumerable<Tuple<string, StringValues>> GenerateHeaderValuesTuples(string[] headerValueStrings) {
var returnedTuples = new List<Tuple<string, StringValues>>();
foreach (var headString in headerValueStrings)
{
var splitLocation = headString.IndexOf(':');
var headerKey = headString.Substring(0, splitLocation);
var headerValue = headString.Substring(splitLocation).Split(";").ToArray();
returnedTuples.Add(new Tuple<string, StringValues>(headerKey, headerValue));
}

return returnedTuples;
}

private NullLoggerFactory _nullLogger = new NullLoggerFactory();

Expand Down Expand Up @@ -564,6 +592,45 @@ public async Task CreateEntryUsesAbsoluteUri()
var entry = await RecordingHandler.CreateEntryAsync(request);
Assert.Equal(uri.AbsoluteUri, entry.RequestUri);
}


[Theory]
[InlineData("Content-Type:application/json; odata=minimalmetadata; streaming=true", "Accept:application/json;odata=minimalmetadata")]
[InlineData("Content-MD5:<ContentHash>", "x-ms-version:2019-02-02", "RequestMethod:POST", "Connection:keep-alive")]
[InlineData("Content-Encoding:utf-8", "x-ms-version:2019-02-02", "RequestMethod:POST", "Content-Length:50")]
public void TestCreateUpstreamRequestIncludesExpectedHeaders(params string[] incomingHeaders)
{
var requestContext = GenerateHttpRequestContext(incomingHeaders);
var recordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
var upstreamRequestContext = GenerateHttpRequestContext(incomingHeaders);

var output = recordingHandler.CreateUpstreamRequest(upstreamRequestContext.Request, new byte[] { });

// iterate across the set we know about and confirm that GenerateUpstreamRequest worked properly!
var setOfHeaders = GenerateHeaderValuesTuples(incomingHeaders);
{
foreach (var headerTuple in setOfHeaders)
{
var inContent = false;
var inStandard = false;

try
{
inContent = output.Headers.Contains(headerTuple.Item1);
}
catch (Exception) { }

try
{
inStandard = output.Content.Headers.Contains(headerTuple.Item1);
}
catch (Exception) { }

Assert.True(inContent || inStandard);
}
}
}

}

internal class MockHttpHandler : HttpMessageHandler
Expand Down
30 changes: 11 additions & 19 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ public RecordingHandler(string targetDirectory)
"Proxy-Connection",
};

// Headers which must be set on HttpContent instead of HttpRequestMessage
private static readonly string[] s_contentRequestHeaders = new string[] {
"Content-Length",
"Content-Type",
};

public List<RecordedTestSanitizer> Sanitizers { get; set; }

public List<ResponseTransform> Transforms { get; set; }
Expand Down Expand Up @@ -265,7 +259,7 @@ private byte[] DecompressBody(MemoryStream incomingBody, HttpContentHeaders head
return incomingBody.ToArray();
}

private HttpRequestMessage CreateUpstreamRequest(HttpRequest incomingRequest, byte[] incomingBody)
public HttpRequestMessage CreateUpstreamRequest(HttpRequest incomingRequest, byte[] incomingBody)
{
var upstreamRequest = new HttpRequestMessage();
upstreamRequest.RequestUri = GetRequestUri(incomingRequest);
Expand All @@ -276,29 +270,27 @@ private HttpRequestMessage CreateUpstreamRequest(HttpRequest incomingRequest, by
{
IEnumerable<string> values = header.Value;

// can't handle PROXY_CONNECTION right now.
if (s_excludedRequestHeaders.Contains(header.Key, StringComparer.OrdinalIgnoreCase))
{
continue;
}

try
if (!header.Key.StartsWith("x-recording"))
{
if (s_contentRequestHeaders.Contains(header.Key, StringComparer.OrdinalIgnoreCase))
if (upstreamRequest.Headers.TryAddWithoutValidation(header.Key, values))
{
upstreamRequest.Content.Headers.TryAddWithoutValidation(header.Key, values);
continue;
}
else

if(!upstreamRequest.Content.Headers.TryAddWithoutValidation(header.Key, values))
{
if (!header.Key.StartsWith("x-recording"))
{
upstreamRequest.Headers.TryAddWithoutValidation(header.Key, values);
}
throw new HttpException(
HttpStatusCode.BadRequest,
$"Encountered an unexpected exception while mapping a content header during upstreamRequest creation. Header: \"{header.Key}\". Value: \"{String.Join(",", values)}\""
);
}
}
catch (Exception)
{
// ignore
}
}

upstreamRequest.Headers.Host = upstreamRequest.RequestUri.Host;
Expand Down

0 comments on commit 80fc220

Please sign in to comment.