Skip to content

Commit eb22a0b

Browse files
committed
Adding Accept header optionality configuration (#52)
1 parent be8c8f5 commit eb22a0b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Lib.AspNetCore.ServerSentEvents/ServerSentEventsMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ private bool CheckAcceptHeader(IHeaderDictionary requestHeaders)
118118
{
119119
if (!requestHeaders.ContainsKey(Constants.ACCEPT_HTTP_HEADER))
120120
{
121-
return true;
121+
return !_serverSentEventsOptions.RequireAcceptHeader;
122122
}
123123

124124
if (requestHeaders[Constants.ACCEPT_HTTP_HEADER].Count == 0)
125125
{
126-
return true;
126+
return !_serverSentEventsOptions.RequireAcceptHeader;
127127
}
128128

129129
if (requestHeaders[Constants.ACCEPT_HTTP_HEADER].Any(acceptHeaderValue => acceptHeaderValue == Constants.SSE_CONTENT_TYPE))

Lib.AspNetCore.ServerSentEvents/ServerSentEventsOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ public ServerSentEventsOptions()
2626
/// This can be used to add or change the response headers.
2727
/// </summary>
2828
public Action<HttpResponse> OnPrepareAccept { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the value indicating if Accept HTTP header with value of text/event-stream should be required.
32+
/// </summary>
33+
public bool RequireAcceptHeader { get; set; } = false;
2934
}
3035
}

Test.AspNetCore.ServerSentEvents/Unit/Middleware/HandshakeTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class HandshakeTests
1616

1717
#region Tests
1818
[Fact]
19-
public async Task Invoke_SseRequestWithoutAcceptHeader_Accepts()
19+
public async Task Invoke_SseRequestWithoutAcceptHeaderAndAcceptHeaderNotRequired_Accepts()
2020
{
2121
ServerSentEventsMiddleware<ServerSentEventsService> serverSentEventsMiddleware = SubjectUnderTestHelper.PrepareServerSentEventsMiddleware();
2222
HttpContext context = SubjectUnderTestHelper.PrepareHttpContext(acceptHeaderValue: null);
@@ -26,6 +26,17 @@ public async Task Invoke_SseRequestWithoutAcceptHeader_Accepts()
2626
Assert.Equal(SSE_CONTENT_TYPE, context.Response.ContentType);
2727
}
2828

29+
[Fact]
30+
public async Task Invoke_SseRequestWithoutAcceptHeaderAndAcceptHeaderRequired_DoesNotAccept()
31+
{
32+
ServerSentEventsMiddleware<ServerSentEventsService> serverSentEventsMiddleware = SubjectUnderTestHelper.PrepareServerSentEventsMiddleware(options: new ServerSentEventsOptions { RequireAcceptHeader = true });
33+
HttpContext context = SubjectUnderTestHelper.PrepareHttpContext(acceptHeaderValue: null);
34+
35+
await serverSentEventsMiddleware.Invoke(context, null);
36+
37+
Assert.Null(context.Response.ContentType);
38+
}
39+
2940
[Fact]
3041
public async Task Invoke_SseRequestWithEventStreamAcceptHeader_Accepts()
3142
{

0 commit comments

Comments
 (0)