Skip to content

Commit

Permalink
feat(bus): address duplication issue
Browse files Browse the repository at this point in the history
closes #172
  • Loading branch information
ostridm committed Jun 12, 2024
1 parent aa28055 commit 2450016
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/SecTester.Repeater/Internal/HttpMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SecTester.Repeater.Internal;

public class HttpMethods
public static class HttpMethods
{
public static IDictionary<string, HttpMethod> Items { get; } = typeof(HttpMethod)
.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace SecTester.Repeater.Internal;

internal class JsonHttpMethodEnumerationStringConverter : JsonConverter<HttpMethod>
{
private static readonly IEnumerable<HttpMethod> BaseMethods = typeof(HttpMethod)
.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(x => x.PropertyType.IsAssignableFrom(typeof(HttpMethod)))
.Select(x => x.GetValue(null))
.Cast<HttpMethod>();

private static readonly IEnumerable<HttpMethod> CustomMethods = new List<HttpMethod>
{
new("PATCH"),
new("COPY"),
new("LINK"),
new("UNLINK"),
new("PURGE"),
new("LOCK"),
new("UNLOCK"),
new("PROPFIND"),
new("VIEW")
};

private static readonly IDictionary<string, HttpMethod> Methods = BaseMethods.Concat(CustomMethods).Distinct()
.ToDictionary(x => x.Method, x => x, StringComparer.InvariantCultureIgnoreCase);


public override HttpMethod? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.String:
var token = reader.GetString();
if (token is null || !Methods.TryGetValue(token, out var method))
if (token is null || !HttpMethods.Items.TryGetValue(token, out var method))
{
throw new JsonException(
$"Unexpected value {token} when parsing the {nameof(HttpMethod)}.");
Expand All @@ -56,7 +30,7 @@ internal class JsonHttpMethodEnumerationStringConverter : JsonConverter<HttpMeth

public override void Write(Utf8JsonWriter writer, HttpMethod value, JsonSerializerOptions options)
{
if (!Methods.TryGetValue(value.Method, out var method))
if (!HttpMethods.Items.TryGetValue(value.Method, out var method))
{
throw new JsonException(
$"Unexpected value {value.Method} when writing the {nameof(HttpMethod)}.");
Expand Down

0 comments on commit 2450016

Please sign in to comment.