private static string FormatInt64(long input) { #if NETSTANDARD2_0 return(HeaderUtilities.FormatNonNegativeInt64(input)); #else return(HeaderUtilities.FormatInt64(input)); #endif }
public async Task DecompressionDeflateTest() { // Arrange using (var server = new TestServer(this.CreateDecompressionBuilder(new CompressionOptions { MinimumCompressionThreshold = 0 }))) { // Act byte[] compressedBytes; using (var dataStream = new MemoryStream()) { using (var zipStream = new DeflateStream(dataStream, CompressionMode.Compress)) { using (var writer = new StreamWriter(zipStream)) { writer.Write(Helpers.ResponseText); } } compressedBytes = dataStream.ToArray(); } HttpResponseMessage response; string responseText; using (HttpClient client = server.CreateClient()) { client.DefaultRequestHeaders.Add(HeaderNames.AcceptEncoding, "gzip"); var content = new ByteArrayContent(compressedBytes); content.Headers.Add(HeaderNames.ContentEncoding, "deflate"); content.Headers.Add(HeaderNames.ContentLength, HeaderUtilities.FormatInt64(compressedBytes.Length)); response = await client.PutAsync("/", content); // Assert response.EnsureSuccessStatusCode(); Stream stream = await response.Content.ReadAsStreamAsync(); using (var decompression = new GZipStream(stream, CompressionMode.Decompress)) { using (var ms = new MemoryStream()) { await decompression.CopyToAsync(ms); responseText = Encoding.UTF8.GetString(ms.ToArray()); } } } Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK"); Assert.AreEqual(Helpers.ResponseText, responseText, "Response Text not equal"); Assert.AreEqual(85, response.Content.Headers.ContentLength, "Content-Length != 85"); Assert.AreEqual(true, response.Content.Headers.ContentEncoding.Any(), "Content-Encoding == null"); Assert.AreEqual("gzip", response.Content.Headers.ContentEncoding.ToString(), "Content-Encoding != gzip"); } }
/// <summary> /// Set the content-length header value /// </summary> /// <param name="headers"></param> /// <param name="value"></param> public static void ContentLength(this IHeaderDictionary headers, long?value) { if (value.HasValue) { headers[HeaderNames.ContentLength] = HeaderUtilities.FormatInt64(value.Value); } else { headers.Remove(HeaderNames.ContentLength); } }
public async Task DecompressionGZipNoCompressionTest() { // Arrange using (var server = new TestServer(this.CreateDecompressionBuilder())) { // Act byte[] compressedBytes; using (var dataStream = new MemoryStream()) { using (var zipStream = new GZipStream(dataStream, CompressionMode.Compress)) { using (var writer = new StreamWriter(zipStream)) { writer.Write(Helpers.ResponseText); } } compressedBytes = dataStream.ToArray(); } HttpResponseMessage response; string responseText; using (HttpClient client = server.CreateClient()) { client.DefaultRequestHeaders.Add(HeaderNames.AcceptEncoding, "gzip"); var content = new ByteArrayContent(compressedBytes); content.Headers.Add(HeaderNames.ContentEncoding, "gzip"); content.Headers.Add(HeaderNames.ContentLength, HeaderUtilities.FormatInt64(compressedBytes.Length)); response = await client.PutAsync("/", content); // Assert response.EnsureSuccessStatusCode(); responseText = await response.Content.ReadAsStringAsync(); } Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK"); Assert.AreEqual(Helpers.ResponseText, responseText, "Response Text not equal"); Assert.AreEqual(124, response.Content.Headers.ContentLength, "Content-Length != 124"); Assert.AreEqual(false, response.Content.Headers.ContentEncoding.Any(), "Content-Encoding != null"); } }
/// <summary> /// Append string representation of this <see cref="SetCookieHeaderValue"/> to given /// <paramref name="builder"/>. /// </summary> /// <param name="builder"> /// The <see cref="StringBuilder"/> to receive the string representation of this /// <see cref="SetCookieHeaderValue"/>. /// </param> public void AppendToStringBuilder(StringBuilder builder) { builder.Append(_name); builder.Append("="); builder.Append(_value); if (Expires.HasValue) { AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value)); } if (MaxAge.HasValue) { AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatInt64((long)MaxAge.Value.TotalSeconds)); } if (Domain != null) { AppendSegment(builder, DomainToken, Domain); } if (Path != null) { AppendSegment(builder, PathToken, Path); } if (Secure) { AppendSegment(builder, SecureToken, null); } if (HttpOnly) { AppendSegment(builder, HttpOnlyToken, null); } }
public async Task NoDecompressionGZipTest() { // Arrange IWebHostBuilder builder = new WebHostBuilder() .ConfigureServices(s => s.AddCompression()) .Configure( app => { app.UseCompression(); app.Run( async c => { using (var ms = new MemoryStream()) { await c.Request.Body.CopyToAsync(ms); c.Response.ContentType = "text/plain"; c.Response.ContentLength = ms.Length; ms.Seek(0, SeekOrigin.Begin); await ms.CopyToAsync(c.Response.Body); } }); }); using (var server = new TestServer(builder)) { // Act byte[] compressedBytes; using (var dataStream = new MemoryStream()) { using (var zipStream = new GZipStream(dataStream, CompressionMode.Compress)) { using (var writer = new StreamWriter(zipStream)) { writer.Write(Helpers.ResponseText); } } compressedBytes = dataStream.ToArray(); } HttpResponseMessage response; byte[] responseBytes; using (HttpClient client = server.CreateClient()) { client.DefaultRequestHeaders.Add(HeaderNames.AcceptEncoding, "gzip"); var content = new ByteArrayContent(compressedBytes); content.Headers.Add(HeaderNames.ContentLength, HeaderUtilities.FormatInt64(compressedBytes.Length)); response = await client.PutAsync("/", content); // Assert response.EnsureSuccessStatusCode(); responseBytes = await response.Content.ReadAsByteArrayAsync(); } Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK"); Assert.AreEqual(true, compressedBytes.SequenceEqual(responseBytes), "Response bytes not equal"); Assert.AreEqual(compressedBytes.Length, response.Content.Headers.ContentLength, $"Content-Length != {compressedBytes.Length}"); Assert.AreEqual(true, !response.Content.Headers.ContentEncoding.Any(), "Content-Encoding != null"); } }