Skip to content

Commit

Permalink
dd GetGZipHeader method to provide OS-specific Gzip headers based on …
Browse files Browse the repository at this point in the history
…the runtime platform.
  • Loading branch information
ikpil committed Sep 21, 2024
1 parent b041232 commit b0bf735
Showing 1 changed file with 58 additions and 21 deletions.
79 changes: 58 additions & 21 deletions test/UniNetty.Codecs.Http.Tests/HttpContentCompressorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Copyright (c) Ikpil Choi [email protected] All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.


namespace UniNetty.Codecs.Http.Tests
{
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UniNetty.Buffers;
using UniNetty.Codecs.Compression;
using UniNetty.Common.Utilities;
Expand All @@ -14,6 +17,30 @@ namespace UniNetty.Codecs.Http.Tests

public sealed class HttpContentCompressorTest
{
// GZIPHeader
public static IEnumerable<object[]> GetGZipHeader()
{
string gzipHeaderHex = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
gzipHeaderHex = "1f8b080000000000000b";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
gzipHeaderHex = "1f8b0800000000000003";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
gzipHeaderHex = "1f8b0800000000000007";
}
else
{
gzipHeaderHex = "1f8b08000000000000ff";
}

yield return new object[] { gzipHeaderHex }; // UNIX Gzip 헤더
}

[Fact]
public void GetTargetContentEncoding()
{
Expand Down Expand Up @@ -53,12 +80,14 @@ public void GetTargetContentEncoding()
break;
}
}

Assert.Equal(contentEncoding, targetEncoding);
}
}

[Fact]
public void SplitContent()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void SplitContent(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());
Expand All @@ -71,7 +100,7 @@ public void SplitContent()
AssertEncodedResponse(ch);

var chunk = ch.ReadOutbound<IHttpContent>();
Assert.Equal("1f8b080000000000000bf248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
Assert.Equal(gzipHeaderHex + "f248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
chunk.Release();

chunk = ch.ReadOutbound<IHttpContent>();
Expand All @@ -95,8 +124,9 @@ public void SplitContent()
Assert.Null(last);
}

[Fact]
public void ChunkedContent()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void ChunkedContent(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());
Expand All @@ -112,7 +142,7 @@ public void ChunkedContent()
ch.WriteOutbound(new DefaultLastHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("orld"))));

var chunk = ch.ReadOutbound<IHttpContent>();
Assert.Equal("1f8b080000000000000bf248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
Assert.Equal(gzipHeaderHex + "f248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
chunk.Release();

chunk = ch.ReadOutbound<IHttpContent>();
Expand All @@ -136,8 +166,9 @@ public void ChunkedContent()
Assert.Null(last);
}

[Fact]
public void ChunkedContentWithTrailingHeader()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void ChunkedContentWithTrailingHeader(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());
Expand All @@ -155,7 +186,7 @@ public void ChunkedContentWithTrailingHeader()
ch.WriteOutbound(content);

var chunk = ch.ReadOutbound<IHttpContent>();
Assert.Equal("1f8b080000000000000bf248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
Assert.Equal(gzipHeaderHex + "f248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
chunk.Release();

chunk = ch.ReadOutbound<IHttpContent>();
Expand All @@ -179,8 +210,9 @@ public void ChunkedContentWithTrailingHeader()
Assert.Null(last);
}

[Fact]
public void FullContentWithContentLength()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void FullContentWithContentLength(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());
Expand All @@ -204,7 +236,7 @@ public void FullContentWithContentLength()

var c = ch.ReadOutbound<IHttpContent>();
observedLength += c.Content.ReadableBytes;
Assert.Equal("1f8b080000000000000bf248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(c.Content));
Assert.Equal(gzipHeaderHex + "f248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(c.Content));
c.Release();

c = ch.ReadOutbound<IHttpContent>();
Expand All @@ -221,20 +253,21 @@ public void FullContentWithContentLength()
Assert.Equal(contentLengthHeaderValue, observedLength);
}

[Fact]
public void FullContent()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void FullContent(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());

var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK,
var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK,
Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("Hello, World")));
ch.WriteOutbound(res);

AssertEncodedResponse(ch);

var chunk = ch.ReadOutbound<IHttpContent>();
Assert.Equal("1f8b080000000000000bf248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
Assert.Equal(gzipHeaderHex + "f248cdc9c9d75108cf2fca4901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
chunk.Release();

chunk = ch.ReadOutbound<IHttpContent>();
Expand All @@ -250,8 +283,9 @@ public void FullContent()
Assert.Null(last);
}

[Fact]
public void EmptySplitContent()
[Theory]
[MemberData(nameof(GetGZipHeader))]
public void EmptySplitContent(string gzipHeaderHex)
{
var ch = new EmbeddedChannel(new HttpContentCompressor());
ch.WriteInbound(NewRequest());
Expand All @@ -261,7 +295,7 @@ public void EmptySplitContent()

ch.WriteOutbound(EmptyLastHttpContent.Default);
var chunk = ch.ReadOutbound<IHttpContent>();
Assert.Equal("1f8b080000000000000b03000000000000000000", ByteBufferUtil.HexDump(chunk.Content));
Assert.Equal(gzipHeaderHex + "03000000000000000000", ByteBufferUtil.HexDump(chunk.Content));
chunk.Release();

chunk = ch.ReadOutbound<IHttpContent>();
Expand Down Expand Up @@ -384,15 +418,18 @@ public void TooManyResponses()
{
break;
}

ReferenceCountUtil.Release(message);
}

for (;;)
{
var message = ch.ReadInbound<object>();
if (message == null)
{
break;
}

ReferenceCountUtil.Release(message);
}
}
Expand All @@ -403,7 +440,7 @@ public void Identity()
var ch = new EmbeddedChannel(new HttpContentCompressor());
Assert.True(ch.WriteInbound(NewRequest()));

var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK,
var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK,
Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("Hello, World")));
int len = res.Content.ReadableBytes;
res.Headers.Set(HttpHeaderNames.ContentLength, len);
Expand Down Expand Up @@ -439,4 +476,4 @@ static void AssertEncodedResponse(EmbeddedChannel ch)
Assert.Equal("gzip", res.Headers.Get(HttpHeaderNames.ContentEncoding, null).ToString());
}
}
}
}

0 comments on commit b0bf735

Please sign in to comment.