Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ValidateAspNetCoreUrls #2466

Merged
merged 10 commits into from
Jan 7, 2025
46 changes: 46 additions & 0 deletions src/Service.Tests/Unittests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Azure.DataApiBuilder.Service.Tests.UnitTests;

/// <summary>
/// Contains test involving environment variables.
/// </summary>
[TestClass]
public class EnvironmentTests
{
/// <summary>
/// Tests the behavior of the <c>Main</c> method when the <c>ASPNETCORE_URLS</c> environment variable is set to an invalid value.
/// </summary>
/// <remarks>
/// This test sets the <c>ASPNETCORE_URLS</c> environment variable to an invalid value, invokes the <c>Main</c> method,
/// and verifies that the application exits with an error code of -1. Additionally, it checks if the error message
/// contains the name of the invalid environment variable.
/// </remarks>
[TestMethod]
public void Main_WhenAspNetCoreUrlsInvalid_ShouldExitWithError()
{
const string ASPNETCORE_URLS_NAME = "ASPNETCORE_URLS";
JerryNixon marked this conversation as resolved.
Show resolved Hide resolved
const string ASPNETCORE_URLS_INVALID_VALUE = nameof(Main_WhenAspNetCoreUrlsInvalid_ShouldExitWithError);
string originalEnvValue = Environment.GetEnvironmentVariable(ASPNETCORE_URLS_NAME);

// Arrange
Environment.SetEnvironmentVariable(ASPNETCORE_URLS_NAME, ASPNETCORE_URLS_INVALID_VALUE);
using StringWriter consoleOutput = new();
Console.SetError(consoleOutput);

// Act
Program.Main(Array.Empty<string>());

// Assert
Assert.AreEqual(-1, Environment.ExitCode);
aaronburtle marked this conversation as resolved.
Show resolved Hide resolved
StringAssert.Contains(consoleOutput.ToString(), ASPNETCORE_URLS_NAME, StringComparison.Ordinal);

// Cleanup
Environment.SetEnvironmentVariable(ASPNETCORE_URLS_NAME, originalEnvValue);
}
}
20 changes: 20 additions & 0 deletions src/Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Service.Exceptions;
Expand All @@ -26,6 +27,13 @@ public class Program

public static void Main(string[] args)
{
if (!ValidateAspNetCoreUrls())
{
Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\"");
Environment.ExitCode = -1;
aaronburtle marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (!StartEngine(args))
{
Environment.ExitCode = -1;
Expand Down Expand Up @@ -224,5 +232,17 @@ private static void AddConfigurationProviders(
.AddEnvironmentVariables(prefix: FileSystemRuntimeConfigLoader.ENVIRONMENT_PREFIX)
.AddCommandLine(args);
}

private static bool ValidateAspNetCoreUrls()
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_URLS") is not string value)
{
return true; // If the environment variable is missing, then it cannot be invalid.
}

return value
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.All(x => Uri.TryCreate(x.Trim(), UriKind.Absolute, out _));
aaronburtle marked this conversation as resolved.
Show resolved Hide resolved
}
}
}