Skip to content

Commit

Permalink
Merge pull request #16 from griesoft/dev
Browse files Browse the repository at this point in the history
Adding a ValidationResponse property
  • Loading branch information
jooni91 authored Aug 14, 2022
2 parents 3975fe0 + 40dc647 commit 00ee58e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/Griesoft.AspNetCore.ReCaptcha.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/ReCaptcha/Services/IRecaptchaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ namespace Griesoft.AspNetCore.ReCaptcha.Services
/// </summary>
public interface IRecaptchaService
{
/// <summary>
/// Access the validation response of the last validation that this service did perform.
/// </summary>
/// <remarks>
/// This service is registered as transient (or should be) which means the validation response will
/// always match the request that instantiated this service.
/// </remarks>
ValidationResponse? ValidationResponse { get; }

/// <summary>
/// Validate the reCAPTCHA response token.
/// </summary>
Expand Down
9 changes: 7 additions & 2 deletions src/ReCaptcha/Services/RecaptchaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public RecaptchaService(IOptionsMonitor<RecaptchaSettings> settings,
_logger = logger;
}

/// <inheritdoc />
public ValidationResponse? ValidationResponse { get; private set; }

/// <inheritdoc />
public async Task<ValidationResponse> ValidateRecaptchaResponse(string token, string? remoteIp = null)
{
Expand All @@ -40,7 +43,7 @@ public async Task<ValidationResponse> ValidateRecaptchaResponse(string token, st

response.EnsureSuccessStatusCode();

return JsonConvert.DeserializeObject<ValidationResponse>(
ValidationResponse = JsonConvert.DeserializeObject<ValidationResponse>(
await response.Content.ReadAsStringAsync()
.ConfigureAwait(true))
?? new ValidationResponse()
Expand All @@ -55,7 +58,7 @@ await response.Content.ReadAsStringAsync()
catch (HttpRequestException)
{
_logger.ValidationRequestFailed();
return new ValidationResponse()
ValidationResponse = new ValidationResponse()
{
Success = false,
ErrorMessages = new List<string>()
Expand All @@ -69,6 +72,8 @@ await response.Content.ReadAsStringAsync()
_logger.ValidationRequestUnexpectedException(ex);
throw;
}

return ValidationResponse;
}
}
}
16 changes: 16 additions & 0 deletions tests/ReCaptcha.Tests/Services/RecaptchaServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,21 @@ public async Task ValidateRecaptchaResponse_ShouldReturn_DeserializedResponse()
Assert.IsTrue(response.Success);
Assert.AreEqual(0, response.Errors.Count());
}

[Test]
public async Task ValidateRecaptchaResponse_Should_DeserializedResponse_AndSet_ValidationResponseProperty()
{
// Arrange
var service = new RecaptchaService(_settingsMock.Object, _httpClient, _logger);

// Act
var response = await service.ValidateRecaptchaResponse(Token);

// Assert
_httpMessageHandlerMock.Verify();
Assert.IsTrue(response.Success);
Assert.AreEqual(0, response.Errors.Count());
Assert.NotNull(service.ValidationResponse);
}
}
}

0 comments on commit 00ee58e

Please sign in to comment.