Skip to content

Commit 26fd2e3

Browse files
Add flag to enable running tests with Fiddler (Azure#27392)
* Add flag to enable running tests with Fiddler * PR fb * Fix readme * fix * one more
1 parent d3356f8 commit 26fd2e3

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

eng/nunit.runsettings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
<!-- Change the test mode -->
1818
<!-- <Parameter name="TestMode" value="Record" /> -->
19+
20+
<!-- Enable running Fiddler -->
21+
<!-- <Parameter name="EnableFiddler" value="true" /> -->
1922
</TestRunParameters>
2023

2124
</RunSettings>

sdk/core/Azure.Core.TestFramework/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ You can use `if (Mode == RecordingMode.Playback) { ... }` to change behavior for
302302

303303
You can use `using (Recording.DisableRecording()) { ... }` to disable recording in the code block (useful for polling methods)
304304

305+
In order to enable testing with Fiddler, you can either set the `AZURE_ENABLE_FIDDLER` environment variable or the `EnableFiddler` [runsetting](https://github.com/Azure/azure-sdk-for-net/blob/main/eng/nunit.runsettings) parameter to `true`.
306+
305307
## Support multi service version testing
306308

307309
To enable multi-version testing, add the `ClientTestFixture` attribute listing to all the service versions to the test class itself or a base class:

sdk/core/Azure.Core.TestFramework/src/ProxyTransport.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,30 @@ public class ProxyTransport : HttpPipelineTransport
1919
private readonly TestProxy _proxy;
2020
private readonly bool _isWebRequestTransport;
2121

22-
private static readonly RemoteCertificateValidationCallback ServerCertificateCustomValidationCallback =
23-
(_, certificate, _, _) => certificate.Issuer == TestProxy.DevCertIssuer;
22+
private const string DevCertIssuer = "CN=localhost";
23+
private const string FiddlerCertIssuer = "CN=DO_NOT_TRUST_FiddlerRoot, O=DO_NOT_TRUST, OU=Created by http://www.fiddler2.com";
24+
25+
private readonly RemoteCertificateValidationCallback _serverCertificateCustomValidationCallback;
2426

2527
private readonly Func<EntryRecordModel> _filter;
28+
private readonly string _proxyHost;
29+
30+
private static bool s_useFiddler => TestEnvironment.EnableFiddler;
2631

2732
public ProxyTransport(TestProxy proxy, HttpPipelineTransport transport, TestRecording recording, Func<EntryRecordModel> filter)
2833
{
34+
_recording = recording;
35+
_proxy = proxy;
36+
_filter = filter;
37+
38+
string certIssuer = s_useFiddler ? FiddlerCertIssuer : DevCertIssuer;
39+
_proxyHost = s_useFiddler ? "localhost.fiddler" : TestProxy.IpAddress;
40+
2941
if (transport is HttpClientTransport)
3042
{
3143
var handler = new HttpClientHandler
3244
{
33-
ServerCertificateCustomValidationCallback = (_, certificate, _, _) => certificate.Issuer == TestProxy.DevCertIssuer
45+
ServerCertificateCustomValidationCallback = (_, certificate, _, _) => certificate.Issuer == certIssuer
3446
};
3547
_innerTransport = new HttpClientTransport(handler);
3648
}
@@ -39,10 +51,8 @@ public ProxyTransport(TestProxy proxy, HttpPipelineTransport transport, TestReco
3951
{
4052
_isWebRequestTransport = true;
4153
_innerTransport = transport;
54+
_serverCertificateCustomValidationCallback = (_, certificate, _, _) => certificate.Issuer == certIssuer;
4255
}
43-
_recording = recording;
44-
_proxy = proxy;
45-
_filter = filter;
4656
}
4757

4858
public override void Process(HttpMessage message) =>
@@ -80,7 +90,7 @@ private async Task ProcessAsyncInternalAsync(HttpMessage message, bool async)
8090

8191
if (_isWebRequestTransport)
8292
{
83-
ServicePointManager.ServerCertificateValidationCallback -= ServerCertificateCustomValidationCallback;
93+
ServicePointManager.ServerCertificateValidationCallback -= _serverCertificateCustomValidationCallback;
8494
}
8595
}
8696
}
@@ -159,13 +169,12 @@ private void RedirectToTestProxy(HttpMessage message)
159169
};
160170
request.Headers.SetValue("x-recording-upstream-base-uri", baseUri.ToString());
161171

162-
// for some reason using localhost instead of the ip address causes slowness when combined with SSL callback being specified
163-
request.Uri.Host = TestProxy.IpAddress;
172+
request.Uri.Host = _proxyHost;
164173
request.Uri.Port = request.Uri.Scheme == "https" ? _proxy.ProxyPortHttps.Value : _proxy.ProxyPortHttp.Value;
165174

166175
if (_isWebRequestTransport)
167176
{
168-
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateCustomValidationCallback;
177+
ServicePointManager.ServerCertificateValidationCallback += _serverCertificateCustomValidationCallback;
169178
}
170179
}
171180
}

sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,22 @@ internal static bool GlobalDisableAutoRecording
632632
}
633633
}
634634

635+
/// <summary>
636+
/// Determines whether to enable the test framework to proxy traffic through fiddler.
637+
/// </summary>
638+
internal static bool EnableFiddler
639+
{
640+
get
641+
{
642+
string switchString = TestContext.Parameters["EnableFiddler"] ??
643+
Environment.GetEnvironmentVariable("AZURE_ENABLE_FIDDLER");
644+
645+
bool.TryParse(switchString, out bool enableFiddler);
646+
647+
return enableFiddler;
648+
}
649+
}
650+
635651
private void BootStrapTestResources()
636652
{
637653
lock (s_syncLock)

sdk/core/Azure.Core.TestFramework/src/TestProxy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public class TestProxy
2424
{
2525
private static readonly string s_dotNetExe;
2626

27-
public const string DevCertIssuer = "CN=localhost";
28-
27+
// for some reason using localhost instead of the ip address causes slowness when combined with SSL callback being specified
2928
public const string IpAddress = "127.0.0.1";
3029

3130
public int? ProxyPortHttp => _proxyPortHttp;

0 commit comments

Comments
 (0)