-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#941 Added SseDelegatingHandler #2300
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
Draft
vijay-karavadra
wants to merge
3
commits into
ThreeMammals:develop
Choose a base branch
from
vijay-karavadra:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Ocelot.Requester | ||
{ | ||
public class SseDelegatingHandler : DelegatingHandler | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public SseDelegatingHandler(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var httpContext = _httpContextAccessor.HttpContext; | ||
|
||
var isSse = request.Headers.Accept.Any(h => h.MediaType == "text/event-stream"); | ||
if (!isSse) | ||
{ | ||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
// Correct overload: only 2 parameters | ||
var response = await base.SendAsync(request, cancellationToken); | ||
|
||
httpContext.Response.StatusCode = (int)response.StatusCode; | ||
httpContext.Response.ContentType = "text/event-stream"; | ||
|
||
// Forward response headers | ||
foreach (var header in response.Headers) | ||
{ | ||
httpContext.Response.Headers[header.Key] = header.Value.ToArray(); | ||
} | ||
foreach (var header in response.Content.Headers) | ||
{ | ||
httpContext.Response.Headers[header.Key] = header.Value.ToArray(); | ||
} | ||
|
||
httpContext.Response.Headers.Remove("transfer-encoding"); | ||
|
||
// Stream content | ||
await using var downstreamStream = await response.Content.ReadAsStreamAsync(cancellationToken); | ||
using var reader = new StreamReader(downstreamStream, Encoding.UTF8); | ||
|
||
while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested) | ||
{ | ||
var line = await reader.ReadLineAsync(); | ||
if (line != null) | ||
{ | ||
var buffer = Encoding.UTF8.GetBytes(line + "\n"); | ||
await httpContext.Response.Body.WriteAsync(buffer, 0, buffer.Length, cancellationToken); | ||
await httpContext.Response.Body.FlushAsync(cancellationToken); | ||
} | ||
} | ||
|
||
// Dummy response to complete pipeline | ||
return new HttpResponseMessage(response.StatusCode) | ||
{ | ||
ReasonPhrase = "SSE stream has been forwarded" | ||
}; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/Ocelot.UnitTests/Requester/SseDelegatingHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Moq.Protected; | ||
using Ocelot.Requester; | ||
|
||
namespace Ocelot.UnitTests.Requester | ||
{ | ||
public class SseDelegatingHandlerTests | ||
{ | ||
[Fact] | ||
public async Task SendAsync_ForNonSseRequest_CallsBaseHandler() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a single unit test is just the first step in comprehensive testing. |
||
{ | ||
// Arrange | ||
var mockHttpContext = new DefaultHttpContext(); | ||
var mockAccessor = new Mock<IHttpContextAccessor>(); | ||
mockAccessor.Setup(a => a.HttpContext).Returns(mockHttpContext); | ||
|
||
var mockInnerHandler = new Mock<HttpMessageHandler>(); | ||
mockInnerHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>( | ||
"SendAsync", | ||
ItExpr.IsAny<HttpRequestMessage>(), | ||
ItExpr.IsAny<CancellationToken>()) | ||
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); | ||
|
||
var handler = new SseDelegatingHandler(mockAccessor.Object) | ||
{ | ||
InnerHandler = mockInnerHandler.Object | ||
}; | ||
|
||
var client = new HttpClient(handler); | ||
var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com"); | ||
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | ||
|
||
// Act | ||
var response = await client.SendAsync(request); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
mockInnerHandler.Protected().Verify("SendAsync", Times.Once(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will b 8000 e displayed to describe this comment to others. Learn more.
This handler has been designed, but it hasn't been integrated into Ocelot's pipeline yet.