Skip to content

Commit

Permalink
Update webhook client.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jan 27, 2025
1 parent c557fae commit d70f5a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tools/TestSuite/TestSuite.ApiTests/EventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task Should_cancel_known_event()
},
Scheduling = new SchedulingDto
{
Date = DateTime.Now.AddDays(20),
Date = DateTimeOffset.UtcNow.AddDays(20),
Time = new TimeSpan(12, 0, 0),
},
Id = eventId,
Expand All @@ -90,7 +90,7 @@ private async Task<bool> PollCancelAsync(string userId, string eventId)
{
while (!cts.IsCancellationRequested)
{
var result = await _.Client.Events.CancelEventAsync(_.AppId, request);
var result = await _.Client.Events.CancelEventAsync(_.AppId, request, default);

if (result.HasCancelled)
{
Expand Down
55 changes: 42 additions & 13 deletions tools/TestSuite/TestSuite.ApiTests/WebhookCatcherClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,42 @@

namespace TestSuite.ApiTests;

internal sealed class WebhookSession
public sealed class WebhookSession
{
public string Uuid { get; set; }
}

internal sealed class WebhookRequest
public sealed class WebhookRequest
{
[JsonPropertyName("uuid")]
public string Uuid { get; set; }

[JsonPropertyName("method")]
public string Method { get; set; }

[JsonPropertyName("content_base64")]
[JsonIgnore]
public string Content { get; set; }

[JsonIgnore]
public Dictionary<string, string> Headers { get; set; } = [];

[JsonPropertyName("request_payload_base64")]
public string EncodedContent { get; set; }

[JsonPropertyName("headers")]
public WebhookHeader[] HeaderList { get; set; }
}

public sealed class WebhookHeader
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("value")]
public string Value { get; set; }
}

internal sealed class WebhookCatcherClient
public sealed class WebhookCatcherClient
{
private readonly HttpClient httpClient;

Expand Down Expand Up @@ -60,29 +80,38 @@ public WebhookCatcherClient(string apiHost, int apiPort, string endpointHost, in
public async Task<(string, string)> CreateSessionAsync(
CancellationToken ct = default)
{
var response = await httpClient.PostAsJsonAsync("/api/session", new { }, ct);
var request = new { status_code = 200, };
var response = await httpClient.PostAsJsonAsync("/api/session", request, ct);

response.EnsureSuccessStatusCode();

var responseObj = (await response.Content.ReadFromJsonAsync<WebhookSession>(cancellationToken: ct))!;

return ($"http://{EndpointHost}:{EndpointPort}/{responseObj.Uuid}", responseObj.Uuid);
var json = await response.Content.ReadFromJsonAsync<WebhookSession>(ct);
return ($"http://{EndpointHost}:{EndpointPort}/{json!.Uuid}", json!.Uuid);
}

public async Task<WebhookRequest[]> GetRequestsAsync(string sessionId,
CancellationToken ct = default)
{
var result = (await httpClient.GetFromJsonAsync<WebhookRequest[]>($"/api/session/{sessionId}/requests", ct))!;
var response = await httpClient.GetFromJsonAsync<WebhookRequest[]>($"/api/session/{sessionId}/requests", ct);

foreach (var request in result)
foreach (var request in response!)
{
if (request.Content != null)
var content = request.EncodedContent;
if (content != null)
{
request.Content = Encoding.Default.GetString(Convert.FromBase64String(content));
}

if (request.HeaderList != null)
{
request.Content = Encoding.Default.GetString(Convert.FromBase64String(request.Content));
foreach (var header in request.HeaderList)
{
request.Headers[header.Name] = header.Value;
}
}
}

return result;
return response;
}

public async Task<WebhookRequest[]> WaitForRequestsAsync(string sessionId, TimeSpan timeout)
Expand Down

0 comments on commit d70f5a0

Please sign in to comment.