Skip to content

Commit cfa6f99

Browse files
Merge pull request #10 from AlexanderDotH/develop
feat: Add HTTP version configuration support with fluent API methods …
2 parents 828b6ba + bf48092 commit cfa6f99

6 files changed

Lines changed: 159 additions & 39 deletions

File tree

DevBase.Net/Core/BaseRequest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using System.Text;
23
using DevBase.Net.Configuration;
34
using DevBase.Net.Data.Body;
@@ -20,6 +21,8 @@ public abstract class BaseRequest : IDisposable, IAsyncDisposable
2021
protected bool _validateCertificates = true;
2122
protected bool _followRedirects = true;
2223
protected int _maxRedirects = 50;
24+
protected Version _httpVersion = new Version(3, 0);
25+
protected HttpVersionPolicy _httpVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
2326
protected bool _isBuilt;
2427
protected bool _disposed;
2528

@@ -66,6 +69,16 @@ public abstract class BaseRequest : IDisposable, IAsyncDisposable
6669
/// </summary>
6770
public int MaxRedirects => this._maxRedirects;
6871

72+
/// <summary>
73+
/// Gets the HTTP version for this request.
74+
/// </summary>
75+
public Version HttpVersion => this._httpVersion;
76+
77+
/// <summary>
78+
/// Gets the HTTP version policy for this request.
79+
/// </summary>
80+
public HttpVersionPolicy HttpVersionPolicy => this._httpVersionPolicy;
81+
6982
/// <summary>
7083
/// Gets whether the request has been built.
7184
/// </summary>

DevBase.Net/Core/RequestConfiguration.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,36 @@ public Request WithResponseInterceptor(IResponseInterceptor interceptor)
274274
this._responseInterceptors.Add(interceptor);
275275
return this;
276276
}
277+
278+
/// <summary>
279+
/// Sets the HTTP version for the request.
280+
/// </summary>
281+
/// <param name="version">The HTTP version to use.</param>
282+
/// <param name="policy">The version policy. Default is RequestVersionOrLower.</param>
283+
/// <returns>The request instance for method chaining.</returns>
284+
public Request WithHttpVersion(Version version, HttpVersionPolicy policy = HttpVersionPolicy.RequestVersionOrLower)
285+
{
286+
ArgumentNullException.ThrowIfNull(version);
287+
this._httpVersion = version;
288+
this._httpVersionPolicy = policy;
289+
return this;
290+
}
291+
292+
/// <summary>
293+
/// Configures the request to use HTTP/1.1.
294+
/// </summary>
295+
/// <returns>The request instance for method chaining.</returns>
296+
public Request AsHttp11() => this.WithHttpVersion(System.Net.HttpVersion.Version11);
297+
298+
/// <summary>
299+
/// Configures the request to use HTTP/2.
300+
/// </summary>
301+
/// <returns>The request instance for method chaining.</returns>
302+
public Request AsHttp2() => this.WithHttpVersion(System.Net.HttpVersion.Version20);
303+
304+
/// <summary>
305+
/// Configures the request to use HTTP/3.
306+
/// </summary>
307+
/// <returns>The request instance for method chaining.</returns>
308+
public Request AsHttp3() => this.WithHttpVersion(System.Net.HttpVersion.Version30);
277309
}

