Skip to content

Commit

Permalink
test: Add test for HttpUtils.IsHttpHeader()
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 13, 2021
1 parent 8f59eab commit 634ad8d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 2 additions & 0 deletions UnitTest/BufferSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace UnitTest
{
internal class BufferSegment : ReadOnlySequenceSegment<byte>
{
public static BufferSegment Empty => new(Memory<byte>.Empty);

public BufferSegment(Memory<byte> memory)
{
Memory = memory;
Expand Down
38 changes: 34 additions & 4 deletions UnitTest/HttpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Socks5.Models;
using Socks5.Servers;
using Socks5.Utils;
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace UnitTest
Expand Down Expand Up @@ -48,18 +48,15 @@ public async Task TestAsync()
var httpClient = new HttpClient(handler);

var httpsStr = await httpClient.GetStringAsync(@"https://api.ip.sb/ip");
Console.WriteLine(httpsStr);
Assert.IsFalse(string.IsNullOrWhiteSpace(httpsStr));

var httpChunkStr = await httpClient.GetStringAsync(@"http://api.ip.sb/ip");
Console.WriteLine(httpChunkStr);
Assert.IsFalse(string.IsNullOrWhiteSpace(httpChunkStr));

Assert.AreEqual(httpsStr, httpChunkStr);

httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(@"curl/7.55.1");
var httpStr = await httpClient.GetStringAsync(@"http://ip.sb");
Console.WriteLine(httpStr);
Assert.IsFalse(string.IsNullOrWhiteSpace(httpStr));

Assert.AreEqual(httpChunkStr, httpStr);
Expand All @@ -82,5 +79,38 @@ public async Task TestAsync()
server.Stop();
}
}

[TestMethod]
public void IsHttpHeaderTest()
{
var sequence0 = TestUtils.GetMultiSegmentSequence(
Encoding.ASCII.GetBytes("GET / HTTP/1.1"),
Encoding.ASCII.GetBytes("\r\nHost: ip.sb\r\n"),
Encoding.ASCII.GetBytes("User-Agent: curl/7.55.1\r\n"),
Encoding.ASCII.GetBytes("\r\n")
);
Assert.IsTrue(HttpUtils.IsHttpHeader(sequence0));

var sequence1 = TestUtils.GetMultiSegmentSequence(
Encoding.ASCII.GetBytes("GET / HTTP/1.1"),
Encoding.ASCII.GetBytes("\r\nHost: ip.sb\r\n"),
Encoding.ASCII.GetBytes("User-Agent: curl/7.55.1\r\n")
);
Assert.IsFalse(HttpUtils.IsHttpHeader(sequence1));

var sequence2 = TestUtils.GetMultiSegmentSequence(
Encoding.ASCII.GetBytes("\r\n"),
Encoding.ASCII.GetBytes("\r\n")
);
Assert.IsFalse(HttpUtils.IsHttpHeader(sequence2));

var sequence3 = TestUtils.GetMultiSegmentSequence(
Encoding.ASCII.GetBytes("GET HTTP/1.1"),
Encoding.ASCII.GetBytes("\r\nHost: ip.sb\r\n"),
Encoding.ASCII.GetBytes("User-Agent: curl/7.55.1\r\n"),
Encoding.ASCII.GetBytes("\r\n")
);
Assert.IsFalse(HttpUtils.IsHttpHeader(sequence3));
}
}
}
22 changes: 19 additions & 3 deletions UnitTest/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public static ReadOnlySequence<byte> GetMultiSegmentSequence(Memory<byte> source
{
Requires.Argument(index.LongLength > 1, nameof(index), @"index length must >1");
var orderedIndex = index.OrderBy(x => x);
var first = new BufferSegment(source[..orderedIndex.First()]);

var first = BufferSegment.Empty;
var last = first;
var length = index[0];
var length = 0;

foreach (var i in index.Skip(1))
foreach (var i in orderedIndex)
{
last = last.Append(source.Slice(length, i - length));
length = i;
Expand All @@ -27,6 +27,22 @@ public static ReadOnlySequence<byte> GetMultiSegmentSequence(Memory<byte> source

var sequence = new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length);
Assert.AreEqual(source.Length, sequence.Length);

return sequence;
}

public static ReadOnlySequence<byte> GetMultiSegmentSequence(params Memory<byte>[] memories)
{
Requires.Argument(memories.LongLength > 1, nameof(memories), @"index length must >1");
var first = BufferSegment.Empty;

var last = memories.Aggregate(first, (current, memory) => current.Append(memory));

var sequence = new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length);

var length = memories.Sum(x => (long)x.Length);
Assert.AreEqual(length, sequence.Length);

return sequence;
}
}
Expand Down

0 comments on commit 634ad8d

Please sign in to comment.