Skip to content

Commit

Permalink
test now work
Browse files Browse the repository at this point in the history
  • Loading branch information
Erwinvandervalk committed Jan 10, 2025
1 parent 1ff8bc1 commit 92f98ce
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 25 deletions.
2 changes: 2 additions & 0 deletions bff/samples/Hosts.Tests/BffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ public async Task Can_logout()
await _bffClient.InvokeApi("/local/self-contained", expectedResponse: HttpStatusCode.Unauthorized);
}


}

}
61 changes: 36 additions & 25 deletions bff/samples/Hosts.Tests/TestInfra/AppHostFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Sockets;
using Aspire.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -53,7 +54,8 @@ public async Task InitializeAsync()
{
return new SocketsHttpHandler()
{
CookieContainer = new CookieContainer()
UseCookies = false,
AllowAutoRedirect = false
};
}));

Expand Down Expand Up @@ -134,47 +136,56 @@ public async Task DisposeAsync()

public HttpClient CreateHttpClient(string clientName)
{
HttpMessageHandler inner;
Uri? baseAddress = null;

if (UsingAlreadyRunningInstance)
{
var url = clientName switch
{
"bff" => "https://localhost:5002",
_ => throw new InvalidOperationException("client not configured")
};
baseAddress = new Uri(url);

var socketHandler = new SocketsHttpHandler()
inner = new SocketsHttpHandler()
{
UseCookies = false,
AllowAutoRedirect = false,
};

}
else
{
#if DEBUG_NCRUNCH
throw new InvalidOperationException("This should not be reached in NCrunch");
#else
if (App == null) throw new NotSupportedException("App should not be null");
var client = App.CreateHttpClient(clientName);
baseAddress = client.BaseAddress;
inner = new CloningHttpMessageHandler(client);
}

var loggingHandler =
new OutboundRequestLoggingHandler(
CreateLogger<OutboundRequestLoggingHandler>()
, _ => true)
{
InnerHandler = socketHandler
};
var cookieHandler = new CookieHandler(loggingHandler, new CookieContainer());

var redirectHandler = new AutoFollowRedirectHandler(CreateLogger<AutoFollowRedirectHandler>())
var loggingHandler =
new OutboundRequestLoggingHandler(
CreateLogger<OutboundRequestLoggingHandler>()
, _ => true)
{
InnerHandler = cookieHandler
InnerHandler = inner
};
var cookieHandler = new CookieHandler(loggingHandler, new CookieContainer());

var redirectHandler = new AutoFollowRedirectHandler(CreateLogger<AutoFollowRedirectHandler>())
{
InnerHandler = cookieHandler
};

return new HttpClient(redirectHandler)
{
BaseAddress = new Uri(url)
};
}

#if DEBUG_NCRUNCH
throw new InvalidOperationException("This should not be reached in NCrunch");
#else
if (App == null) throw new NotSupportedException("App should not be null");
var client = App.CreateHttpClient(clientName);
return client;
return new HttpClient(redirectHandler)
{
BaseAddress = baseAddress
};

#endif
}

Expand Down
88 changes: 88 additions & 0 deletions bff/samples/Hosts.Tests/TestInfra/AutoFollowRedirectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,92 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

throw new InvalidOperationException("Keeps redirecting forever");
}
}

public class CloningHttpMessageHandler : HttpMessageHandler
{
private readonly HttpClient _innerHttpClient;

public CloningHttpMessageHandler(HttpClient innerHttpClient)
{
_innerHttpClient = innerHttpClient ?? throw new ArgumentNullException(nameof(innerHttpClient));
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Clone the incoming request
var clonedRequest = await CloneHttpRequestMessageAsync(request);

// Send the cloned request using the inner HttpClient
var response = await _innerHttpClient.SendAsync(clonedRequest, cancellationToken);

// Clone the response and return it
return await CloneHttpResponseMessageAsync(response);
}

private async Task<HttpRequestMessage> CloneHttpRequestMessageAsync(HttpRequestMessage original)
{
var cloned = new HttpRequestMessage(original.Method, original.RequestUri)
{
Version = original.Version
};

// Copy the content if present
if (original.Content != null)
{
//var memoryStream = new MemoryStream();
//await original.Content.CopyToAsync(memoryStream);
//memoryStream.Position = 0;
//cloned.Content = new StreamContent(memoryStream);
cloned.Content = new StreamContent(await original.Content.ReadAsStreamAsync());

// Copy headers from the original content to the cloned content
foreach (var header in original.Content.Headers)
{
cloned.Content.Headers.Add(header.Key, header.Value);
}
}

// Copy headers
foreach (var header in original.Headers)
{
cloned.Headers.Add(header.Key, header.Value);
}

return cloned;
}

private async Task<HttpResponseMessage> CloneHttpResponseMessageAsync(HttpResponseMessage original)
{
var cloned = new HttpResponseMessage(original.StatusCode)
{
Version = original.Version,
ReasonPhrase = original.ReasonPhrase,
RequestMessage = original.RequestMessage,
};

// Copy the content if present
if (original.Content != null)
{
//var memoryStream = new MemoryStream();
//await original.Content.CopyToAsync(memoryStream);
//memoryStream.Position = 0;
//cloned.Content = new StreamContent(memoryStream);
cloned.Content = new StreamContent(await original.Content.ReadAsStreamAsync());

// Copy headers from the original content to the cloned content
foreach (var header in original.Content.Headers)
{
cloned.Content.Headers.Add(header.Key, header.Value);
}
}

// Copy headers
foreach (var header in original.Headers)
{
cloned.Headers.Add(header.Key, header.Value);
}

return cloned;
}
}

0 comments on commit 92f98ce

Please sign in to comment.