Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All semaphor all the time #8755

Merged
merged 9 commits into from
Aug 1, 2024
68 changes: 34 additions & 34 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/AdminTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async void TestAddSanitizersThrowsOnEmptyArray()

httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody(requestBody);
httpContext.Request.ContentLength = httpContext.Request.Body.Length;
testRecordingHandler.SanitizerRegistry.ResetSessionSanitizers();
await testRecordingHandler.SanitizerRegistry.ResetSessionSanitizers();

var controller = new Admin(testRecordingHandler, _nullLogger)
{
Expand All @@ -64,7 +64,7 @@ public async void TestAddSanitizersHandlesPopulatedArray()
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
var httpContext = new DefaultHttpContext();

var defaultSessionSanitizers = testRecordingHandler.SanitizerRegistry.GetSanitizers();
var defaultSessionSanitizers = await testRecordingHandler.SanitizerRegistry.GetSanitizers();

string requestBody = @"[
{
Expand All @@ -88,7 +88,7 @@ public async void TestAddSanitizersHandlesPopulatedArray()

httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody(requestBody);
httpContext.Request.ContentLength = httpContext.Request.Body.Length;
testRecordingHandler.SanitizerRegistry.ResetSessionSanitizers();
await testRecordingHandler.SanitizerRegistry.ResetSessionSanitizers();
httpContext.Response.Body = new MemoryStream();

var controller = new Admin(testRecordingHandler, _nullLogger)
Expand All @@ -100,7 +100,7 @@ public async void TestAddSanitizersHandlesPopulatedArray()
};
await controller.AddSanitizers();

var amendedSessionSanitizers = testRecordingHandler.SanitizerRegistry.GetSanitizers();
var amendedSessionSanitizers = await testRecordingHandler.SanitizerRegistry.GetSanitizers();

Assert.Equal(defaultSessionSanitizers.Count + 2, amendedSessionSanitizers.Count);
Assert.True(amendedSessionSanitizers[defaultSessionSanitizers.Count] is GeneralRegexSanitizer);
Expand Down Expand Up @@ -144,7 +144,7 @@ public async void TestAddSanitizersThrowsOnSingleBadInput()

httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody(requestBody);
httpContext.Request.ContentLength = httpContext.Request.Body.Length;
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var controller = new Admin(testRecordingHandler, _nullLogger)
{
Expand Down Expand Up @@ -174,7 +174,7 @@ public async void TestAddSanitizerThrowsOnInvalidAbstractionId()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand All @@ -195,7 +195,7 @@ public async void TestAddSanitizerThrowsOnEmptyAbstractionId()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand Down Expand Up @@ -302,7 +302,7 @@ public async Task TestSetMatcher()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.SetMatcher();

var result = testRecordingHandler.Matcher;
Expand Down Expand Up @@ -433,7 +433,7 @@ public async void TestAddSanitizer()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.AddSanitizer();

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
Expand All @@ -442,7 +442,7 @@ public async void TestAddSanitizer()
Assert.Equal((int)HttpStatusCode.OK, httpContext.Response.StatusCode);
Assert.True(!string.IsNullOrWhiteSpace(response.RootElement.GetProperty("Sanitizer").GetString()));

var result = testRecordingHandler.SanitizerRegistry.GetSanitizers().First();
var result = (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).First();
Assert.True(result is HeaderRegexSanitizer);
}

Expand All @@ -464,10 +464,10 @@ public async void TestAddSanitizerWithOddDefaults()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.AddSanitizer();

var result = testRecordingHandler.SanitizerRegistry.GetSanitizers().First();
var result = (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).First();
Assert.True(result is BodyKeySanitizer);
}

Expand All @@ -487,7 +487,7 @@ public async void TestAddSanitizerWrongEmptyValue()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand All @@ -511,10 +511,10 @@ public async void TestAddSanitizerAcceptableEmptyValue()
HttpContext = httpContext
}
};
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.AddSanitizer();

var result = testRecordingHandler.SanitizerRegistry.GetSanitizers().First();
var result = (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).First();
Assert.True(result is HeaderRegexSanitizer);
}

Expand All @@ -541,7 +541,7 @@ public async void TestAddSanitizerIndividualRecording()
};
await controller.AddSanitizer();

var result = testRecordingHandler.SanitizerRegistry.GetSanitizers(testRecordingHandler.PlaybackSessions[recordingId]).Last();
var result = (await testRecordingHandler.SanitizerRegistry.GetSanitizers(testRecordingHandler.PlaybackSessions[recordingId])).Last();

