Skip to content

Commit 96660c3

Browse files
author
Adit Sheth
committed
Added and updated tests.
1 parent 0d7102d commit 96660c3

File tree

1 file changed

+148
-3
lines changed

1 file changed

+148
-3
lines changed

src/Http/Http.Extensions/test/ProblemDetailsDefaultWriterTest.cs

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ public async Task WriteAsync_AddExtensions()
494494
Assert.Equal("traceId", extension.Key);
495495
Assert.Equal(expectedTraceId, extension.Value.ToString());
496496
});
497+
497498
}
498499

499500
[Fact]
@@ -503,6 +504,10 @@ public async Task WriteAsync_AddExtensions_WithJsonContext()
503504
var options = new JsonOptions();
504505
options.SerializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(CustomProblemDetailsContext.Default, ProblemDetailsJsonContext.Default);
505506

507+
var mockNamingPolicy = new Mock<JsonNamingPolicy>();
508+
mockNamingPolicy.Setup(policy => policy.ConvertName("traceId")).Returns("custom_traceId");
509+
options.SerializerOptions.PropertyNamingPolicy = mockNamingPolicy.Object;
510+
506511
var writer = GetWriter(jsonOptions: options);
507512
var stream = new MemoryStream();
508513
var context = CreateContext(stream);
@@ -517,10 +522,10 @@ public async Task WriteAsync_AddExtensions_WithJsonContext()
517522
ProblemDetails = expectedProblem
518523
};
519524

520-
//Act
525+
// Act
521526
await writer.WriteAsync(problemDetailsContext);
522527

523-
//Assert
528+
// Assert
524529
stream.Position = 0;
525530

526531
var problemDetails = await JsonSerializer.DeserializeAsync<ProblemDetails>(stream, options.SerializerOptions);
@@ -537,7 +542,7 @@ public async Task WriteAsync_AddExtensions_WithJsonContext()
537542
},
538543
(extension) =>
539544
{
540-
Assert.Equal("traceId", extension.Key);
545+
Assert.Equal("custom_traceId", extension.Key); // Updated to reflect the custom naming policy
541546
Assert.Equal(expectedTraceId, extension.Value.ToString());
542547
});
543548
}
@@ -670,6 +675,146 @@ public void CanWrite_ReturnsFalse_WhenJsonNotAccepted(string contentType)
670675
Assert.False(result);
671676
}
672677

