Skip to content

Commit d910ce5

Browse files
committed
feat: check content-type header
1 parent a3af3be commit d910ce5

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/Voxel.MiddyNet.HttpJsonBodyParserMiddleware/HttpJsonBodyParserMiddleware.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ public class HttpJsonBodyParserMiddleware<T> : ILambdaMiddleware<APIGatewayProxy
99
{
1010
public Task Before(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
1111
{
12+
if (!HasJSONContentHeaders(lambdaEvent))
13+
{
14+
context.AdditionalContext.Add("Body", lambdaEvent.Body);
15+
return Task.CompletedTask;
16+
}
17+
1218
T source;
1319
try
1420
{
1521
source = JsonConvert.DeserializeObject<T>(lambdaEvent.Body);
1622
}
17-
catch (JsonReaderException ex)
23+
catch (JsonReaderException)
1824
{
1925
throw new Exception($"Error parsing \"{lambdaEvent.Body}\" to type {typeof(T)}");
2026
}
@@ -23,6 +29,13 @@ public Task Before(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
2329
return Task.CompletedTask;
2430
}
2531

32+
private static bool HasJSONContentHeaders(APIGatewayProxyRequest lambdaEvent)
33+
{
34+
return lambdaEvent.Headers != null &&
35+
(lambdaEvent.Headers.ContainsKey("Content-Type") &&
36+
lambdaEvent.Headers["Content-Type"] == "application/json");
37+
}
38+
2639
public Task<APIGatewayProxyResponse> After(APIGatewayProxyResponse lambdaResponse, MiddyNetContext context) => Task.FromResult(lambdaResponse);
2740
}
2841
}

test/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests/HttpJsonBodyParserMiddlewareShould.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests
1212
{
1313
public class HttpJsonBodyParserMiddlewareShould
1414
{
15+
private MiddyNetContext context;
16+
private TestObject expectation;
17+
private string serializedExpectation;
18+
19+
public HttpJsonBodyParserMiddlewareShould()
20+
{
21+
context = new MiddyNetContext(Substitute.For<ILambdaContext>());
22+
expectation = new TestObject("bar");
23+
serializedExpectation = JsonConvert.SerializeObject(expectation);
24+
}
25+
1526
[Fact]
1627
public async Task ProcessTheJsonRequest()
1728
{
18-
var context = new MiddyNetContext(Substitute.For<ILambdaContext>());
19-
var expectation = new TestObject("bar");
20-
var source = JsonConvert.SerializeObject(expectation);
2129
var request = new APIGatewayProxyRequest()
2230
{
2331
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } },
24-
Body = source
32+
Body = serializedExpectation
2533
};
2634
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
2735
await middleware.Before(request, context);
@@ -33,9 +41,7 @@ public async Task ProcessTheJsonRequest()
3341
[Fact]
3442
public async Task ErrorWhenJsonNotMapsToObject()
3543
{
36-
var context = new MiddyNetContext(Substitute.For<ILambdaContext>());
37-
var expectation = new TestObject("bar");
38-
var source = "Not Mapped object" + JsonConvert.SerializeObject(expectation);
44+
var source = "Not Mapped object" + serializedExpectation;
3945
var request = new APIGatewayProxyRequest()
4046
{
4147
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } },
@@ -46,5 +52,19 @@ public async Task ErrorWhenJsonNotMapsToObject()
4652

4753
action.Should().Throw<Exception>().WithMessage($"Error parsing \"{source}\" to type {typeof(TestObject)}");
4854
}
55+
56+
[Fact]
57+
public async Task NotProcessTheBodyIfNoHeaderIsPassed()
58+
{
59+
var request = new APIGatewayProxyRequest()
60+
{
61+
Body = serializedExpectation
62+
};
63+
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
64+
await middleware.Before(request, context);
65+
66+
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
67+
context.AdditionalContext["Body"].Should().Be(serializedExpectation);
68+
}
4969
}
5070
}

0 commit comments

Comments
 (0)