Files
wehub-resource-sync db620d33df
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:25 +08:00

59 lines
2.7 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.AgentServer.Responses;
namespace Microsoft.Agents.AI.Foundry.Hosting.UnitTests;
/// <summary>
/// Unit tests for <see cref="HostedProtocolCompatibility"/>, the protocol-version gate that turns an
/// opaque 500 into a clear 501 when this 2.0.0-only image is served container protocol 1.0.0.
/// </summary>
public sealed class HostedProtocolCompatibilityTests
{
[Fact]
public void GetUnsupportedProtocolError_HostedWithoutCallId_ReturnsUnsupportedProtocolError()
{
// Hosted by Foundry (FoundryEnvironment.IsHosted == true) but no x-agent-foundry-call-id header
// means the platform is talking protocol 1.0.0 to a 2.0.0-only image.
ResponsesApiException? error = HostedProtocolCompatibility.GetUnsupportedProtocolError(isHosted: true, callId: null);
Assert.NotNull(error);
Assert.Equal(HostedProtocolCompatibility.UnsupportedProtocolStatusCode, error!.StatusCode);
Assert.Equal(501, error.StatusCode);
Assert.Equal(HostedProtocolCompatibility.UnsupportedProtocolErrorCode, error.Error.Code);
Assert.Equal("Unsupported responses protocol version. This agent requires responses protocol v2.0.0", error.Error.Message);
}
[Fact]
public void GetUnsupportedProtocolError_HostedWithEmptyCallId_ReturnsUnsupportedProtocolError()
{
// An empty or whitespace-only header value is treated the same as an absent one, so a proxy that
// injects whitespace cannot bypass the gate.
foreach (var callId in new[] { "", " ", "\t" })
{
ResponsesApiException? error = HostedProtocolCompatibility.GetUnsupportedProtocolError(isHosted: true, callId: callId);
Assert.NotNull(error);
Assert.Equal(501, error!.StatusCode);
Assert.Equal(HostedProtocolCompatibility.UnsupportedProtocolErrorCode, error.Error.Code);
}
}
[Fact]
public void GetUnsupportedProtocolError_HostedWithCallId_ReturnsNull()
{
// Protocol 2.0.0: the platform supplies x-agent-foundry-call-id, so the request is compatible.
ResponsesApiException? error = HostedProtocolCompatibility.GetUnsupportedProtocolError(isHosted: true, callId: "fcid_abc123");
Assert.Null(error);
}
[Fact]
public void GetUnsupportedProtocolError_NotHosted_ReturnsNull()
{
// Local development (not hosted by Foundry) is never flagged, regardless of the call id.
Assert.Null(HostedProtocolCompatibility.GetUnsupportedProtocolError(isHosted: false, callId: null));
Assert.Null(HostedProtocolCompatibility.GetUnsupportedProtocolError(isHosted: false, callId: "fcid_abc123"));
}
}