Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProblemDetailsJsonConverterTest.cs « test « Http.Extensions « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 76b0c99b85dc5e458e17b4150d6e895511023c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace Microsoft.AspNetCore.Http.Extensions;

public class ProblemDetailsJsonConverterTest
{
    private static JsonSerializerOptions JsonSerializerOptions => new JsonOptions().SerializerOptions;

    [Fact]
    public void Read_ThrowsIfJsonIsIncomplete()
    {
        // Arrange
        var json = "{";
        var converter = new ProblemDetailsJsonConverter();

        // Act & Assert
        var ex = Record.Exception(() =>
        {
            var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
            converter.Read(ref reader, typeof(ProblemDetails), JsonSerializerOptions);
        });
        Assert.IsAssignableFrom<JsonException>(ex);
    }

    [Fact]
    public void Read_Works()
    {
        // Arrange
        var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4";
        var title = "Not found";
        var status = 404;
        var detail = "Product not found";
        var instance = "http://example.com/products/14";
        var traceId = "|37dd3dd5-4a9619f953c40a16.";
        var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"detail\":\"{detail}\", \"instance\":\"{instance}\",\"traceId\":\"{traceId}\"}}";
        var converter = new ProblemDetailsJsonConverter();
        var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
        reader.Read();

        // Act
        var problemDetails = converter.Read(ref reader, typeof(ProblemDetails), JsonSerializerOptions);

        Assert.Equal(type, problemDetails.Type);
        Assert.Equal(title, problemDetails.Title);
        Assert.Equal(status, problemDetails.Status);
        Assert.Equal(instance, problemDetails.Instance);
        Assert.Equal(detail, problemDetails.Detail);
        Assert.Collection(
            problemDetails.Extensions,
            kvp =>
            {
                Assert.Equal("traceId", kvp.Key);
                Assert.Equal(traceId, kvp.Value.ToString());
            });
    }

    [Fact]
    public void Read_UsingJsonSerializerWorks()
    {
        // Arrange
        var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4";
        var title = "Not found";
        var status = 404;
        var detail = "Product not found";
        var instance = "http://example.com/products/14";
        var traceId = "|37dd3dd5-4a9619f953c40a16.";
        var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"detail\":\"{detail}\", \"instance\":\"{instance}\",\"traceId\":\"{traceId}\"}}";

        // Act
        var problemDetails = JsonSerializer.Deserialize<ProblemDetails>(json, JsonSerializerOptions);

        Assert.Equal(type, problemDetails.Type);
        Assert.Equal(title, problemDetails.Title);
        Assert.Equal(status, problemDetails.Status);
        Assert.Equal(instance, problemDetails.Instance);
        Assert.Equal(detail, problemDetails.Detail);
        Assert.Collection(
            problemDetails.Extensions,
            kvp =>
            {
                Assert.Equal("traceId", kvp.Key);
                Assert.Equal(traceId, kvp.Value.ToString());
            });
    }

    [Fact]
    public void Read_WithSomeMissingValues_Works()
    {
        // Arrange
        var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4";
        var title = "Not found";
        var status = 404;
        var traceId = "|37dd3dd5-4a9619f953c40a16.";
        var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"traceId\":\"{traceId}\"}}";
        var converter = new ProblemDetailsJsonConverter();
        var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
        reader.Read();

        // Act
        var problemDetails = converter.Read(ref reader, typeof(ProblemDetails), JsonSerializerOptions);

        Assert.Equal(type, problemDetails.Type);
        Assert.Equal(title, problemDetails.Title);
        Assert.Equal(status, problemDetails.Status);
        Assert.Collection(
            problemDetails.Extensions,
            kvp =>
            {
                Assert.Equal("traceId", kvp.Key);
                Assert.Equal(traceId, kvp.Value.ToString());
            });
    }

    [Fact]
    public void Write_Works()
    {
        // Arrange
        var traceId = "|37dd3dd5-4a9619f953c40a16.";
        var value = new ProblemDetails
        {
            Title = "Not found",
            Type = "https://tools.ietf.org/html/rfc7231#section-6.5.4",
            Status = 404,
            Detail = "Product not found",
            Instance = "http://example.com/products/14",
            Extensions =
                {
                    { "traceId", traceId },
                    { "some-data", new[] { "value1", "value2" } }
                }
        };
        var expected = $"{{\"type\":\"{JsonEncodedText.Encode(value.Type)}\",\"title\":\"{value.Title}\",\"status\":{value.Status},\"detail\":\"{value.Detail}\",\"instance\":\"{JsonEncodedText.Encode(value.Instance)}\",\"traceId\":\"{traceId}\",\"some-data\":[\"value1\",\"value2\"]}}";
        var converter = new ProblemDetailsJsonConverter();
        var stream = new MemoryStream();

        // Act
        using (var writer = new Utf8JsonWriter(stream))
        {
            converter.Write(writer, value, JsonSerializerOptions);
        }

        // Assert
        var actual = Encoding.UTF8.GetString(stream.ToArray());
        Assert.Equal(expected, actual);
    }

    [Fact]
    public void Write_WithSomeMissingContent_Works()
    {
        // Arrange
        var value = new ProblemDetails
        {
            Title = "Not found",
            Type = "https://tools.ietf.org/html/rfc7231#section-6.5.4",
            Status = 404,
        };
        var expected = $"{{\"type\":\"{JsonEncodedText.Encode(value.Type)}\",\"title\":\"{value.Title}\",\"status\":{value.Status}}}";
        var converter = new ProblemDetailsJsonConverter();
        var stream = new MemoryStream();

        // Act
        using (var writer = new Utf8JsonWriter(stream))
        {
            converter.Write(writer, value, JsonSerializerOptions);
        }

        // Assert
        var actual = Encoding.UTF8.GetString(stream.ToArray());
        Assert.Equal(expected, actual);
    }
}