< Summary

Information
Class: Elsa.Http.Serialization.HttpHeadersConverter
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Serialization/HttpHeadersConverter.cs
Line coverage
32%
Covered lines: 9
Uncovered lines: 19
Coverable lines: 28
Total lines: 69
Line coverage: 32.1%
Branch coverage
20%
Covered branches: 4
Total branches: 20
Branch coverage: 20%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)18.75%1241625%
Write(...)25%6450%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Serialization/HttpHeadersConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Elsa.Http.Serialization;
 5
 6/// <summary>
 7/// A custom JSON converter for HttpHeaders that supports both single and multiple values.
 8/// </summary>
 9public class HttpHeadersConverter : JsonConverter<HttpHeaders>
 10{
 11    /// <inheritdoc />
 12    public override HttpHeaders Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 13    {
 114        if (reader.TokenType != JsonTokenType.StartObject)
 015            throw new JsonException("Expected StartObject token");
 16
 117        var headers = new HttpHeaders();
 18
 119        while (reader.Read())
 20        {
 121            if (reader.TokenType == JsonTokenType.EndObject)
 122                return headers;
 23
 024            if (reader.TokenType != JsonTokenType.PropertyName)
 025                throw new JsonException("Expected a PropertyName token");
 26
 027            var key = reader.GetString()!;
 028            reader.Read();
 29
 30            // If the next token is not a StartArray token, then we expect a String token.
 031            switch (reader.TokenType)
 32            {
 33                case JsonTokenType.StartArray:
 34                {
 035                    var values = new List<string>();
 036                    while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) values.Add(reader.GetString()!);
 037                    headers.Add(key, values.ToArray());
 038                    break;
 39                }
 40                case JsonTokenType.String:
 41                {
 042                    var singleValue = reader.GetString()!;
 043                    headers.Add(key, new[] { singleValue });
 044                    break;
 45                }
 46                default:
 047                    throw new JsonException("Expected a String or StartArray token");
 48            }
 49        }
 50
 051        throw new JsonException("Expected an EndObject token");
 52    }
 53
 54    /// <inheritdoc />
 55    public override void Write(Utf8JsonWriter writer, HttpHeaders value, JsonSerializerOptions options)
 56    {
 1257        writer.WriteStartObject();
 58
 2459        foreach (var header in value)
 60        {
 061            writer.WritePropertyName(header.Key);
 062            writer.WriteStartArray();
 063            foreach (var headerValue in header.Value) writer.WriteStringValue(headerValue);
 064            writer.WriteEndArray();
 65        }
 66
 1267        writer.WriteEndObject();
 1268    }
 69}