-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the tests i forgot in the last commit
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
src/Altinn.App.Api/Controllers/Conventions/AltinnApiJsonFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnApiJsonFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Altinn.App.Api.Controllers.Attributes; | ||
using Altinn.App.Api.Controllers.Conventions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
|
||
namespace Altinn.App.Api.Tests.Controllers.Conventions; | ||
|
||
public class AltinnApiJsonFormatterTests | ||
{ | ||
[Fact] | ||
public void CreateFormatter_WhenEncoderIsNull_SetsUnsafeRelaxedJsonEscaping() | ||
{ | ||
// Arrange | ||
string settingsName = "AltinnApi"; | ||
var serializerOptions = new JsonSerializerOptions | ||
{ | ||
Encoder = null, | ||
TypeInfoResolver = new DefaultJsonTypeInfoResolver() | ||
}; | ||
|
||
// Act | ||
var formatter = AltinnApiJsonFormatter.CreateFormatter(settingsName, serializerOptions); | ||
|
||
// Assert | ||
Assert.NotNull(formatter); | ||
Assert.Equal(settingsName, formatter.SettingsName); | ||
Assert.NotNull(formatter.SerializerOptions.Encoder); | ||
Assert.Equal(JavaScriptEncoder.UnsafeRelaxedJsonEscaping, formatter.SerializerOptions.Encoder); | ||
} | ||
|
||
[Fact] | ||
public void CreateFormatter_WhenEncoderIsNotNull_PreservesEncoder() | ||
{ | ||
// Arrange | ||
string settingsName = "AltinnApi"; | ||
var originalEncoder = JavaScriptEncoder.Default; | ||
var serializerOptions = new JsonSerializerOptions | ||
{ | ||
Encoder = originalEncoder, | ||
TypeInfoResolver = new DefaultJsonTypeInfoResolver() | ||
}; | ||
|
||
// Act | ||
var formatter = AltinnApiJsonFormatter.CreateFormatter(settingsName, serializerOptions); | ||
|
||
// Assert | ||
Assert.NotNull(formatter); | ||
Assert.Equal(settingsName, formatter.SettingsName); | ||
Assert.Equal(originalEncoder, formatter.SerializerOptions.Encoder); | ||
} | ||
|
||
[Fact] | ||
public void CanWriteResult_SettingsNameMatches_ReturnsTrue() | ||
{ | ||
// Arrange | ||
string settingsName = JsonSettingNames.AltinnApi; | ||
var formatter = AltinnApiJsonFormatter.CreateFormatter( | ||
settingsName, | ||
new JsonSerializerOptions() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() } | ||
); | ||
|
||
var httpContext = new DefaultHttpContext(); | ||
|
||
// Create an Endpoint with JsonSettingsNameAttribute | ||
var endpoint = new Endpoint( | ||
requestDelegate: null, | ||
metadata: new EndpointMetadataCollection(new JsonSettingsNameAttribute(settingsName)), | ||
displayName: null | ||
); | ||
|
||
httpContext.SetEndpoint(endpoint); | ||
|
||
var context = new OutputFormatterWriteContext( | ||
httpContext, | ||
(stream, encoding) => new StreamWriter(stream, encoding), | ||
typeof(object), | ||
new object() | ||
); | ||
|
||
// Act | ||
bool canWrite = formatter.CanWriteResult(context); | ||
|
||
// Assert | ||
Assert.True(canWrite); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnControllerConventionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Reflection; | ||
using Altinn.App.Api.Controllers.Attributes; | ||
using Altinn.App.Api.Controllers.Conventions; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
|
||
namespace Altinn.App.Api.Tests.Controllers.Conventions; | ||
|
||
public class AltinnControllerConventionsTests | ||
{ | ||
[Fact] | ||
public void Apply_AddsJsonSettingsNameAttributeToControllerModel() | ||
{ | ||
// Arrange | ||
var convention = new AltinnControllerConventions(); | ||
var controllerType = typeof(TestController).GetTypeInfo(); | ||
var controllerModel = new ControllerModel(controllerType, []); | ||
|
||
// Act | ||
convention.Apply(controllerModel); | ||
|
||
// Assert | ||
var attribute = controllerModel.Filters.OfType<JsonSettingsNameAttribute>().FirstOrDefault(); | ||
|
||
Assert.NotNull(attribute); | ||
Assert.Equal(JsonSettingNames.AltinnApi, attribute.Name); | ||
} | ||
|
||
// Dummy controller | ||
private class TestController : ControllerBase { } | ||
} |