Skip to content

Commit 116cd9f

Browse files
cancellationToken
1 parent a8f6bf8 commit 116cd9f

3 files changed

Lines changed: 10 additions & 13 deletions

File tree

EssentialCSharp.Web.Tests/Integration/CaptchaTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ public async Task CaptchaService_Verify_Success(CancellationToken cancellationTo
1919
string hCaptchaSecret = "0x0000000000000000000000000000000000000000";
2020
string hCaptchaToken = "10000000-aaaa-bbbb-cccc-000000000001";
2121
string hCaptchaSiteKey = "10000000-ffff-ffff-ffff-000000000001";
22-
// Note: cancellationToken is not passed to VerifyAsync because ICaptchaService.VerifyAsync
23-
// does not currently accept a CancellationToken. If the timeout fires, the underlying
24-
// HTTP request will continue running in the background.
25-
HCaptchaResult? response = await captchaService.VerifyAsync(hCaptchaSecret, hCaptchaToken, hCaptchaSiteKey);
22+
HCaptchaResult? response = await captchaService.VerifyAsync(hCaptchaSecret, hCaptchaToken, hCaptchaSiteKey, cancellationToken);
2623

2724
await Assert.That(response).IsNotNull();
2825
await Assert.That(response.Success).IsTrue();

EssentialCSharp.Web/Services/CaptchaService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
1111

1212
// Verify captcha. Optionally add overload to pass in remoteIp as in the docs
1313
// https://docs.hcaptcha.com/#verify-the-user-response-server-side
14-
public async Task<HCaptchaResult?> VerifyAsync(string secret, string response, string sitekey)
14+
public async Task<HCaptchaResult?> VerifyAsync(string secret, string response, string sitekey, CancellationToken cancellationToken = default)
1515
{
1616
// create post data
1717
List<KeyValuePair<string, string>> postData =
@@ -21,10 +21,10 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
2121
new KeyValuePair<string, string>("sitekey", sitekey)
2222
];
2323

24-
return await PostVerification(postData);
24+
return await PostVerification(postData, cancellationToken);
2525
}
2626

27-
public async Task<HCaptchaResult?> VerifyAsync(string? response)
27+
public async Task<HCaptchaResult?> VerifyAsync(string? response, CancellationToken cancellationToken = default)
2828
{
2929
if (string.IsNullOrWhiteSpace(response))
3030
{
@@ -33,21 +33,21 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
3333
string secret = Options.SecretKey ?? throw new InvalidOperationException($"{CaptchaOptions.CaptchaSender} {nameof(Options.SecretKey)} is unexpectedly null");
3434
string sitekey = Options.SiteKey ?? throw new InvalidOperationException($"{CaptchaOptions.CaptchaSender} {nameof(Options.SiteKey)} is unexpectedly null");
3535

36-
return await VerifyAsync(secret, response, sitekey);
36+
return await VerifyAsync(secret, response, sitekey, cancellationToken);
3737
}
3838

39-
public async Task<HCaptchaResult?> PostVerification(List<KeyValuePair<string, string>> postData)
39+
public async Task<HCaptchaResult?> PostVerification(List<KeyValuePair<string, string>> postData, CancellationToken cancellationToken = default)
4040
{
4141
HttpClient client = ClientFactory.CreateClient("hCaptcha");
4242

4343
// request api
4444
HttpResponseMessage res = await client.PostAsync(
4545
// base url is given in IHttpClientFactory service registration
4646
// hCaptcha wants URL-encoded POST
47-
"/siteverify", new FormUrlEncodedContent(postData));
47+
"/siteverify", new FormUrlEncodedContent(postData), cancellationToken);
4848

4949
res.EnsureSuccessStatusCode();
5050
// convert JSON string into Class
51-
return JsonSerializer.Deserialize<HCaptchaResult>(await res.Content.ReadAsStringAsync());
51+
return JsonSerializer.Deserialize<HCaptchaResult>(await res.Content.ReadAsStringAsync(cancellationToken));
5252
}
5353
}

EssentialCSharp.Web/Services/ICaptchaService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace EssentialCSharp.Web.Services;
44

55
public interface ICaptchaService
66
{
7-
Task<HCaptchaResult?> VerifyAsync(string secret, string response, string sitekey);
8-
Task<HCaptchaResult?> VerifyAsync(string? response);
7+
Task<HCaptchaResult?> VerifyAsync(string secret, string response, string sitekey, CancellationToken cancellationToken = default);
8+
Task<HCaptchaResult?> VerifyAsync(string? response, CancellationToken cancellationToken = default);
99
}

0 commit comments

Comments
 (0)