This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ValidationProblemDetailsTest.cs
107 lines (95 loc) · 3.8 KB
/
ValidationProblemDetailsTest.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Xunit;
namespace Microsoft.AspNetCore.Mvc
{
public class ValidationProblemDetailsTest
{
[Fact]
public void Constructor_SetsTitle()
{
// Arrange & Act
var problemDescription = new ValidationProblemDetails();
// Assert
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
Assert.Empty(problemDescription.Errors);
}
[Fact]
public void Constructor_SerializesErrorsFromModelStateDictionary()
{
// Arrange
var modelStateDictionary = new ModelStateDictionary();
modelStateDictionary.AddModelError("key1", "error1");
modelStateDictionary.SetModelValue("key2", new object(), "value");
modelStateDictionary.AddModelError("key3", "error2");
modelStateDictionary.AddModelError("key3", "error3");
// Act
var problemDescription = new ValidationProblemDetails(modelStateDictionary);
// Assert
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
Assert.Collection(
problemDescription.Errors,
item =>
{
Assert.Equal("key1", item.Key);
Assert.Equal(new[] { "error1" }, item.Value);
},
item =>
{
Assert.Equal("key3", item.Key);
Assert.Equal(new[] { "error2", "error3" }, item.Value);
});
}
[Fact]
public void Constructor_SerializesErrorsFromModelStateDictionary_AddsDefaultMessage()
{
// Arrange
var modelStateDictionary = new ModelStateDictionary();
var provider = new EmptyModelMetadataProvider();
var metadata = provider.GetMetadataForProperty(typeof(string), nameof(string.Length));
modelStateDictionary.AddModelError("unsafeError",
new Exception("This message should not be returned to clients"),
metadata);
// Act
var problemDescription = new ValidationProblemDetails(modelStateDictionary);
// Assert
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
Assert.Collection(
problemDescription.Errors,
item =>
{
Assert.Equal("unsafeError", item.Key);
Assert.Equal(new[] { "The input was not valid." }, item.Value);
});
}
[Fact]
public void Constructor_CopiesPassedInDictionary()
{
// Arrange
var errors = new Dictionary<string, string[]>
{
["key1"] = new[] { "error1", "error2" },
["key2"] = new[] { "error3", },
};
// Act
var problemDescription = new ValidationProblemDetails(errors);
// Assert
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
Assert.Collection(
problemDescription.Errors,
item =>
{
Assert.Equal("key1", item.Key);
Assert.Equal(new[] { "error1", "error2" }, item.Value);
},
item =>
{
Assert.Equal("key2", item.Key);
Assert.Equal(new[] { "error3" }, item.Value);
});
}
}
}