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

Fault-injector: yet another port fix, also make bodyless requests work again #7792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<VersionPrefix>0.2.0</VersionPrefix>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ private async Task<UpstreamResponse> 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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,6 +30,7 @@ public static void Main(string[] args)
{
settings.CaseSensitive = false;
settings.HelpWriter = Console.Error;
settings.IgnoreUnknownArguments = true;
});

parser.ParseArguments<Options>(args).WithParsed(options => Run(options, args));
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,5 @@
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://+:7777"
},
"https": {
"Url": "https://+:7778"
}
}
}
"urls": "http://+:7777;https://+:7778"
}