-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/SanitizerTestExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Azure.Sdk.Tools.TestProxy.Common; | ||
using Azure.Sdk.Tools.TestProxy.Common.Exceptions; | ||
using Azure.Sdk.Tools.TestProxy.Sanitizers; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Azure.Sdk.Tools.TestProxy.Tests | ||
{ | ||
/// <summary> | ||
/// This test is provided as a preface to a "real" response in helping folks understand why their regex aren't working as they expect | ||
/// | ||
/// Users should modify "Test.RecordEntries/sample_entry.json" to match their request or response, then use the function below to test | ||
/// the regex they are attempting to register. | ||
/// | ||
/// Below a generalRegexSanitizer is being used, feel free to replace with any sanitizer provided in Azure.Sdk.Tools.TestProxy.Sanitizers. | ||
/// </summary> | ||
public class SanitizerTestExample | ||
{ | ||
[Fact] | ||
public void SanitizerWorksAgainstSample() | ||
{ | ||
var session = TestHelpers.LoadRecordSession("Test.RecordEntries/sample_entry.json"); | ||
|
||
var generalRegexSanitizer = new GeneralRegexSanitizer(value: "https://replacementvalue.table.core.windows.net", regex: "https://.*.table.core.windows.net"); | ||
|
||
session.Session.Sanitize(generalRegexSanitizer); | ||
Assert.Contains("replacementvalue", session.Session.Entries.First().RequestUri); | ||
} | ||
|
||
[Fact] | ||
public async Task APISanitizerWorksAgainstSample() | ||
{ | ||
var session = TestHelpers.LoadRecordSession("Test.RecordEntries/sample_entry.json"); | ||
|
||
// this is what your json body will look like coming over the wire. Notice the double escapes to prevent JSON parse break. | ||
// it is an identical sanitizer registration to the one above | ||
var overTheWire = "{ \"value\": \"https://replacementvalue.table.core.windows.net\", \"regex\": \"https://.*.table.core.windows.net\" }"; | ||
|
||
// Target the type of sanitizer using this. (This is similar to selecting a constructor above) | ||
var sanitizerName = "GeneralRegexSanitizer"; | ||
|
||
|
||
#region API registration and running of sanitizer | ||
// feel free to ignore this setup, bunch of implementation details to register as if coming from external request | ||
RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory()); | ||
testRecordingHandler.Sanitizers.Clear(); | ||
var httpContext = new DefaultHttpContext(); | ||
httpContext.Request.Headers["x-abstraction-identifier"] = sanitizerName; | ||
httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody(overTheWire); | ||
httpContext.Request.ContentLength = httpContext.Request.Body.Length; | ||
var controller = new Admin(testRecordingHandler, new NullLoggerFactory()) | ||
{ | ||
ControllerContext = new ControllerContext() | ||
{ | ||
HttpContext = httpContext | ||
} | ||
}; | ||
await controller.AddSanitizer(); | ||
var registeredSanitizer = testRecordingHandler.Sanitizers[0]; | ||
Assert.NotNull(registeredSanitizer); | ||
#endregion | ||
|
||
session.Session.Sanitize(registeredSanitizer); | ||
Assert.Contains("replacementvalue", session.Session.Entries.First().RequestUri); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/sample_entry.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"Entries": [ | ||
{ | ||
"RequestUri": "https://fakeazsdktestaccount.table.core.windows.net/Tables", | ||
"RequestMethod": "POST", | ||
"RequestHeaders": { | ||
"Accept": "application/json;odata=minimalmetadata", | ||
"Accept-Encoding": "gzip, deflate", | ||
"Authorization": "Sanitized", | ||
"Connection": "keep-alive", | ||
"Content-Length": "34", | ||
"Content-Type": "application/json", | ||
"Content-Encoding": "gzip", | ||
"DataServiceVersion": "3.0", | ||
"Date": "Tue, 18 May 2021 23:27:42 GMT", | ||
"User-Agent": "azsdk-python-data-tables/12.0.0b7 Python/3.8.6 (Windows-10-10.0.19041-SP0)", | ||
"x-ms-client-request-id": "a4c24b7a-b830-11eb-a05e-10e7c6392c5a", | ||
"x-ms-date": "Tue, 18 May 2021 23:27:42 GMT", | ||
"x-ms-version": "2019-02-02" | ||
}, | ||
"RequestBody": "{\u0022TableName\u0022: \u0022listtable09bf2a3d\u0022}", | ||
"StatusCode": 201, | ||
"ResponseHeaders": { | ||
"Cache-Control": "no-cache", | ||
"Content-Type": "application/json", | ||
"Content-Encoding": "gzip", | ||
"Date": "Tue, 18 May 2021 23:27:43 GMT", | ||
"Retry-After": "10", | ||
"Location": "https://fakeazsdktestaccount.table.core.windows.net/Tables(\u0027listtable09bf2a3d\u0027)", | ||
"Server": [ | ||
"Windows-Azure-Table/1.0", | ||
"Microsoft-HTTPAPI/2.0" | ||
], | ||
"Transfer-Encoding": "chunked", | ||
"X-Content-Type-Options": "nosniff", | ||
"x-ms-client-request-id": "a4c24b7a-b830-11eb-a05e-10e7c6392c5a", | ||
"x-ms-request-id": "d2270777-c002-0072-313d-4ce19f000000", | ||
"x-ms-version": "2019-02-02" | ||
}, | ||
"ResponseBody": { | ||
"odata.metadata": "https://fakeazsdktestaccount.table.core.windows.net/$metadata#Tables/@Element", | ||
"TableName": "listtable09bf2a3d", | ||
"connectionString": null | ||
} | ||
} | ||
], | ||
"Variables": {} | ||
} |