forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelBindingEndToEndTests.cs
More file actions
297 lines (260 loc) · 11.1 KB
/
Copy pathModelBindingEndToEndTests.cs
File metadata and controls
297 lines (260 loc) · 11.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// 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.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Newtonsoft.Json;
namespace System.Web.Http.ModelBinding
{
/// <summary>
/// End-to-end tests for model binding.
/// </summary>
public class ModelBindingEndToEndTests
{
[Fact]
public async Task BindModel_BindsValuesFromUrl()
{
// Arrange
string value = "some-value";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
"http://localhost/ModelBinding/Url?somekey=" + value);
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
Assert.Equal(value, await ReadAsJson<string>(response));
}
[Fact]
public async Task BindModel_BindsSimpleTypesFromBody()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "Name", "PersonName" },
{ "Sibling.Name", "SiblingName" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/ModelBinding/SimpleType")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
Person result = await ReadAsJson<Person>(response);
Assert.Equal("PersonName", result.Name);
Assert.Equal("SiblingName", result.Sibling.Name);
}
[Fact]
public async Task BindModel_WithCollection()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "AddressLines[0].Line", "Street Address 0" },
{ "AddressLines[1].Line", "Street Address 1" },
{ "ZipCode", "98052" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put,
"http://localhost/ModelBinding/CollectionType")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
Address address = await ReadAsJson<Address>(response);
Assert.Equal(2, address.AddressLines.Count);
Assert.Equal("Street Address 0", address.AddressLines[0].Line);
Assert.Equal("Street Address 1", address.AddressLines[1].Line);
Assert.Equal("98052", address.ZipCode);
}
[Fact]
public async Task BindModel_WithCollection_SpecifyingIndex()
{
// Arrange
IEnumerable<KeyValuePair<string, string>> bodyParameters = new[]
{
new KeyValuePair<string, string>("AddressLines.index", "3"),
new KeyValuePair<string, string>("AddressLines.index", "10000"),
new KeyValuePair<string, string>("AddressLines[3].Line", "Street Address 0"),
new KeyValuePair<string, string>("AddressLines[10000].Line", "Street Address 1"),
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put,
"http://localhost/ModelBinding/CollectionType")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
Address address = await ReadAsJson<Address>(response);
Assert.Equal(2, address.AddressLines.Count);
Assert.Equal("Street Address 0", address.AddressLines[0].Line);
Assert.Equal("Street Address 1", address.AddressLines[1].Line);
}
[Fact]
public async Task BindModel_WithNestedCollection()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "Addresses[0].AddressLines[0].Line", "Street Address 00" },
{ "Addresses[0].AddressLines[1].Line", "Street Address 01" },
{ "Addresses[0].ZipCode", "98052" },
{ "Addresses[1].AddressLines[0].Line", "Street Address 10" },
{ "Addresses[1].AddressLines[3].Line", "Street Address 13" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/ModelBinding/NestedCollection")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
UserWithAddress result = await ReadAsJson<UserWithAddress>(response);
Assert.Equal(2, result.Addresses.Count);
Address address = result.Addresses[0];
Assert.Equal(2, address.AddressLines.Count);
Assert.Equal("Street Address 00", address.AddressLines[0].Line);
Assert.Equal("Street Address 01", address.AddressLines[1].Line);
Assert.Equal("98052", address.ZipCode);
address = result.Addresses[1];
StreetAddress streetAddress= streetAddress = Assert.Single(address.AddressLines);
Assert.Equal("Street Address 10", streetAddress.Line);
Assert.Null(address.ZipCode);
}
[Fact]
public async Task BindModel_WithIncorrectlyFormattedNestedCollectionValue()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "Addresses", "Street Address 00" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/ModelBinding/NestedCollection")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
UserWithAddress result = await ReadAsJson<UserWithAddress>(response);
Address address = Assert.Single(result.Addresses);
Assert.Null(address.AddressLines);
Assert.Null(address.ZipCode);
}
[Fact]
public async Task BindModel_WithNestedCollectionContainingARecursiveRelation()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "People[0].Name", "Person 0" },
{ "People[0].Sibling.Name", "Person 0 Sibling" },
{ "People[1].Sibling.Name", "Person 1 Sibling" },
{ "People[2].Sibling", "Person 2 Sibling" },
{ "People[1000].Name", "Person 1000 Sibling" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/ModelBinding/NestedCollectionOfRecursiveTypes")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
PeopleModel result = await ReadAsJson<PeopleModel>(response);
Assert.Equal(3, result.People.Count);
Person person = result.People[0];
Assert.Equal("Person 0", person.Name);
Assert.Equal("Person 0 Sibling", person.Sibling.Name);
Assert.Null(person.Sibling.Sibling);
person = result.People[1];
Assert.Equal("Person 1 Sibling", person.Sibling.Name);
Assert.Null(person.Sibling.Sibling);
person = result.People[2];
Assert.Null(person.Name);
Assert.NotNull(person.Sibling);
Assert.Null(person.Sibling.Name);
}
[Fact]
public async Task BindModel_WithNestedCollectionContainingRecursiveRelation_WithMalformedValue()
{
// Arrange
Dictionary<string, string> bodyParameters = new Dictionary<string, string>
{
{ "People", "Person 0" },
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/ModelBinding/NestedCollectionOfRecursiveTypes")
{
Content = new FormUrlEncodedContent(bodyParameters)
};
// Act
HttpResponseMessage response = await SubmitRequestAsync(request);
// Assert
PeopleModel result = await ReadAsJson<PeopleModel>(response);
Person person = Assert.Single(result.People);
Assert.Null(person.Name);
Assert.Null(person.Sibling);
}
private static async Task<HttpResponseMessage> SubmitRequestAsync(HttpRequestMessage request)
{
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
HttpServer server = new HttpServer(config);
using (HttpMessageInvoker client = new HttpMessageInvoker(server))
{
HttpResponseMessage response = await client.SendAsync(request, CancellationToken.None);
response.EnsureSuccessStatusCode();
return response;
}
}
private static async Task<TVal> ReadAsJson<TVal>(HttpResponseMessage response)
{
Assert.Equal(MediaTypeHeaderValue.Parse("application/json; charset=utf-8"),
response.Content.Headers.ContentType);
string content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TVal>(content);
}
}
[RoutePrefix("ModelBinding")]
public class ModelBindingController : ApiController
{
[HttpGet]
[Route("Url")]
public string UrlBinding([FromUri] string someKey)
{
return someKey;
}
[HttpPost]
[Route("SimpleType")]
public Person SimpleType([FromBody] Person model)
{
return model;
}
[HttpPut]
[Route("CollectionType")]
public Address CollectionType([FromBody] Address address)
{
return address;
}
[HttpPost]
[Route("NestedCollection")]
public UserWithAddress NestedCollectionType([FromBody] UserWithAddress user)
{
return user;
}
[HttpPost]
[Route("NestedCollectionOfRecursiveTypes")]
public PeopleModel NestedCollectionType([FromBody] PeopleModel model)
{
return model;
}
}
}