|
| 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