forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBodyBindingTests.cs
More file actions
174 lines (152 loc) · 7.32 KB
/
Copy pathBodyBindingTests.cs
File metadata and controls
174 lines (152 loc) · 7.32 KB
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
// 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.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Properties;
using Microsoft.TestCommon;
namespace System.Web.Http.ModelBinding
{
/// <summary>
/// End to end functional tests for model binding via request body
/// </summary>
public class BodyBindingTests : ModelBindingTests
{
[Fact]
[ReplaceCulture]
public async Task Body_Bad_Input_Receives_Validation_Error()
{
// Arrange
string formUrlEncodedString = "Id=101&Name=testFirstNameTooLong";
StringContent stringContent = new StringContent(formUrlEncodedString, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + "ModelBinding/PostComplexWithValidation"),
Method = HttpMethod.Post,
Content = stringContent,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal("Failed to bind customer.RequiredValue. The errors are:\nErrorMessage: The RequiredValue property is required.\nFailed to bind customer.Name. The errors are:\nErrorMessage: The field Name must be a string with a maximum length of 6.\n",
await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task Body_Good_Input_Succeed()
{
// Arrange
string formUrlEncodedString = "Id=111&Name=John&RequiredValue=9";
StringContent stringContent = new StringContent(formUrlEncodedString, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + "ModelBinding/PostComplexWithValidation"),
Method = HttpMethod.Post,
Content = stringContent,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("111", await response.Content.ReadAsStringAsync());
}
[Theory]
[InlineData("PostWithOptionalBodyParameter")]
[InlineData("PostWithOptionalBodyParameterAndUriParameter?id=3")]
public async Task Body_OptionalParameter_Throws(string actionName)
{
// Arrange
StringContent stringContent = new StringContent(@"""string value""", Encoding.UTF8, "application/json");
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + "ModelBinding/" + actionName),
Method = HttpMethod.Post,
Content = stringContent,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
HttpError error = await response.Content.ReadAsAsync<HttpError>();
// Assert
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal(String.Format(SRResources.OptionalBodyParameterNotSupported, "value", typeof(FormatterParameterBinding).Name), error["ExceptionMessage"]);
}
[Theory]
[InlineData("application/json")]
[InlineData(null)]
public async Task Body_Binds_EmptyContentWithOrWithoutContentTypeHeader(string mediaType)
{
// Arrange
StringContent stringContent = new StringContent(String.Empty);
stringContent.Headers.ContentType = mediaType != null ? new MediaTypeHeaderValue(mediaType) : null;
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + "ModelBinding/PostComplexTypeFromBody"),
Method = HttpMethod.Post,
Content = stringContent,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
ModelBindOrder actualItem = await response.Content.ReadAsAsync<ModelBindOrder>();
Assert.Null(actualItem);
}
[Theory]
[InlineData("PostComplexType", "application/json")]
[InlineData("PostComplexType", "application/xml")]
[InlineData("PostComplexTypeFromBody", "application/json")]
[InlineData("PostComplexTypeFromBody", "application/xml")]
public async Task Body_Binds_ComplexType_Type_Key_Value_Read(string action, string mediaType)
{
// Arrange
ModelBindOrder expectedItem = new ModelBindOrder()
{
ItemName = "Bike",
Quantity = 1,
Customer = new ModelBindCustomer { Name = "Fred" }
};
var formatter = new MediaTypeFormatterCollection().FindWriter(typeof(ModelBindOrder), new MediaTypeHeaderValue(mediaType));
HttpRequestMessage request = new HttpRequestMessage
{
Content = new ObjectContent<ModelBindOrder>(expectedItem, formatter),
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}", action)),
Method = HttpMethod.Post,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
ModelBindOrder actualItem = await response.Content.ReadAsAsync<ModelBindOrder>();
Assert.Equal(expectedItem, actualItem, new ModelBindOrderEqualityComparer());
}
[Theory]
[InlineData("PostComplexType", "application/json")]
[InlineData("PostComplexType", "application/xml")]
[InlineData("PostComplexTypeFromBody", "application/json")]
[InlineData("PostComplexTypeFromBody", "application/xml")]
public async Task Body_Binds_ComplexType_Type_Whole_Body_Read(string action, string mediaType)
{
// Arrange
ModelBindOrder expectedItem = new ModelBindOrder()
{
ItemName = "Bike",
Quantity = 1,
Customer = new ModelBindCustomer { Name = "Fred" }
};
var formatter = new MediaTypeFormatterCollection().FindWriter(typeof(ModelBindOrder), new MediaTypeHeaderValue(mediaType));
HttpRequestMessage request = new HttpRequestMessage
{
Content = new ObjectContent<ModelBindOrder>(expectedItem, formatter),
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}", action)),
Method = HttpMethod.Post,
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
ModelBindOrder actualItem = await response.Content.ReadAsAsync<ModelBindOrder>();
Assert.Equal(expectedItem, actualItem, new ModelBindOrderEqualityComparer());
}
}
}