Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@
}

[Test]
public async Task RecaptchaV2_IncorrectProxyPort_ShouldThrowArgumentException()

Check warning on line 827 in CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 827 in CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
Action actual = () => ObjectGen.RecaptchaV2.CreateTask(
proxyPort: Gen.RandomInt(65535));
Expand Down Expand Up @@ -1117,6 +1117,48 @@
sut.GetActualRequests().Should().BeEquivalentTo(expectedRequests);
actual.Should().BeEquivalentTo(expectedResult);
}

[Test]
public async Task MTCaptcha_ShouldSolve()
{
var clientKey = Gen.RandomString();
var taskId = Gen.RandomInt();

var captchaRequest = ObjectGen.MTCaptchaTask.CreateTask();
var expectedResult = ObjectGen.MTCaptchaTask.CreateSolution();

var expectedRequests = new List<(RequestType Type, string ExpectedRequest)>
{
(
Type: RequestType.CreateTask,
ExpectedRequest: JsonConvert.SerializeObject(new
{ clientKey = clientKey, task = captchaRequest, softId = 53 })
),
(
Type: RequestType.GetTaskResult,
ExpectedRequest: JsonConvert.SerializeObject(new { clientKey = clientKey, taskId = taskId })
),
};

var captchaResults = new List<object>
{
new { taskId = taskId, errorId = 0, errorCode = (string)null! },
new
{
status = "ready",
solution = new { token = expectedResult.Solution.Value },
errorId = 0,
errorCode = (string)null!
}
};

var sut = new Sut(clientKey);
sut.SetupHttpServer(captchaResults);

var actual = await sut.SolveAsync(captchaRequest);

sut.GetActualRequests().Should().BeEquivalentTo(expectedRequests);
actual.Should().BeEquivalentTo(expectedResult);
}
}
}
29 changes: 29 additions & 0 deletions CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,36 @@ public static CaptchaResult<CustomTaskResponse> CreateSolution()
}
};
}
}

public static class MTCaptchaTask
{
public static MTCaptchaTaskRequest CreateTask()
{
return new MTCaptchaTaskRequest
{
WebsiteUrl = Gen.RandomUri().ToString(),
WebsiteKey = Gen.RandomString(),
Invisible = Gen.RandomBool(),
PageAction = Gen.RandomString(),
UserAgent = Gen.UserAgent(),
Proxy = new ProxyContainer(
Gen.RandomString(), Gen.RandomInt(0, 65535),
Gen.RandomEnum<ProxyType>(), Gen.RandomString(), Gen.RandomString())
};
}

public static CaptchaResult<MTCaptchaTaskResponse> CreateSolution()
{
return new CaptchaResult<MTCaptchaTaskResponse>
{
Error = null,
Solution = new MTCaptchaTaskResponse
{
Value = Gen.RandomString()
}
};
}
}
}
}
2 changes: 2 additions & 0 deletions CapMonsterCloud.Client.IntegrationTests/Sut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public async Task<CaptchaResult<DynamicComplexImageTaskResponse>> SolveAsync(
public async Task<CaptchaResult<CustomTaskResponse>> SolveAsync(
TemuCustomTaskRequest request) => await _cloudClient.SolveAsync<CustomTaskResponse>(request);

public async Task<CaptchaResult<MTCaptchaTaskResponse>> SolveAsync(
MTCaptchaTaskRequest request) => await _cloudClient.SolveAsync<MTCaptchaTaskResponse>(request);

public async Task<decimal> GetBalanceAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ private static GetResultTimeouts GetTimeouts(Type type)
Timeout = TimeSpan.FromSeconds(180)
}
},
};
{
typeof(MTCaptchaTaskRequest),
new GetResultTimeouts
{
FirstRequestDelay = TimeSpan.FromSeconds(1),
FirstRequestNoCacheDelay = TimeSpan.FromSeconds(10),
RequestsInterval = TimeSpan.FromSeconds(3),
Timeout = TimeSpan.FromSeconds(180)
}
}, };
}
}
57 changes: 57 additions & 0 deletions CapMonsterCloud.Client/Requests/MTCaptchaTaskRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Zennolab.CapMonsterCloud.Responses;

namespace Zennolab.CapMonsterCloud.Requests
{
/// <summary>
/// MTCaptcha recognition request.
/// </summary>
/// <example>
/// https://docs.capmonster.cloud/docs/captchas/mtcaptcha-task/
/// </example>
public sealed class MTCaptchaTaskRequest : CaptchaRequestBaseWithProxy<MTCaptchaTaskResponse>
{
/// <summary>
/// Recognition task type
/// </summary>
public const string TaskType = "MTCaptchaTask";

/// <inheritdoc/>
[JsonProperty("type", Required = Required.Always)]
public sealed override string Type => TaskType;

/// <summary>
/// Address of a web page with MTCaptcha.
/// </summary>
[JsonProperty("websiteURL", Required = Required.Always)]
[Url]
public string WebsiteUrl { get; set; }

/// <summary>
/// The MTCaptcha key (sk/sitekey).
/// </summary>
[JsonProperty("websiteKey", Required = Required.Always)]
[StringLength(int.MaxValue, MinimumLength = 1)]
public string WebsiteKey { get; set; }

/// <summary>
/// true for invisible widget (has hidden confirmation field).
/// </summary>
[JsonProperty("isInvisible")]
public bool Invisible { get; set; }

/// <summary>
/// Action value (passed as "act" and shown during token validation).
/// Provide only if it differs from default "%24".
/// </summary>
[JsonProperty("pageAction")]
public string PageAction { get; set; }

/// <summary>
/// Browser's User-Agent (actual Windows UA recommended).
/// </summary>
[JsonProperty("userAgent")]
public string UserAgent { get; set; }
}
}
16 changes: 16 additions & 0 deletions CapMonsterCloud.Client/Responses/MTCaptchaTaskResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace Zennolab.CapMonsterCloud.Responses
{
/// <summary>
/// MTCaptcha recognition response
/// </summary>
public sealed class MTCaptchaTaskResponse : CaptchaResponseBase
{
/// <summary>
/// MTCaptcha token to submit to the target site.
/// </summary>
[JsonProperty("token")]
public string Value { get; set; }
}
}
Loading