Files
csharp/tests/KubernetesClient.Tests/SerializationTests.cs
Luis Cantero 2af57cade8 Add AddJsonOptions to KubernetesClientConfiguration and KubernetesJson (#1257)
* Use camelCase policy when serializing enums

* Revert "Use camelCase policy when serializing enums"

This reverts commit 467f49d8734bcbd6aabb87447fbd7d21840c4c48.

* Add jonSerializerOptions to GenericClient

* Add jsonSerializerOptions param

* Revert pass deserialization options in GenericClient

* Add JsonSerializerOptions to KubernetesClientConfiguration

* Use user JsonSerializerOptions in SendRequest and CreateResultAsync

* Improve comment

* Remove JsonSerializerOptions

* Cosmetic

* Add AddJsonOptions

* Fix example

* Fix test

* Add test

* Add summary

* Improve summary

* Remove configure from Kubernetes ctor and tests

* Add AddJsonOptions to config and test

* Support per client json serializer options

* Add ConfigureAwait for tests

* Check for nullargument
2023-04-04 13:35:39 -07:00

80 lines
3.0 KiB
C#

using k8s.Tests.Mock;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace k8s.Tests
{
public class SerializationTests
{
private readonly ITestOutputHelper testOutput;
private enum Animals
{
Dog,
Cat,
Mouse,
}
public SerializationTests(ITestOutputHelper testOutput)
{
this.testOutput = testOutput;
}
[Fact]
public async Task SerializeEnumUsingPascalCase()
{
using var server = new MockKubeApiServer(testOutput);
var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() };
config.AddJsonOptions(options =>
{
// Insert the converter at the front of the list so it overrides any others.
options.Converters.Insert(index: 0, new JsonStringEnumConverter());
});
var client = new Kubernetes(config);
var customObject = Animals.Dog;
var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(false);
var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
// Assert that the client serializes using the default options.
Assert.Equal(@"""Dog""", content);
// Assert that the underlying KubernetesJson serializes using the default options.
string animal = KubernetesJson.Serialize(Animals.Cat);
Assert.Equal(@"""Cat""", animal);
}
[Fact]
public async Task SerializeEnumUsingCamelCase()
{
using var server = new MockKubeApiServer(testOutput);
var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() };
config.AddJsonOptions(options =>
{
// Insert the converter at the front of the list so it overrides
// the default JsonStringEnumConverter without namingPolicy.
options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
var client = new Kubernetes(config);
var customObject = Animals.Dog;
var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(false);
var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
// Assert that the client serializes using the specified options.
Assert.Equal(@"""dog""", content);
// Assert that the underlying KubernetesJson serializes using the default options.
string animal = KubernetesJson.Serialize(Animals.Cat);
Assert.Equal(@"""Cat""", animal);
}
}
}