svrnty-mcp-gateway/tests/Svrnty.MCP.Gateway.Infrastructure.Tests/Transport/HttpServerTransportTests.cs
Svrnty 19ef79362e refactor: rename OpenHarbor.MCP to Svrnty.MCP across all libraries
- Renamed all directories: OpenHarbor.MCP.* → Svrnty.MCP.*
- Updated all namespaces in 179 C# files
- Renamed 20 .csproj files and 3 .sln files
- Updated 193 documentation references
- Updated 33 references in main CODEX codebase
- Updated Codex.sln with new paths
- Build verified: 0 errors

Preparing for extraction to standalone repositories.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-22 21:04:17 -04:00

177 lines
5.6 KiB
C#

using Xunit;
using Moq;
using Moq.Protected;
using System.Net;
using Svrnty.MCP.Gateway.Infrastructure.Transport;
using Svrnty.MCP.Gateway.Core.Models;
namespace Svrnty.MCP.Gateway.Infrastructure.Tests.Transport;
/// <summary>
/// Unit tests for HttpServerTransport following TDD approach.
/// Tests HTTP transport implementation.
/// </summary>
public class HttpServerTransportTests
{
[Fact]
public void Constructor_WithValidUrl_CreatesSuccessfully()
{
// Arrange & Act
var transport = new HttpServerTransport("http://localhost:5000");
// Assert
Assert.NotNull(transport);
Assert.False(transport.IsConnected);
}
[Fact]
public void Constructor_WithNullUrl_ThrowsException()
{
// Arrange, Act & Assert
Assert.Throws<ArgumentNullException>(() =>
new HttpServerTransport(null!));
}
[Fact]
public async Task ConnectAsync_WithHealthyServer_SetsConnected()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var transport = new HttpServerTransport("http://localhost:5000", httpClient);
// Act
await transport.ConnectAsync();
// Assert
Assert.True(transport.IsConnected);
}
[Fact]
public async Task SendRequestAsync_WithSuccessfulResponse_ReturnsResponse()
{
// Arrange
var expectedResponse = new GatewayResponse
{
Success = true,
Result = new Dictionary<string, object> { { "data", "test" } }
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage // Health check
{
StatusCode = HttpStatusCode.OK
})
.ReturnsAsync(new HttpResponseMessage // Actual request
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(expectedResponse))
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var transport = new HttpServerTransport("http://localhost:5000", httpClient);
await transport.ConnectAsync();
var request = new GatewayRequest { ToolName = "test_tool" };
// Act
var response = await transport.SendRequestAsync(request);
// Assert
Assert.NotNull(response);
Assert.True(response.Success);
}
[Fact]
public async Task SendRequestAsync_WithHttpError_ReturnsErrorResponse()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.SetupSequence<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage // Health check
{
StatusCode = HttpStatusCode.OK
})
.ReturnsAsync(new HttpResponseMessage // Actual request
{
StatusCode = HttpStatusCode.InternalServerError,
ReasonPhrase = "Internal Server Error"
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var transport = new HttpServerTransport("http://localhost:5000", httpClient);
await transport.ConnectAsync();
var request = new GatewayRequest { ToolName = "test_tool" };
// Act
var response = await transport.SendRequestAsync(request);
// Assert
Assert.NotNull(response);
Assert.False(response.Success);
Assert.Contains("InternalServerError", response.Error);
}
[Fact]
public async Task SendRequestAsync_WithoutConnect_ThrowsException()
{
// Arrange
var transport = new HttpServerTransport("http://localhost:5000");
var request = new GatewayRequest { ToolName = "test" };
// Act & Assert
await Assert.ThrowsAsync<InvalidOperationException>(() =>
transport.SendRequestAsync(request));
}
[Fact]
public async Task DisconnectAsync_SetsNotConnected()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var transport = new HttpServerTransport("http://localhost:5000", httpClient);
await transport.ConnectAsync();
Assert.True(transport.IsConnected);
// Act
await transport.DisconnectAsync();
// Assert
Assert.False(transport.IsConnected);
}
}