Skip to content

Commit

Permalink
Add pipe test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jun 14, 2019
1 parent 992dacf commit 8ca6917
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3712,6 +3712,87 @@ await ExpectAsync(Http2FrameType.DATA,
Assert.Equal("Hello World", Encoding.UTF8.GetString(bodyFrame.Payload.Span));
}

[Fact]
public async Task CompleteAsync_AfterPipeWrite_WithTrailers_SendsBodyAndTrailersWithEndStream()
{
var startingTcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var appTcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var clientTcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var headers = new[]
{
new KeyValuePair<string, string>(HeaderNames.Method, "GET"),
new KeyValuePair<string, string>(HeaderNames.Path, "/"),
new KeyValuePair<string, string>(HeaderNames.Scheme, "http"),
};
await InitializeConnectionAsync(async context =>
{
try
{
context.Response.OnStarting(() => { startingTcs.SetResult(0); return Task.CompletedTask; });
var completionFeature = context.Features.Get<IHttpResponseCompletionFeature>();
Assert.NotNull(completionFeature);

var buffer = context.Response.BodyWriter.GetMemory();
var length = Encoding.UTF8.GetBytes("Hello World", buffer.Span);
context.Response.BodyWriter.Advance(length);

Assert.False(startingTcs.Task.IsCompletedSuccessfully); // OnStarting did not get called.
Assert.False(context.Response.Headers.IsReadOnly);

context.Response.AppendTrailer("CustomName", "Custom Value");

await completionFeature.CompleteAsync().DefaultTimeout();
Assert.True(startingTcs.Task.IsCompletedSuccessfully); // OnStarting got called.
Assert.True(context.Response.Headers.IsReadOnly);

Assert.True(context.Features.Get<IHttpResponseTrailersFeature>().Trailers.IsReadOnly);

// Make sure the client gets our results from CompleteAsync instead of from the request delegate exiting.
await clientTcs.Task.DefaultTimeout();
appTcs.SetResult(0);
}
catch (Exception ex)
{
appTcs.SetException(ex);
}
});

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

var headersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 37,
withFlags: (byte)(Http2HeadersFrameFlags.END_HEADERS),
withStreamId: 1);
var bodyFrame = await ExpectAsync(Http2FrameType.DATA,
withLength: 11,
withFlags: (byte)(Http2HeadersFrameFlags.NONE),
withStreamId: 1);
var trailersFrame = await ExpectAsync(Http2FrameType.HEADERS,
withLength: 25,
withFlags: (byte)(Http2HeadersFrameFlags.END_HEADERS | Http2HeadersFrameFlags.END_STREAM),
withStreamId: 1);

clientTcs.SetResult(0);
await appTcs.Task;

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

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

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

Assert.Equal("Hello World", Encoding.UTF8.GetString(bodyFrame.Payload.Span));

_decodedHeaders.Clear();

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

Assert.Single(_decodedHeaders);
Assert.Equal("Custom Value", _decodedHeaders["CustomName"]);
}

[Fact]
public async Task CompleteAsync_AfterBodyStarted_WithTrailers_SendsBodyAndTrailersWithEndStream()
{
Expand Down

0 comments on commit 8ca6917

Please sign in to comment.