Skip to content

Commit

Permalink
feat(repeater): improve coverage
Browse files Browse the repository at this point in the history
closes #170
  • Loading branch information
ostridm committed May 24, 2024
1 parent 5428396 commit ad15fcb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/SecTester.Repeater/Bus/IncomingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace SecTester.Repeater.Bus;

[MessagePackObject]
public record IncomingRequest(Uri Url): HttpMessage, IRequest
public record IncomingRequest(Uri Url) : HttpMessage, IRequest
{
private const string UrlKey = "url";
private const string MethodKey = "method";
Expand Down
2 changes: 1 addition & 1 deletion src/SecTester.Repeater/Bus/OutgoingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace SecTester.Repeater.Bus;

[MessagePackObject]
public record OutgoingResponse : HttpMessage, IResponse
public record OutgoingResponse : HttpMessage, IResponse
{
[Key("statusCode")]
public int? StatusCode { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ private static IEnumerable<
}
};

public static readonly IEnumerable<object[]> WrongFormatFixtures = new List<object[]>
{
new object[]
{
"{\"headers\":5}",
},
new object[]
{
"{\"headers\":[]}",
},
new object[]
{
"{\"headers\":{\"content-type\":{\"foo\"}:{\"bar\"}}}",
}
};

public static IEnumerable<object?[]> SerializeDeserializeFixtures => Fixtures
.Select((x) => new object?[]
{
Expand Down Expand Up @@ -76,4 +92,19 @@ public void HttpHeadersMessagePackFormatter_Deserialize_ShouldCorrectlyHandleMis
Headers = null
});
}

[Theory]
[MemberData(nameof(WrongFormatFixtures))]
public void HttpHeadersMessagePackFormatter_Deserialize_ShouldThrow(
string input)
{
// arrange
var binary = MessagePackSerializer.ConvertFromJson(input, Options);

// act
var act = () => MessagePackSerializer.Deserialize<HttpHeadersDto>(binary, Options);

// assert
act.Should().Throw<MessagePackSerializationException>().WithMessage("Failed to deserialize SecTester.Repeater.Tests.Bus.Formatters.MessagePackHttpHeadersFormatterTests+HttpHeadersDto value.");
}
}
20 changes: 20 additions & 0 deletions test/SecTester.Repeater.Tests/Bus/IncomingRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ public void IncomingRequest_FromDictionary_ShouldAssignDefaultPropertyValues()
// assert
result.Should().BeEquivalentTo(new IncomingRequest(new Uri("https://foo.bar/1")));
}

[Fact]
public void IncomingRequest_FromDictionary_ShouldParseEnumNamedValue()
{
// arrange
var packJson =
"{\"type\":2,\"data\":[\"request\",{\"protocol\":\"http\",\"url\":\"https://foo.bar/1\"}],\"options\":{\"compress\":true},\"id\":1,\"nsp\":\"/some\"}";

var serializer = new SocketIOMessagePackSerializer(MessagePackSerializerOptions.Standard);

var deserializedPackMessage = MessagePackSerializer.Deserialize<PackMessage>(MessagePackSerializer.ConvertFromJson(packJson));

var deserializedDictionary = serializer.Deserialize<Dictionary<object, object>>(deserializedPackMessage, 1);

// act
var result = IncomingRequest.FromDictionary(deserializedDictionary);

// assert
result.Should().BeEquivalentTo(new IncomingRequest(new Uri("https://foo.bar/1")){ Protocol = Protocol.Http});
}
}

0 comments on commit ad15fcb

Please sign in to comment.