-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
NATS.Jwt.Tests/Models/NatsJsonDateTimeOffsetConverterTests.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,59 @@ | ||
// Copyright (c) The NATS Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using NATS.Jwt.Internal; | ||
using Xunit; | ||
|
||
namespace NATS.Jwt.Tests.Models; | ||
|
||
public class NatsJsonDateTimeOffsetConverterTests | ||
{ | ||
[Fact] | ||
public void Read_UnsupportedType_ShouldThrowException() | ||
{ | ||
// Act & Assert | ||
var ex = Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
// Arrange | ||
var converter = new TestConverter(); | ||
var reader = new Utf8JsonReader(JsonSerializer.SerializeToUtf8Bytes(123)); | ||
reader.Read(); // Move to the first token | ||
converter.Read(ref reader, typeof(DateTime), new JsonSerializerOptions()); | ||
}); | ||
Assert.Contains("Reading unknown date type", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void Write_ValidValue_ShouldWriteCorrectly() | ||
{ | ||
// Arrange | ||
var converter = new TestConverter(); | ||
MemoryStream memoryStream = new(); | ||
var writer = new Utf8JsonWriter(memoryStream); | ||
var value = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero); | ||
|
||
// Act | ||
converter.Write(writer, value, new JsonSerializerOptions()); | ||
writer.Flush(); | ||
|
||
// Assert | ||
var json = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
Assert.Equal("1609459200", json); | ||
} | ||
|
||
private class TestConverter : NatsJsonDateTimeOffsetConverter | ||
{ | ||
public new void Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
base.Read(ref reader, typeToConvert, options); | ||
} | ||
|
||
public new void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options) | ||
{ | ||
base.Write(writer, value, options); | ||
} | ||
} | ||
} |