Skip to content

Commit

Permalink
Added missing method for the test
Browse files Browse the repository at this point in the history
  • Loading branch information
ManickaP committed Mar 15, 2024
1 parent 7912bde commit 3f625b9
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public async Task Http2_DataFrameOnlyPadding_Success()

// The read must pend because we havent received any data yet.
var buffer = new byte[dataLength];
var readTask = responseStream.ReadAtLeastAsync(buffer, dataLength);
var readTask = ReadAtLeastAsync(responseStream, buffer, dataLength);
Assert.False(readTask.IsCompleted);

// Send DATA frame with padding
Expand All @@ -221,6 +221,38 @@ public async Task Http2_DataFrameOnlyPadding_Success()
}
}

private static async ValueTask<int> ReadAtLeastAsync(Stream stream, Memory<byte> buffer, int minimumBytes, bool throwOnEndOfStream = true, CancellationToken cancellationToken = default)
{
if (minimumBytes < 0)
{
throw new ArgumentOutOfRangeException(nameof(minimumBytes));
}
if (buffer.Length < minimumBytes)
{
throw new ArgumentOutOfRangeException($"{nameof(buffer)}.{nameof(buffer.Length)}");
}

int totalRead = 0;
while (totalRead < minimumBytes)
{
int read = await stream.ReadAsync(buffer.Slice(totalRead), cancellationToken).ConfigureAwait(false);
if (read == 0)
{
if (throwOnEndOfStream)
{
throw new EndOfStreamException();
}

return totalRead;
}

totalRead += read;
}

return totalRead;
}


[Theory]
[InlineData("Client content", null)]
[InlineData("Client content", "Server content")]
Expand Down Expand Up @@ -248,7 +280,7 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async uri =>
Http2LoopbackConnection connection = await server.EstablishConnectionAsync();
Assert.IsNotType<SslStream>(connection.Stream);

HttpRequestData requestData = await connection.ReadRequestDataAsync();
HttpRequestData requestData = await connection.ReadRequestDataAsync();
string requestContent = requestData.Body is null ? (string)null : Encoding.ASCII.GetString(requestData.Body);
Assert.Equal(clientContent, requestContent);
await connection.SendResponseAsync(HttpStatusCode.OK, content: serverContent);
Expand Down

0 comments on commit 3f625b9

Please sign in to comment.