mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 02:08:29 +02:00
Add tests
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -27,9 +22,9 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
|
||||
public bool IsStarted { get; private set; }
|
||||
|
||||
public List<string> Urls { get; } = new();
|
||||
public List<string> Urls { get; } = [];
|
||||
|
||||
public List<int> Ports { get; } = new();
|
||||
public List<int> Ports { get; } = [];
|
||||
|
||||
public Exception? RunningException { get; private set; }
|
||||
|
||||
@@ -120,14 +115,42 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
{
|
||||
var addresses = _host.ServerFeatures
|
||||
.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>()!
|
||||
.Addresses;
|
||||
.Addresses
|
||||
.ToArray();
|
||||
|
||||
foreach (var address in addresses)
|
||||
if (_urlOptions.Urls == null)
|
||||
{
|
||||
Urls.Add(address.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost"));
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
PortUtils.TryExtract(address, out _, out _, out var scheme, out var host, out var port);
|
||||
|
||||
PortUtils.TryExtract(address, out _, out _, out _, out _, out var port);
|
||||
Ports.Add(port);
|
||||
var replacedHost = ReplaceHostWithLocalhost(host!);
|
||||
var newUrl = $"{scheme}://{replacedHost}:{port}";
|
||||
Urls.Add(newUrl);
|
||||
Ports.Add(port);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var urlOptions = _urlOptions?.Urls?.ToArray() ?? [];
|
||||
|
||||
for (int i = 0; i < urlOptions.Length; i++)
|
||||
{
|
||||
PortUtils.TryExtract(urlOptions[i], out _, out _, out var originalScheme, out _, out _);
|
||||
if (originalScheme!.StartsWith("grpc", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Always replace "grpc" with "http" in the scheme because GrpcChannel needs http or https.
|
||||
originalScheme = originalScheme.Replace("grpc", "http", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
PortUtils.TryExtract(addresses[i], out _, out _, out _, out var realHost, out var realPort);
|
||||
|
||||
var replacedHost = ReplaceHostWithLocalhost(realHost!);
|
||||
var newUrl = $"{originalScheme}://{replacedHost}:{realPort}";
|
||||
|
||||
Urls.Add(newUrl);
|
||||
Ports.Add(realPort);
|
||||
}
|
||||
}
|
||||
|
||||
IsStarted = true;
|
||||
@@ -159,4 +182,9 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
IsStarted = false;
|
||||
return _host.StopAsync();
|
||||
}
|
||||
|
||||
private static string ReplaceHostWithLocalhost(string host)
|
||||
{
|
||||
return host.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user