Assert.True(result is HeaderRegexSanitizer);
}
Expand Down Expand Up @@ -588,7 +588,7 @@ public async Task GenerateInstanceThrowsOnBadBodyFormat()
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand All @@ -615,7 +615,7 @@ public async Task AddSanitizerThrowsOnAdditionOfBadRegex()
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand Down Expand Up @@ -778,12 +778,12 @@ public async Task TestAddSanitizerWithInvalidConditionJson(string requestBody, s
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
);
Assert.Equal(HttpStatusCode.BadRequest, assertion.StatusCode);
Assert.Empty(testRecordingHandler.SanitizerRegistry.GetSanitizers());
Assert.Empty(await testRecordingHandler.SanitizerRegistry.GetSanitizers());
Assert.Contains(errorText, assertion.Message);
}

Expand All @@ -807,10 +807,10 @@ public async Task TestAddSanitizerWithValidUriRegexCondition(string requestBody,
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.AddSanitizer();

var appliedSanitizers = testRecordingHandler.SanitizerRegistry.GetSanitizers();
var appliedSanitizers = await testRecordingHandler.SanitizerRegistry.GetSanitizers();

Assert.Single(appliedSanitizers);
Assert.True(appliedSanitizers.First() is GeneralRegexSanitizer);
Expand Down Expand Up @@ -896,7 +896,7 @@ public async Task TestAddSanitizerThrowsOnMissingRequiredArgument()
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();

var assertion = await Assert.ThrowsAsync<HttpException>(
async () => await controller.AddSanitizer()
Expand Down Expand Up @@ -926,10 +926,10 @@ public async Task TestAddSanitizerContinuesWithTwoRequiredParams()
}
};

testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await controller.AddSanitizer();

var addedSanitizer = testRecordingHandler.SanitizerRegistry.GetSanitizers().First();
var addedSanitizer = (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).First();
Assert.True(addedSanitizer is HeaderStringSanitizer);

var actualTargetString = (string)typeof(HeaderStringSanitizer).GetField("_targetValue", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(addedSanitizer);
Expand Down Expand Up @@ -1025,7 +1025,7 @@ public async Task RemoveSanitizerSucceedsForExistingSessionSanitizer()
}
};

var expectedSanitizerCount = testRecordingHandler.SanitizerRegistry.GetSanitizers().Count;
var expectedSanitizerCount = (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).Count;
await controller.RemoveSanitizers(new RemoveSanitizerList() { Sanitizers = new List<string>() { "AZSDK0000" } });

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
Expand All @@ -1038,14 +1038,14 @@ public async Task RemoveSanitizerSucceedsForExistingSessionSanitizer()
Assert.Single(returnedSanitizerIds);
Assert.Equal("AZSDK0000", returnedSanitizerIds.First());

Assert.Equal(expectedSanitizerCount - 1, testRecordingHandler.SanitizerRegistry.GetSanitizers().Count);
Assert.Equal(expectedSanitizerCount - 1, (await testRecordingHandler.SanitizerRegistry.GetSanitizers()).Count);
}

[Fact]
public async Task RemoveSanitizerSucceedsForAddedRecordingSanitizer()
{
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
var httpContext = new DefaultHttpContext();
await testRecordingHandler.StartPlaybackAsync("Test.RecordEntries/oauth_request_with_variables.json", httpContext.Response);
var recordingId = httpContext.Response.Headers["x-recording-id"];
Expand All @@ -1062,11 +1062,11 @@ public async Task RemoveSanitizerSucceedsForAddedRecordingSanitizer()
};
var session = testRecordingHandler.GetActiveSession(recordingId);

var forRemoval = testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Content-Type"), recordingId);
testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"), recordingId);
var forRemoval = await testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Content-Type"), recordingId);
await testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"), recordingId);
await controller.RemoveSanitizers(new RemoveSanitizerList() { Sanitizers = new List<string>() { forRemoval } });

var activeRecordingSanitizers = testRecordingHandler.SanitizerRegistry.GetSanitizers(session);
var activeRecordingSanitizers = await testRecordingHandler.SanitizerRegistry.GetSanitizers(session);

