diff --git a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Azure.Sdk.Tools.HttpFaultInjector.csproj b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Azure.Sdk.Tools.HttpFaultInjector.csproj index 4de145d3572..f1b68f0f6d1 100644 --- a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Azure.Sdk.Tools.HttpFaultInjector.csproj +++ b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Azure.Sdk.Tools.HttpFaultInjector.csproj @@ -1,4 +1,4 @@ - + 0.2.0 diff --git a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/FaultInjectingMiddleware.cs b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/FaultInjectingMiddleware.cs index a7843a8a841..bad2b77afbb 100644 --- a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/FaultInjectingMiddleware.cs +++ b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/FaultInjectingMiddleware.cs @@ -67,10 +67,13 @@ private async Task SendUpstreamRequest(HttpRequest request, st using (var upstreamRequest = new HttpRequestMessage(new HttpMethod(request.Method), upstreamUri)) { - upstreamRequest.Content = new StreamContent(request.Body); - foreach (var header in request.Headers.Where(h => Utils.ContentRequestHeaders.Contains(h.Key))) - { - upstreamRequest.Content.Headers.Add(header.Key, values: header.Value); + if (Utils.HasBody(request)) + { + upstreamRequest.Content = new StreamContent(request.Body); + foreach (var header in request.Headers.Where(h => Utils.ContentRequestHeaders.Contains(h.Key))) + { + upstreamRequest.Content.Headers.Add(header.Key, values: header.Value); + } } foreach (var header in request.Headers.Where(h => !Utils.ExcludedRequestHeaders.Contains(h.Key) && !Utils.ContentRequestHeaders.Contains(h.Key))) diff --git a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Program.cs b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Program.cs index 6d1672f5d65..af49dff5c89 100644 --- a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Program.cs +++ b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Program.cs @@ -7,7 +7,9 @@ using OpenTelemetry.Logs; using OpenTelemetry.Resources; using System; +using System.IO; using System.Net.Http; +using System.Reflection; namespace Azure.Sdk.Tools.HttpFaultInjector { @@ -28,6 +30,7 @@ public static void Main(string[] args) { settings.CaseSensitive = false; settings.HelpWriter = Console.Error; + settings.IgnoreUnknownArguments = true; }); parser.ParseArguments(args).WithParsed(options => Run(options, args)); @@ -36,7 +39,12 @@ public static void Main(string[] args) private static void Run(Options options, string[] args) { TimeSpan keepAlive = TimeSpan.FromSeconds(options.KeepAliveTimeout); - var builder = WebApplication.CreateBuilder(args); + var builder = WebApplication.CreateBuilder(new WebApplicationOptions() + { + Args = args, + ContentRootPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location)?.FullName + }); + builder.WebHost.ConfigureKestrel(kestrelOptions => { kestrelOptions.Limits.KeepAliveTimeout = keepAlive; diff --git a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Utils.cs b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Utils.cs index 4743c98038c..cacc0e705e7 100644 --- a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Utils.cs +++ b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/Utils.cs @@ -1,5 +1,9 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using Microsoft.AspNetCore.Http; +using OpenTelemetry.Trace; namespace Azure.Sdk.Tools.HttpFaultInjector { @@ -34,15 +38,29 @@ public static class Utils ResponseSelectionHeader }; - // Headers which must be set on HttpContent instead of HttpRequestMessage + // Headers which must be set on HttpContent instead of HttpRequestMessage, values from HttpContentHeaders public static readonly string[] ContentRequestHeaders = new string[] { + "Allow", + "Content-Disposition", + "Content-Encoding", + "Content-Language", "Content-Length", + "Content-Location", + "Content-MD5", + "Content-Range", "Content-Type", + "Expires", + "Last-Modified" }; public const string ResponseSelectionHeader = "x-ms-faultinjector-response-option"; public const string UpstreamBaseUriHeader = "X-Upstream-Base-Uri"; + public static bool HasBody(HttpRequest request) + { + return request.ContentLength > 0 || request.Headers.TransferEncoding.Contains("chunked"); + } + public static string ReadSelectionFromConsole() { string fault; diff --git a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/appsettings.json b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/appsettings.json index 1579d4c3b78..05c80958452 100644 --- a/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/appsettings.json +++ b/tools/http-fault-injector/Azure.Sdk.Tools.HttpFaultInjector/appsettings.json @@ -7,14 +7,5 @@ "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information" } }, - "Kestrel": { - "Endpoints": { - "http": { - "Url": "http://+:7777" - }, - "https": { - "Url": "https://+:7778" - } - } - } + "urls": "http://+:7777;https://+:7778" }