|
| 1 | +using System.Net; |
| 2 | +using System.Reflection; |
| 3 | +using System.Text.Json; |
| 4 | +using EssentialCSharp.Web.Services; |
| 5 | + |
| 6 | +namespace EssentialCSharp.Web.Tests; |
| 7 | + |
| 8 | +public class VersionEndpointTests : IntegrationTestBase |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public async Task GetVersion_ReturnsRunningAssemblyBuildInfo_WithoutCaching() |
| 12 | + { |
| 13 | + using HttpClient client = CreateClientWithoutAutoRedirect(); |
| 14 | + |
| 15 | + using HttpResponseMessage response = await client.GetAsync("/version"); |
| 16 | + |
| 17 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 18 | + await Assert.That(response.Headers.CacheControl?.NoStore).IsTrue(); |
| 19 | + await Assert.That(response).HasJsonContent(); |
| 20 | + |
| 21 | + string body = await response.Content.ReadAsStringAsync(); |
| 22 | + await Assert.That(body).IsValidJsonObject(); |
| 23 | + |
| 24 | + // Deep equality pins the whole contract that deployment checks rely on: no renamed or extra fields. |
| 25 | + string? expectedVersion = typeof(Program).Assembly |
| 26 | + .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; |
| 27 | + using JsonDocument expected = JsonDocument.Parse(JsonSerializer.Serialize(new |
| 28 | + { |
| 29 | + version = expectedVersion, |
| 30 | + commit = BuildInfo.FromInformationalVersion(expectedVersion).Commit, |
| 31 | + })); |
| 32 | + using JsonDocument actual = JsonDocument.Parse(body); |
| 33 | + await Assert.That(actual.RootElement).IsDeepEqualTo(expected.RootElement); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public async Task HeadVersion_ReturnsOk() |
| 38 | + { |
| 39 | + using HttpClient client = CreateClientWithoutAutoRedirect(); |
| 40 | + using var request = new HttpRequestMessage(HttpMethod.Head, "/version"); |
| 41 | + |
| 42 | + using HttpResponseMessage response = await client.SendAsync(request); |
| 43 | + |
| 44 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public async Task BuildInfo_InGitHubActions_IsStampedWithCheckedOutCommit() |
| 49 | + { |
| 50 | + // Locally the stamp depends on the developer's checkout, so only enforce it in CI, where the |
| 51 | + // checked-out commit is GITHUB_SHA and the SDK's SourceLink stamps it into the informational version. |
| 52 | + if (!string.Equals(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), "true", StringComparison.OrdinalIgnoreCase)) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + await Assert.That(BuildInfo.Current.Commit).IsEqualTo(Environment.GetEnvironmentVariable("GITHUB_SHA")); |
| 58 | + } |
| 59 | + |
| 60 | + [Test] |
| 61 | + public async Task GetVersion_IsNotRateLimited() |
| 62 | + { |
| 63 | + // Deployment checks poll /version until the new revision answers; the global limiter must not reject them. |
| 64 | + using HttpClient client = CreateClientWithoutAutoRedirect(); |
| 65 | + |
| 66 | + for (int i = 0; i < 40; i++) |
| 67 | + { |
| 68 | + using HttpResponseMessage response = await client.GetAsync("/version"); |
| 69 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + [Arguments("1.0.0+0123456789abcdef0123456789abcdef01234567", "1.0.0+0123456789abcdef0123456789abcdef01234567", "0123456789abcdef0123456789abcdef01234567")] |
| 75 | + [Arguments("1.0.0", "1.0.0", null)] |
| 76 | + [Arguments("1.0.0+", "1.0.0+", null)] |
| 77 | + [Arguments(null, null, null)] |
| 78 | + [Arguments("", null, null)] |
| 79 | + public async Task FromInformationalVersion_ExtractsCommitAfterPlus(string? informationalVersion, string? expectedVersion, string? expectedCommit) |
| 80 | + { |
| 81 | + BuildInfo info = BuildInfo.FromInformationalVersion(informationalVersion); |
| 82 | + |
| 83 | + await Assert.That(info.Version).IsEqualTo(expectedVersion); |
| 84 | + await Assert.That(info.Commit).IsEqualTo(expectedCommit); |
| 85 | + } |
| 86 | +} |
0 commit comments