678+
[Fact]
679+
public async Task WriteAsync_Respects_CustomNamingPolicy_ForTraceId()
680+
{
681+
// Arrange
682+
var writer = GetWriter();
683+
var stream = new MemoryStream();
684+
var context = CreateContext(stream);
685+
var expectedTraceId = Activity.Current?.Id ?? context.TraceIdentifier;
686+
687+
var mockNamingPolicy = new Mock<JsonNamingPolicy>();
688+
mockNamingPolicy.Setup(policy => policy.ConvertName("traceId")).Returns("custom_traceId");
689+
690+
var serializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = mockNamingPolicy.Object };
691+
SetSerializerOptions(writer, serializerOptions);
692+
693+
var expectedProblem = new ProblemDetails()
694+
{
695+
Status = StatusCodes.Status500InternalServerError
696+
};
697+
698+
var problemDetailsContext = new ProblemDetailsContext()
699+
{
700+
HttpContext = context,
701+
ProblemDetails = expectedProblem
702+
};
703+
704+
// Act
705+
await writer.WriteAsync(problemDetailsContext);
706+
707+
// Assert
708+
stream.Position = 0;
709+
var problemDetails = await JsonSerializer.DeserializeAsync<ProblemDetails>(stream, serializerOptions);
710+
Assert.NotNull(problemDetails);
711+
Assert.Equal(expectedTraceId, problemDetails.Extensions["custom_traceId"].ToString());
712+
Assert.DoesNotContain("traceId", problemDetails.Extensions.Keys);
713+
}
714+
715+
[Fact]
716+
public async Task WriteAsync_FallsBack_WhenNamingPolicyReturnsNull()
717+
{
718+
// Arrange
719+
var writer = GetWriter();
720+
var stream = new MemoryStream();
721+
var context = CreateContext(stream);
722+
var expectedTraceId = Activity.Current?.Id ?? context.TraceIdentifier;
723+
724+
var mockNamingPolicy = new Mock<JsonNamingPolicy>();
725+
mockNamingPolicy.Setup(policy => policy.ConvertName("traceId")).Returns((string)null);
726+
727+
var serializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = mockNamingPolicy.Object };
728+
SetSerializerOptions(writer, serializerOptions);
729+
730+
var expectedProblem = new ProblemDetails()
731+
{
732+
Status = StatusCodes.Status500InternalServerError
733+
};
734+
735+
var problemDetailsContext = new ProblemDetailsContext()
736+
{
737+
HttpContext = context,
738+
ProblemDetails = expectedProblem
739+
};
740+
741+
// Act
742+
await writer.WriteAsync(problemDetailsContext);
743+
744+
// Assert
745+
stream.Position = 0;
746+
var problemDetails = await JsonSerializer.DeserializeAsync<ProblemDetails>(stream, serializerOptions);
747+
Assert.NotNull(problemDetails);
748+
Assert.Equal(expectedTraceId, problemDetails.Extensions["traceId"].ToString());
749+
}
750+
751+
[Fact]
752+
public async Task WriteAsync_Respects_CustomNamingPolicy_ForValidationProblemDetails()
753+
{
754+
// Arrange
755+
var writer = GetWriter();
756+
var stream = new MemoryStream();
757+
var context = CreateContext(stream);
758+
var expectedTraceId = Activity.Current?.Id ?? context.TraceIdentifier;
759+
760+
var mockNamingPolicy = new Mock<JsonNamingPolicy>();
761+
mockNamingPolicy.Setup(policy => policy.ConvertName("traceId")).Returns("custom_traceId");
762+
763+
var serializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = mockNamingPolicy.Object };
764+
SetSerializerOptions(writer, serializerOptions);
765+
766+
var expectedProblem = new ValidationProblemDetails()
767+
{
768+
Errors = new Dictionary<string, string[]> { { "sample", new[] { "error-message" } } }
769+
};
770+
771+
var problemDetailsContext = new ProblemDetailsContext()
772+
{
773+
HttpContext = context,
774+
ProblemDetails = expectedProblem
775+
};
776+
777+
// Act
778+
await writer.WriteAsync(problemDetailsContext);
779+
780+
// Assert
781+
stream.Position = 0;
782+
var problemDetails = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(stream, serializerOptions);
783+
Assert.NotNull(problemDetails);
784+
Assert.Equal(expectedTraceId, problemDetails.Extensions["custom_traceId"].ToString());
785+
Assert.DoesNotContain("traceId", problemDetails.Extensions.Keys);
786+
}
787+
788+
[Fact]
789+
public async Task WriteAsync_Uses_DefaultTraceIdKey_WhenNoNamingPolicy()
790+
{
791+
// Arrange
792+
var writer = GetWriter();
793+
var stream = new MemoryStream();
794+
var context = CreateContext(stream);
795+
var expectedTraceId = Activity.Current?.Id ?? context.TraceIdentifier;
796+
797+
var expectedProblem = new ProblemDetails()
798+
{
799+
Status = StatusCodes.Status500InternalServerError
800+
};
801+
802+
var problemDetailsContext = new ProblemDetailsContext()
803+
{
804+
HttpContext = context,
805+
ProblemDetails = expectedProblem
806+
};
807+
808+
// Act
809+
await writer.WriteAsync(problemDetailsContext);
810+
811+
// Assert
812+
stream.Position = 0;
813+
var problemDetails = await JsonSerializer.DeserializeAsync<ProblemDetails>(stream, SerializerOptions);
814+
Assert.NotNull(problemDetails);
815+
Assert.Equal(expectedTraceId, problemDetails.Extensions["traceId"].ToString());
816+
}
817+
673818
private static HttpContext CreateContext(
674819
Stream body,
675820
int statusCode = StatusCodes.Status400BadRequest,

0 commit comments

Comments
 (0)