Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
HTTP/2 response headers test
  • Loading branch information
Tratcher committed Jun 30, 2021
commit 134458d1b28fa4234be2cd0581ec717656baacc1
2 changes: 1 addition & 1 deletion src/Servers/Kestrel/Core/src/KestrelServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class KestrelServerOptions

private Func<string, Encoding?> _requestHeaderEncodingSelector = DefaultHeaderEncodingSelector;

private Func<string, Encoding?> _responseHeaderEncodingSelector = _ => Encoding.UTF8; // DefaultHeaderEncodingSelector;
private Func<string, Encoding?> _responseHeaderEncodingSelector = DefaultHeaderEncodingSelector;

// The following two lists configure the endpoints that Kestrel should listen to. If both lists are empty, the "urls" config setting (e.g. UseUrls) is used.
internal List<ListenOptions> CodeBackedListenOptions { get; } = new List<ListenOptions>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,83 @@ await InitializeConnectionAsync(async context =>
Assert.Equal("0", _decodedHeaders[HeaderNames.ContentLength]);
}

[Fact]
public async Task ResponseHeaders_WithNonAscii_Throws()
{
await InitializeConnectionAsync(async context =>
{
Assert.Throws<InvalidOperationException>(() => context.Response.Headers.Append("Custom你好Name", "Custom Value"));
Assert.Throws<InvalidOperationException>(() => context.Response.Headers.Append("CustomName", "Custom 你好 Value"));
await context.Response.WriteAsync("Hello World");
});

await StartStreamAsync(1, _browserRequestHeaders, endStream: true);

var headersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 32,
withFlags: (byte)Http2HeadersFrameFlags.END_HEADERS,
withStreamId: 1);

await ExpectAsync(Http2FrameType.DATA,
withLength: 11,
withFlags: (byte)Http2DataFrameFlags.NONE,
withStreamId: 1);

await ExpectAsync(Http2FrameType.DATA,
withLength: 0,
withFlags: (byte)Http2DataFrameFlags.END_STREAM,
withStreamId: 1);

await StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);

_hpackDecoder.Decode(headersFrame.PayloadSequence, endHeaders: true, handler: this);

Assert.Equal(2, _decodedHeaders.Count);
Assert.Contains("date", _decodedHeaders.Keys, StringComparer.OrdinalIgnoreCase);
Assert.Equal("200", _decodedHeaders[HeaderNames.Status]);
}

[Fact]
public async Task ResponseHeaders_WithNonAsciiAndCustomEncoder_Works()
{
// TODO: How to correctly modify this per test?
_serviceContext.ServerOptions.ResponseHeaderEncodingSelector = _ => Encoding.UTF8;
_serviceContext.ServerOptions.RequestHeaderEncodingSelector = _ => Encoding.UTF8; // Used for decoding response.

await InitializeConnectionAsync(async context =>
{
Assert.Throws<InvalidOperationException>(() => context.Response.Headers.Append("Custom你好Name", "Custom Value"));
context.Response.Headers.Append("CustomName", "Custom 你好 Value");
await context.Response.WriteAsync("Hello World");
});

await StartStreamAsync(1, _browserRequestHeaders, endStream: true);

var headersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 32,
withFlags: (byte)Http2HeadersFrameFlags.END_HEADERS,
withStreamId: 1);

await ExpectAsync(Http2FrameType.DATA,
withLength: 11,
withFlags: (byte)Http2DataFrameFlags.NONE,
withStreamId: 1);

await ExpectAsync(Http2FrameType.DATA,
withLength: 0,
withFlags: (byte)Http2DataFrameFlags.END_STREAM,
withStreamId: 1);

await StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);

_hpackDecoder.Decode(headersFrame.PayloadSequence, endHeaders: true, handler: this);

Assert.Equal(3, _decodedHeaders.Count);
Assert.Contains("date", _decodedHeaders.Keys, StringComparer.OrdinalIgnoreCase);
Assert.Equal("200", _decodedHeaders[HeaderNames.Status]);
Assert.Equal("Custom 你好 Value", _decodedHeaders["CustomName"]);
}

[Fact]
public async Task ResponseTrailers_WithoutData_Sent()
{
Expand Down Expand Up @@ -2218,6 +2295,7 @@ public async Task ResponseTrailers_WithNonAsciiAndCustomEncoder_Works()
{
// TODO: How to correctly modify this per test?
_serviceContext.ServerOptions.ResponseHeaderEncodingSelector = _ => Encoding.UTF8;
_serviceContext.ServerOptions.RequestHeaderEncodingSelector = _ => Encoding.UTF8; // Used for decoding response.

await InitializeConnectionAsync(async context =>
{
Expand Down