Skip to content

Commit

Permalink
feat(repeater): address pr comments
Browse files Browse the repository at this point in the history
closes #170
  • Loading branch information
ostridm committed May 31, 2024
1 parent 82502d2 commit 57e168a
Show file tree
Hide file tree
Showing 21 changed files with 591 additions and 461 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions src/SecTester.Repeater/Bus/HttpMessage.cs

This file was deleted.

19 changes: 16 additions & 3 deletions src/SecTester.Repeater/Bus/IncomingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
using System.Reflection;
using System.Runtime.Serialization;
using MessagePack;
using SecTester.Repeater.Bus.Formatters;
using SecTester.Repeater.Internal;
using SecTester.Repeater.Runners;

namespace SecTester.Repeater.Bus;

[MessagePackObject]
public record IncomingRequest(Uri Url) : HttpMessage, IRequest
public record IncomingRequest(Uri Url) : IRequest
{
private const string UrlKey = "url";
private const string MethodKey = "method";
private const string HeadersKey = "headers";
private const string BodyKey = "body";
private const string ProtocolKey = "protocol";

private static readonly Dictionary<string, Protocol> ProtocolEntries = typeof(Protocol)
.GetFields(BindingFlags.Public | BindingFlags.Static)
Expand All @@ -26,8 +29,18 @@ public record IncomingRequest(Uri Url) : HttpMessage, IRequest
})
.ToDictionary(x => x.StringValue, x => x.Value);


[Key(ProtocolKey)]
public Protocol Protocol { get; set; } = Protocol.Http;

[Key(HeadersKey)]
public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Headers { get; set; } =
new List<KeyValuePair<string, IEnumerable<string>>>();

[Key(BodyKey)]
public string? Body { get; set; }

[Key(MethodKey)]
[MessagePackFormatter(typeof(MessagePackHttpMethodFormatter))]
public HttpMethod Method { get; set; } = HttpMethod.Get;

[Key(UrlKey)]
Expand Down
15 changes: 14 additions & 1 deletion src/SecTester.Repeater/Bus/OutgoingResponse.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
using System.Collections.Generic;
using MessagePack;
using SecTester.Repeater.Internal;
using SecTester.Repeater.Runners;

namespace SecTester.Repeater.Bus;

[MessagePackObject]
public record OutgoingResponse : HttpMessage, IResponse
public record OutgoingResponse : IResponse
{
[Key("protocol")]
public Protocol Protocol { get; set; } = Protocol.Http;

[Key("statusCode")]
public int? StatusCode { get; set; }

[Key("body")]
public string? Body { get; set; }

[Key("message")]
public string? Message { get; set; }

[Key("errorCode")]
public string? ErrorCode { get; set; }

[Key("headers")]
public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Headers { get; set; } =
new List<KeyValuePair<string, IEnumerable<string>>>();

}
12 changes: 2 additions & 10 deletions src/SecTester.Repeater/Bus/SocketIoMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using SecTester.Repeater.Bus.Formatters;
using SecTester.Repeater.Internal;
using SocketIOClient;

namespace SecTester.Repeater.Bus;
Expand All @@ -16,15 +16,7 @@ public SocketIoMessage(SocketIOResponse response)
_response = response ?? throw new ArgumentNullException(nameof(response));
}

public virtual T GetValue<T>(int index = 0)
{
if (typeof(T) == typeof(IncomingRequest))
{
return (T)(object)IncomingRequest.FromDictionary(_response.GetValue<Dictionary<object, object>>(index));
}

return _response.GetValue<T>(index);
}
public virtual T GetValue<T>(int index = 0) => _response.GetValue<T>(index);

public virtual Task CallbackAsync(params object[] data) => _response.CallbackAsync(data);

Expand Down
3 changes: 2 additions & 1 deletion src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
Expand Down Expand Up @@ -64,7 +65,7 @@ private void DelegateEvents()
}
var ct = new CancellationTokenSource(_options.AckTimeout);
var request = response.GetValue<IncomingRequest>();
var request = IncomingRequest.FromDictionary(response.GetValue<Dictionary<object, object>>());
var result = await RequestReceived.Invoke(request).ConfigureAwait(false);
await response.CallbackAsync(ct.Token, result).ConfigureAwait(false);
});
Expand Down
Loading

0 comments on commit 57e168a

Please sign in to comment.