Skip to content

Commit

Permalink
add the tests i forgot in the last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HauklandJ committed Oct 4, 2024
1 parent 3edcc98 commit 935b870
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using Altinn.App.Api.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;

namespace Altinn.App.Api.Controllers.Conventions;
Expand Down
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);
}
}
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 { }
}

0 comments on commit 935b870

Please sign in to comment.