Assert.Single(activeRecordingSanitizers);
var privateSetting = (string)typeof(HeaderRegexSanitizer).GetField("_targetKey", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(activeRecordingSanitizers.First());
Expand All @@ -1077,7 +1077,7 @@ public async Task RemoveSanitizerSucceedsForAddedRecordingSanitizer()
public async void GetSanitizersReturnsSessionSanitizers()
{
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"));
await testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"));

var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();
Expand Down Expand Up @@ -1112,7 +1112,7 @@ public async Task GetSanitizersReturnsRecordingSanitizers()
{
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
var httpContext = new DefaultHttpContext();
testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.StartPlaybackAsync("Test.RecordEntries/oauth_request_with_variables.json", httpContext.Response);
var recordingId = httpContext.Response.Headers["x-recording-id"];
httpContext.Request.Headers["x-recording-id"] = recordingId;
Expand All @@ -1125,7 +1125,7 @@ public async Task GetSanitizersReturnsRecordingSanitizers()
}
};

testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"), recordingId);
await testRecordingHandler.RegisterSanitizer(new HeaderRegexSanitizer("Connection"), recordingId);
await controller.GetSanitizers();

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
Expand Down
29 changes: 17 additions & 12 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/InfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ namespace Azure.Sdk.Tools.TestProxy.Tests
{
public class InfoTests
{
private int DefaultExtensionCount { get { return new RecordingHandler(null).SanitizerRegistry.GetSanitizers().Count; } }

private async Task<int> GetDefaultExtensionCount(){
var handler = new RecordingHandler(null);
return (await handler.SanitizerRegistry.GetSanitizers()).Count;
}

[Fact]
public void TestReflectionModelBuild()
{
Expand All @@ -45,12 +48,12 @@ public void TestReflectionModelBuild()
}

[Fact]
public void TestReflectionModelWithAdvancedType()
public async void TestReflectionModelWithAdvancedType()
{
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
var httpContext = new DefaultHttpContext();
testRecordingHandler.SanitizerRegistry.Clear();
testRecordingHandler.SanitizerRegistry.Register(new GeneralRegexSanitizer(value: "A new value", condition: new ApplyCondition() { UriRegex= ".+/Tables" }));
await testRecordingHandler.SanitizerRegistry.Clear();
await testRecordingHandler.SanitizerRegistry.Register(new GeneralRegexSanitizer(value: "A new value", condition: new ApplyCondition() { UriRegex= ".+/Tables" }));

var controller = new Info(testRecordingHandler)
{
Expand All @@ -74,24 +77,26 @@ public async Task TestReflectionModelWithTargetRecordSession()

var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();

testRecordingHandler.RegisterSanitizer(new UriRegexSanitizer(regex: "ABC123"), recordingId);
testRecordingHandler.RegisterSanitizer(new BodyRegexSanitizer(regex: ".+?"), recordingId);
await testRecordingHandler.RegisterSanitizer(new UriRegexSanitizer(regex: "ABC123"), recordingId);
await testRecordingHandler.RegisterSanitizer(new BodyRegexSanitizer(regex: ".+?"), recordingId);
testRecordingHandler.SetMatcherForRecording(recordingId, new CustomDefaultMatcher(compareBodies: false, excludedHeaders: "an-excluded-header"));

var model = new ActiveMetadataModel(testRecordingHandler, recordingId);
var descriptions = model.Descriptions.ToList();

int defaultExtensionCount = await GetDefaultExtensionCount();

// we should have exactly DefaultExtensionCount + 2 if we're counting all the customizations appropriately
Assert.True(descriptions.Count == DefaultExtensionCount + 3);
Assert.True(descriptions.Count == defaultExtensionCount + 3);
Assert.True(model.Matchers.Count() == 1);
Assert.True(model.Sanitizers.Count() == DefaultExtensionCount + 2);
Assert.True(model.Sanitizers.Count() == defaultExtensionCount + 2);

// confirm that the overridden matcher is showing up
Assert.True(descriptions[DefaultExtensionCount].ConstructorDetails.Arguments[1].Item2 == "\"ABC123\"");
Assert.True(descriptions[DefaultExtensionCount + 1].ConstructorDetails.Arguments[1].Item2 == "\".+?\"");
Assert.True(descriptions[defaultExtensionCount].ConstructorDetails.Arguments[1].Item2 == "\"ABC123\"");
Assert.True(descriptions[defaultExtensionCount + 1].ConstructorDetails.Arguments[1].Item2 == "\".+?\"");

// and finally confirm our sanitizers are what we expect
Assert.True(descriptions[DefaultExtensionCount + 2].Name == "CustomDefaultMatcher");
Assert.True(descriptions[defaultExtensionCount + 2].Name == "CustomDefaultMatcher");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task PlaybackLogsSanitizedRequest()
HttpResponse response = new DefaultHttpContext().Response;
await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response);

AssertLogs(logger, 5, 9, 12);
AssertLogs(logger, 4, 8, 12);
}
finally
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public async Task RecordingHandlerLogsSanitizedRequests()
var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();

await testRecordingHandler.HandleRecordRequestAsync(recordingId, request, httpContext.Response);
testRecordingHandler.StopRecording(recordingId);
await testRecordingHandler.StopRecording(recordingId);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async void TestStartPlaybackInMemory()
await recordController.Start();
var inMemId = recordContext.Response.Headers["x-recording-id"].ToString();
recordContext.Request.Headers["x-recording-id"] = new string[] { inMemId };
recordController.Stop();
await recordController.Stop();

// apply same recordingId when starting in-memory session
var playbackContext = new DefaultHttpContext();
Expand Down Expand Up @@ -175,7 +175,7 @@ public async void TestStopPlaybackInMemory()
await recordController.Start();
var inMemId = recordContext.Response.Headers["x-recording-id"].ToString();
recordContext.Request.Headers["x-recording-id"] = new string[] { inMemId };
recordController.Stop();
await recordController.Stop();

var playbackContext = new DefaultHttpContext();
playbackContext.Request.Headers["x-recording-id"] = inMemId;
Expand Down
Loading
Loading