Files
Heimdall/tests/Heimdall.Infrastructure.Tests/OnvifProbeServiceTests.cs
T

107 lines
4.4 KiB
C#
Raw Normal View History

using System.Net;
using Heimdall.Infrastructure.Services.Discovery;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class OnvifProbeServiceTests
{
private readonly ILogger<OnvifProbeService> _logger = Substitute.For<ILogger<OnvifProbeService>>();
private readonly IHttpClientFactory _httpClientFactory = Substitute.For<IHttpClientFactory>();
[Fact]
public void Constructor_CreatesService()
{
var service = new OnvifProbeService(_httpClientFactory, _logger);
service.ShouldNotBeNull();
}
[Fact]
public async Task ProbeDeviceService_with_credentials_should_send_ws_security_header()
{
string? capturedBody = null;
var handler = new StubHttpMessageHandler(request =>
{
capturedBody = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
const string soapResponse = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soap:Body>
<tds:GetDeviceInformationResponse>
<tds:Manufacturer>Hikvision</tds:Manufacturer>
<tds:Model>DS-2CD2T47</tds:Model>
</tds:GetDeviceInformationResponse>
</soap:Body>
</soap:Envelope>
""";
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(soapResponse, System.Text.Encoding.UTF8, "application/soap+xml")
};
});
_httpClientFactory.CreateClient("Discovery").Returns(new HttpClient(handler));
var service = new OnvifProbeService(_httpClientFactory, _logger);
var result = await service.ProbeDeviceServiceAsync(
"http://192.168.1.10/onvif/device_service", "admin", "secret", CancellationToken.None);
result.ShouldNotBeNull();
result.Name.ShouldBe("Hikvision");
result.Hardware.ShouldBe("DS-2CD2T47");
capturedBody.ShouldNotBeNull();
capturedBody.ShouldContain("<Security xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">");
capturedBody.ShouldContain("<Username>admin</Username>");
capturedBody.ShouldContain("PasswordDigest");
}
[Fact]
public async Task ProbeDeviceService_without_credentials_should_send_no_security_header()
{
string? capturedBody = null;
var handler = new StubHttpMessageHandler(request =>
{
capturedBody = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
const string soapResponse = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soap:Body>
<tds:GetDeviceInformationResponse>
<tds:Manufacturer>Axis</tds:Manufacturer>
<tds:Model>M3085</tds:Model>
</tds:GetDeviceInformationResponse>
</soap:Body>
</soap:Envelope>
""";
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(soapResponse, System.Text.Encoding.UTF8, "application/soap+xml")
};
});
_httpClientFactory.CreateClient("Discovery").Returns(new HttpClient(handler));
var service = new OnvifProbeService(_httpClientFactory, _logger);
var result = await service.ProbeDeviceServiceAsync(
"http://192.168.1.11/onvif/device_service", null, null, CancellationToken.None);
result.ShouldNotBeNull();
result.Name.ShouldBe("Axis");
capturedBody.ShouldNotBeNull();
capturedBody.ShouldNotContain("Security");
}
/// <summary>HttpMessageHandler stub for capturing request bodies.</summary>
private sealed class StubHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responder)
: HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken ct)
=> Task.FromResult(responder(request));
}
}