-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserTestEnvironment.cs
More file actions
57 lines (46 loc) · 1.59 KB
/
BrowserTestEnvironment.cs
File metadata and controls
57 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Net;
using System.Net.Sockets;
namespace DotPilot.UITests;
internal static class BrowserTestEnvironment
{
private const string BrowserBaseUriEnvironmentVariableName = "DOTPILOT_UITEST_BASE_URI";
private const string DefaultScheme = "http";
private const string DefaultHost = "127.0.0.1";
private const char TrailingSlash = '/';
public static string WebAssemblyUri { get; } = ResolveWebAssemblyUri();
public static string WebAssemblyUrlsValue => WebAssemblyUri.TrimEnd('/');
private static string ResolveWebAssemblyUri()
{
var configuredUri = Environment.GetEnvironmentVariable(BrowserBaseUriEnvironmentVariableName);
if (!string.IsNullOrWhiteSpace(configuredUri) &&
Uri.TryCreate(configuredUri, UriKind.Absolute, out var absoluteUri))
{
return NormalizeUri(absoluteUri);
}
return NormalizeUri(CreateLoopbackUri(GetFreeTcpPort()));
}
private static Uri CreateLoopbackUri(int port)
{
return new UriBuilder(DefaultScheme, DefaultHost, port).Uri;
}
private static int GetFreeTcpPort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
try
{
return ((IPEndPoint)listener.LocalEndpoint).Port;
}
finally
{
listener.Stop();
}
}
private static string NormalizeUri(Uri uri)
{
var absoluteUri = uri.AbsoluteUri;
return absoluteUri.EndsWith(TrailingSlash)
? absoluteUri
: string.Concat(absoluteUri, TrailingSlash);
}
}