Skip to content

Commit 3d523c9

Browse files
committed
Add some basic tests for formatting httprequestmessages
1 parent a659f8c commit 3d523c9

10 files changed

+123
-0
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ trim_trailing_whitespace = true
99
[*.md]
1010
trim_trailing_whitespace = false
1111

12+
[*.verified.http]
13+
insert_final_newline = false
14+
1215
[*.cs]
1316
indent_size = 4
1417
dotnet_style_qualification_for_field = false:warning

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.verified.http text eol=crlf
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Globalization;
2+
3+
namespace TestableHttpClient.Utils;
4+
5+
internal static class HttpRequestMessageFormatter
6+
{
7+
internal static string Format(HttpRequestMessage? request, HttpRequestMessageFormatOptions options)
8+
{
9+
if (request is null)
10+
{
11+
return "null";
12+
}
13+
14+
IFormatProvider formatProvider = CultureInfo.InvariantCulture;
15+
StringBuilder builder = new();
16+
builder.Append(formatProvider, $"{request.Method} {request.RequestUri} HTTP/{request.Version}\r\n");
17+
foreach(var header in request.Headers)
18+
{
19+
builder.Append(formatProvider, $"{header.Key}: {string.Join(", ", header.Value)}\r\n");
20+
}
21+
foreach(var header in request.Content?.Headers ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>())
22+
{
23+
builder.Append(formatProvider, $"{header.Key}: {string.Join(", ", header.Value)}\r\n");
24+
}
25+
builder.Append("\r\n");
26+
builder.Append(request.Content?.ReadAsStringAsync().GetAwaiter().GetResult());
27+
28+
return builder.ToString();
29+
}
30+
}
31+
32+
[Flags]
33+
internal enum HttpRequestMessageFormatOptions
34+
{
35+
HttpMethod = 1,
36+
RequestUri = 2,
37+
HttpVersion = 4,
38+
RequestLine = HttpMethod | RequestUri | HttpVersion,
39+
Headers = 8,
40+
Content = 16,
41+
All = RequestLine | Headers | Content
42+
}

src/TestableHttpClient/Utils/NetStandardPollyFill.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if NETSTANDARD
22

3+
using System.Runtime.CompilerServices;
4+
35
namespace TestableHttpClient.Utils;
46

57
internal static class NetStandardPollyFill
@@ -13,6 +15,11 @@ public static string Replace(this string input, string oldValue, string newValue
1315
{
1416
return input.Replace(oldValue, newValue);
1517
}
18+
19+
public static StringBuilder Append(this StringBuilder builder, IFormatProvider? provider, string handler)
20+
{
21+
return builder.Append(handler);
22+
}
1623
}
1724

1825
#endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GET https://example.com/ HTTP/1.0
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GET https://example.com/ HTTP/1.1
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GET https://example.com/ HTTP/2.0
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GET https://example.com/ HTTP/3.0
2+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POST https://example.com/ HTTP/1.1
2+
Content-Type: text/plain; charset=utf-8
3+
Content-Length: 13
4+
5+
Hello, World!
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.IO;
2+
using System.Runtime.CompilerServices;
3+
4+
using TestableHttpClient.Utils;
5+
6+
namespace TestableHttpClient.Tests.Utils;
7+
8+
public sealed class HttpRequestMessageFormatterTests
9+
{
10+
private static string FetchTestData(string filename, [CallerFilePath] string callerFilePath = "")
11+
{
12+
string directory = Path.GetDirectoryName(callerFilePath)!;
13+
string filePath = Path.Combine(directory, "HttpRequestMessageFormatterTestData", filename);
14+
filePath += ".verified.http";
15+
return File.ReadAllText(filePath);
16+
}
17+
[Fact]
18+
public void Format_NullRequest_CreatesExpectedString()
19+
{
20+
HttpRequestMessage? request = null;
21+
22+
var result = HttpRequestMessageFormatter.Format(request, HttpRequestMessageFormatOptions.All);
23+
24+
Assert.Equal("null", result);
25+
}
26+
27+
[Theory]
28+
[InlineData("1.0")]
29+
[InlineData("1.1")]
30+
[InlineData("2.0")]
31+
[InlineData("3.0")]
32+
public void Format_SimpleGetRequest_CreatesExpectedString(string version)
33+
{
34+
using HttpRequestMessage request = new(HttpMethod.Get, "https://example.com")
35+
{
36+
Version = Version.Parse(version)
37+
};
38+
string result = HttpRequestMessageFormatter.Format(request, HttpRequestMessageFormatOptions.All);
39+
40+
string expected = FetchTestData($"simple_get_request_version_{version}");
41+
42+
Assert.Equal(expected, result);
43+
}
44+
45+
[Fact]
46+
public void Format_SimplePostRequestWithHttpVersion2_CreatesExpectedString()
47+
{
48+
using HttpRequestMessage request = new(HttpMethod.Post, "https://example.com");
49+
request.Content = new StringContent("Hello, World!");
50+
request.Content.Headers.ContentLength = 13;
51+
string result = HttpRequestMessageFormatter.Format(request, HttpRequestMessageFormatOptions.All);
52+
53+
string expected = FetchTestData("simple_post_request");
54+
55+
Assert.Equal(expected, result);
56+
}
57+
}

0 commit comments

Comments
 (0)