DevBase.Net/Core/RequestHttp.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public override async Task<Response> SendAsync(CancellationToken cancellationTok
132132
metricsBuilder.SetProxy(this._proxy != null, this._proxy?.Key);
133133

134134
using HttpRequestMessage httpRequest = this.ToHttpRequestMessage();
135-
httpRequest.Version = new Version(3, 0);
136-
httpRequest.VersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
135+
httpRequest.Version = this._httpVersion;
136+
httpRequest.VersionPolicy = this._httpVersionPolicy;
137137

138138
metricsBuilder.MarkConnectStart();
139139
HttpResponseMessage httpResponse = await client.SendAsync(httpRequest,
@@ -284,6 +284,8 @@ private string BuildClientKey()
284284
sb.Append(this._validateCertificates);
285285
sb.Append("|redirect:");
286286
sb.Append(this._followRedirects);
287+
sb.Append("|httpver:");
288+
sb.Append(this._httpVersion);
287289

288290
return sb.ToStringAndRelease();
289291
}

DevBase.Net/DevBase.Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageProjectUrl>https://github.com/AlexanderDotH/DevBase.git</PackageProjectUrl>
1616
<RepositoryUrl>https://github.com/AlexanderDotH/DevBase.git</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
18-
<Version>1.3.2</Version>
18+
<Version>1.4.0</Version>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2121
<PackageTags>http;client;requests;proxy;socks5;jwt;authentication;fluent-api;async;retry;rate-limiting;json;html-parsing</PackageTags>

DevBase.Test/DevBaseRequests/RequestTest.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,89 @@ public async Task DisposeAsync_ClearsInterceptors()
577577

578578
#endregion
579579

580+
#region HTTP Version Tests
581+
582+
[Test]
583+
public void WithHttpVersion_SetsHttpVersion()
584+
{
585+
var request = new Request("https://example.com")
586+
.WithHttpVersion(HttpVersion.Version20);
587+
588+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version20));
589+
}
590+
591+
[Test]
592+
public void WithHttpVersion_WithPolicy_SetsBoth()
593+
{
594+
var request = new Request("https://example.com")
595+
.WithHttpVersion(HttpVersion.Version20, HttpVersionPolicy.RequestVersionExact);
596+
597+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version20));
598+
Assert.That(request.HttpVersionPolicy, Is.EqualTo(HttpVersionPolicy.RequestVersionExact));
599+
}
600+
601+
[Test]
602+
public void AsHttp11_SetsHttpVersion11()
603+
{
604+
var request = new Request("https://example.com")
605+
.AsHttp11();
606+
607+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version11));
608+
}
609+
610+
[Test]
611+
public void AsHttp2_SetsHttpVersion20()
612+
{
613+
var request = new Request("https://example.com")
614+
.AsHttp2();
615+
616+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version20));
617+
}
618+
619+
[Test]
620+
public void AsHttp3_SetsHttpVersion30()
621+
{
622+
var request = new Request("https://example.com")
623+
.AsHttp3();
624+
625+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version30));
626+
}
627+
628+
[Test]
629+
public void DefaultHttpVersion_IsHttp3()
630+
{
631+
var request = new Request("https://example.com");
632+
633+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version30));
634+
Assert.That(request.HttpVersionPolicy, Is.EqualTo(HttpVersionPolicy.RequestVersionOrLower));
635+
}
636+
637+
[Test]
638+
public void HttpVersion_CanBeSwitchedMultipleTimes()
639+
{
640+
var request = new Request("https://example.com")
641+
.AsHttp3()
642+
.AsHttp2()
643+
.AsHttp11();
644+
645+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version11));
646+
}
647+
648+
[Test]
649+
public void FluentApi_WithHttpVersion_ChainsCorrectly()
650+
{
651+
var request = new Request("https://example.com")
652+
.AsPost()
653+
.AsHttp2()
654+
.WithHeader("X-Test", "Value")
655+
.Build();
656+
657+
Assert.That(request.Method, Is.EqualTo(HttpMethod.Post));
658+
Assert.That(request.HttpVersion, Is.EqualTo(HttpVersion.Version20));
659+
}
660+
661+
#endregion
662+
580663
#region Fluent API Tests
581664

582665
[Test]

DevBaseLive/Program.cs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515

1616
namespace DevBaseLive;
1717

18-
/// <summary>
19-
/// Represents a person record.
20-
/// </summary>
21-
/// <param name="name">The name of the person.</param>
22-
/// <param name="age">The age of the person.</param>
23-
record Person(string name, int age);
24-
2518
/// <summary>
2619
/// Entry point class for the DevBaseLive application.
2720
/// </summary>
@@ -34,41 +27,38 @@ class Program
3427
/// <param name="args">Command line arguments.</param>
3528
public static async Task Main(string[] args)
3629
{
37-
Person p = new Person("alex", 1);
38-
3930
var l = new LoggerConfiguration()
4031
.WriteTo.Console()
4132
.MinimumLevel.Information()
4233
.CreateLogger();
4334

44-
for (int i = 0; i < 20; i++)
45-
{
46-
Request request = new Request()
47-
.AsGet()
48-
.WithHostCheck(new HostCheckConfig())
49-
.UseBasicAuthentication("joe", "mama")
50-
.WithRetryPolicy(new RetryPolicy()
51-
{
52-
MaxRetries = 2
53-
})
54-
.WithLogging(new LoggingConfig()
55-
{
56-
Logger = l
57-
})
58-
.WithMultipleFiles(
59-
("file1", AFile.ReadFileToObject("C:\\Users\\alex\\Desktop\\zoom1.txt")),
60-
("file2", AFile.ReadFileToObject("C:\\Users\\alex\\Desktop\\zoom2.txt"))
61-
)
35+
36+
37+
Request request = new Request()
38+
.AsGet()
39+
.WithHostCheck(new HostCheckConfig())
40+
.UseBasicAuthentication("joe", "mama")
41+
.WithRetryPolicy(new RetryPolicy()
42+
{
43+
MaxRetries = 2
44+
})
45+
.WithLogging(new LoggingConfig()
46+
{
47+
Logger = l
48+
})
49+
.WithMultipleFiles(
50+
("file1", AFile.ReadFileToObject("C:\\Users\\alex\\Desktop\\zoom1.txt")),
51+
("file2", AFile.ReadFileToObject("C:\\Users\\alex\\Desktop\\zoom2.txt"))
52+
)
6253

63-
.WithScrapingBypass(new ScrapingBypassConfig()
64-
{
65-
BrowserProfile = EnumBrowserProfile.Firefox
66-
}).WithHeader("sec-fetch-mode", "yoemamam")
67-
.WithUrl("https://webhook.site/bd100268-d633-43f5-b298-28ee17c97ccf");
68-
Response response = await request.SendAsync();
54+
.WithScrapingBypass(new ScrapingBypassConfig()
55+
{
56+
BrowserProfile = EnumBrowserProfile.Firefox
57+
}).WithHeader("sec-fetch-mode", "yoemamam")
58+
.WithUrl("https://webhook.site/bd100268-d633-43f5-b298-28ee17c97ccf");
59+
Response response = await request.SendAsync();
6960

70-
string data = await response.GetStringAsync();
71-
data.DumpConsole();
72-
}
61+
string data = await response.GetStringAsync();
62+
data.DumpConsole();
7363
}
7464
}

0 commit comments

Comments
 (0)