Skip to content

Commit

Permalink
Make the test proxy URL configurable via environment or build variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-msft committed Apr 5, 2024
1 parent 26250eb commit 6b45609
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
9 changes: 4 additions & 5 deletions src/testing/adapters/recordingadapter/RecordedTestObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -36,7 +35,7 @@ public void RecordStart(TestCase testCase)

if (_mode != RecordedTestMode.Live)
{
Environment.SetEnvironmentVariable("HTTPS_PROXY", "http://localhost:5004");
Environment.SetEnvironmentVariable("HTTPS_PROXY", TestProxyClient.BaseUrl);
foreach (var trait in testCase.Traits.Where((Trait t) => t.Name.Equals("_sanitize", StringComparison.OrdinalIgnoreCase)))
{
var sanitizeJson = trait.Value;
Expand Down Expand Up @@ -80,7 +79,7 @@ private async Task AddSanitizer(string sanitizeJson)
{
foreach (var uri in currentElement.EnumerateArray())
{
await TestProxyClient.AddUriSinatizer(uri.GetProperty("regex").GetString(), uri.GetProperty("value").GetString());
await TestProxyClient.AddUriSanitizer(uri.GetProperty("regex").GetString(), uri.GetProperty("value").GetString());
}
}
else if (sanitizeLine.TryGetProperty("body", out currentElement))
Expand All @@ -106,12 +105,12 @@ public void RecordEnd(TestCase testCase, TestOutcome outcome)
case RecordedTestMode.Record:
TestProxyClient.StopRecording(_id).Wait();
Environment.SetEnvironmentVariable("HTTPS_PROXY", null);
TestProxyClient.ClearSanatizers().Wait();
TestProxyClient.ClearSanitizers().Wait();
break;
case RecordedTestMode.Playback:
TestProxyClient.StopPlayback(_id).Wait();
Environment.SetEnvironmentVariable("HTTPS_PROXY", null);
TestProxyClient.ClearSanatizers().Wait();
TestProxyClient.ClearSanitizers().Wait();
break;
case RecordedTestMode.Live:
// Live test
Expand Down
58 changes: 29 additions & 29 deletions src/testing/adapters/recordingadapter/TestProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -18,38 +17,39 @@ namespace YamlTestAdapter
{
public class TestProxyClient
{
private const string _proxy = "http://localhost:5004";

private static readonly HttpClient _httpClient = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
});

public enum SanatizeLocation
public enum SanitizeLocation
{
Header,
Body,
Uri
}

public static string BaseUrl => Environment.GetEnvironmentVariable("TEST_PROXY_URL")
?? "http://localhost:5004";

/*
private static async Task Record()
{
var recordingId = await StartRecording();
await SendRequest(recordingId, "record");
await Task.Delay(TimeSpan.FromSeconds(2));
await SendRequest(recordingId, "record");
await StopRecording(recordingId);
}
private static async Task Playback()
{
var recordingId = await StartPlayback();
await SendRequest(recordingId, "playback");
await SendRequest(recordingId, "playback");
await StopPlayback(recordingId);
}
*/
private static async Task Record()
{
var recordingId = await StartRecording();
await SendRequest(recordingId, "record");
await Task.Delay(TimeSpan.FromSeconds(2));
await SendRequest(recordingId, "record");
await StopRecording(recordingId);
}
private static async Task Playback()
{
var recordingId = await StartPlayback();
await SendRequest(recordingId, "playback");
await SendRequest(recordingId, "playback");
await StopPlayback(recordingId);
}
*/

public static Process InvokeProxy()
{
Expand All @@ -72,7 +72,7 @@ public static async Task<string> StartPlayback(string recordingFile)
{
Console.WriteLine($"StartPlayback {recordingFile}");

var message = new HttpRequestMessage(HttpMethod.Post, _proxy + "/playback/start");
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + "/playback/start");

var json = "{\"x-recording-file\":\"" + recordingFile + "\"}";
var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
Expand Down Expand Up @@ -100,7 +100,7 @@ public static async Task StopPlayback(string recordingId)
Console.WriteLine($"StopPlayback {recordingId}");
Console.WriteLine();

var message = new HttpRequestMessage(HttpMethod.Post, _proxy + "/playback/stop");
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + "/playback/stop");
message.Headers.Add("x-recording-id", recordingId);

var response = await _httpClient.SendAsync(message);
Expand All @@ -119,7 +119,7 @@ public static async Task<string> StartRecording(string recordingFile)
{
Console.WriteLine($"StartRecording {recordingFile}");

var message = new HttpRequestMessage(HttpMethod.Post, _proxy + "/record/start");
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + "/record/start");

var json = "{\"x-recording-file\":\"" + recordingFile + "\"}";
var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
Expand Down Expand Up @@ -148,7 +148,7 @@ public static async Task StopRecording(string recordingId)
Console.WriteLine($"StopRecording {recordingId}");
Console.WriteLine();

var message = new HttpRequestMessage(HttpMethod.Post, _proxy + "/record/stop");
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + "/record/stop");
message.Headers.Add("x-recording-id", recordingId);
message.Headers.Add("x-recording-save", bool.TrueString);

Expand All @@ -164,7 +164,7 @@ public static async Task StopRecording(string recordingId)
}
}

public static Task AddUriSinatizer(string regexToMatch, string replaceValue) => AddSanitizer("UriRegexSanitizer", new JsonObject() { ["value"] = replaceValue, ["regex"] = regexToMatch });
public static Task AddUriSanitizer(string regexToMatch, string replaceValue) => AddSanitizer("UriRegexSanitizer", new JsonObject() { ["value"] = replaceValue, ["regex"] = regexToMatch });

public static Task AddHeaderSanitizer(string key, string regex, string value)
{
Expand All @@ -184,7 +184,7 @@ public static Task AddHeaderSanitizer(string key, string regex, string value)
private static async Task AddSanitizer(string headerName, JsonObject json)
{
var url = "/admin/addsanitizer";
var message = new HttpRequestMessage(HttpMethod.Post, _proxy + url);
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + url);
message.Headers.Add("x-abstraction-identifier", headerName);
message.Content = new StringContent(json.ToJsonString(), Encoding.UTF8, "application/json");
var result = await _httpClient.SendAsync(message);
Expand All @@ -204,10 +204,10 @@ private static async Task AddSanitizer(string headerName, JsonObject json)
}
}

public static async Task ClearSanatizers()
public static async Task ClearSanitizers()
{
var url = "/admin/reset";
var message = new HttpRequestMessage(HttpMethod.Post, _proxy + url);
var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + url);
var result = await _httpClient.SendAsync(message);

if (result.StatusCode != HttpStatusCode.OK)
Expand Down

0 comments on commit 6b45609

Please sign in to comment.