Skip to content

Commit

Permalink
add testing, add api to public namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
westin-m committed Jul 12, 2024
1 parent c40dfec commit 7a7ce1f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
using Microsoft.IdentityModel.Abstractions;
Expand Down Expand Up @@ -93,6 +94,27 @@ public static string Write(OpenIdConnectConfiguration configuration)
return OpenIdConnectConfigurationSerializer.Write(configuration);
}

/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> to a json string using the provided <see cref="Stream"/>.
/// </summary>
/// <param name="configuration">The <see cref="OpenIdConnectConfiguration"/> to serialize.</param>
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <remarks>Because a <see cref="Stream"/> is provided, this method does not return a value.</remarks>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> or <paramref name="stream"/> is null.</exception>
public static void Write(OpenIdConnectConfiguration configuration, Stream stream)
{
if (configuration == null)
throw LogHelper.LogArgumentNullException(nameof(configuration));

if (stream == null)
throw LogHelper.LogArgumentNullException(nameof(stream));

if (LogHelper.IsEnabled(EventLogLevel.Verbose))
LogHelper.LogVerbose(LogMessages.IDX21809);

OpenIdConnectConfigurationSerializer.Write(configuration, stream);
}

/// <summary>
/// Initializes an new instance of <see cref="OpenIdConnectConfiguration"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,6 @@ public static OpenIdConnectConfiguration Read(ref Utf8JsonReader reader, OpenIdC
#endregion

#region Write
/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> to a UTF8-encoded JSON string.
/// </summary>
/// <param name="OpenIdConnectConfiguration">The configuration to be written.</param>
/// <returns>A UTF8-encoded JSON string.</returns>
public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration)
{
using (MemoryStream memoryStream = new MemoryStream())
Expand All @@ -622,20 +617,13 @@ public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration
}
}

/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> to a UTF8-encoded JSON string using the provided <see cref="Stream"/>.
/// </summary>
/// <param name="OpenIdConnectConfiguration">The <see cref="OpenIdConnectConfiguration"/> to be written.</param>
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <remarks>Because a <see cref="Stream"/> is provided, this method does not return a value.</remarks>
public static void Write(OpenIdConnectConfiguration OpenIdConnectConfiguration, Stream stream)
{
Utf8JsonWriter writer = null;
try
{
writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Write(ref writer, OpenIdConnectConfiguration);
writer.Flush();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -289,6 +292,22 @@ public void EmptyCollectionSerialization()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void EmptyCollectionSerializationWithStream()
{
using (MemoryStream stream = new MemoryStream())
{
var context = new CompareContext {Title = "EmptyCollectionSerialization"};
// Initialize an OpenIdConnectConfiguration object with all collections empty.
var oidcWithEmptyCollections = new OpenIdConnectConfiguration();
OpenIdConnectConfiguration.Write(oidcWithEmptyCollections, stream);
var emptyCollectionBytes = Encoding.UTF8.GetBytes("{}");
IdentityComparer.AreEqual(stream.ToArray(), emptyCollectionBytes, context);

TestUtilities.AssertFailIfErrors(context);
}
}

[Fact]
public void NonemptyCollectionSerialization()
{
Expand Down Expand Up @@ -345,5 +364,25 @@ public void NonemptyCollectionSerialization()
}
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void NonemptyCollectionSerializationWithStream()
{
using (MemoryStream stream = new MemoryStream())
{

var context = new CompareContext { Title = "NonemptyCollectionSerialization" };
// Initialize an OpenIdConnectConfiguration object that has at least one element in each Collection.
var oidcWithAllCollections = OpenIdConnectConfiguration.Create(OpenIdConfigData.JsonAllValues);
var oidcWithAllCollectionsJson = OpenIdConnectConfiguration.Write(oidcWithAllCollections);
var oidcWithAllCollectionsBytes = Encoding.UTF8.GetBytes(oidcWithAllCollectionsJson);

OpenIdConnectConfiguration.Write(oidcWithAllCollections, stream);

IdentityComparer.AreBytesEqual(oidcWithAllCollectionsBytes, stream.GetBuffer(), context);

TestUtilities.AssertFailIfErrors(context);
}
}
}
}

0 comments on commit 7a7ce1f

Please sign in to comment.