forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryStringBindingTests.cs
More file actions
146 lines (129 loc) · 6.51 KB
/
Copy pathQueryStringBindingTests.cs
File metadata and controls
146 lines (129 loc) · 6.51 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
// 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.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.TestCommon;
namespace System.Web.Http.ModelBinding
{
/// <summary>
/// End to end functional tests for model binding via query strings
/// </summary>
public class QueryStringBindingTests : ModelBindingTests
{
[Theory]
[InlineData("GetString", "?value=test", "\"test\"")]
[InlineData("GetInt", "?value=99", "99")]
[InlineData("GetBool", "?value=false", "false")]
[InlineData("GetBool", "?value=true", "true")]
[InlineData("GetIntWithDefault", "?value=99", "99")] // action has default, but we provide value
[InlineData("GetIntWithDefault", "", "-1")] // action has default, we provide no value
[InlineData("GetStringWithDefault", "", "null")] // action has null default, we provide no value
[InlineData("GetIntFromUri", "?value=99", "99")] // [FromUri]
[InlineData("GetIntPrefixed", "?somePrefix=99", "99")] // [FromUri(Prefix=somePrefix)]
[InlineData("GetIntAsync", "?value=5", "5")]
[InlineData("GetOptionalNullableInt", "", "null")]
[InlineData("GetOptionalNullableInt", "?value=6", "6")]
public async Task Query_String_Binds_Simple_Types_Get(string action, string queryString, string expectedResponse)
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}{1}", action, queryString)),
Method = HttpMethod.Get
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
string responseString = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResponse, responseString);
}
[Theory]
[InlineData("PostString", "?value=test", "\"test\"")]
[InlineData("PostInt", "?value=99", "99")]
[InlineData("PostBool", "?value=false", "false")]
[InlineData("PostBool", "?value=true", "true")]
[InlineData("PostIntFromUri", "?value=99", "99")] // [FromUri]
[InlineData("PostIntUriPrefixed", "?somePrefix=99", "99")] // [FromUri(Prefix=somePrefix)]
[InlineData("PostIntArray", "?value={[1,2,3]}", "0")] // TODO: DevDiv2 333257 -- make this array real when fix JsonValue array model binding
public async Task Query_String_Binds_Simple_Types_Post(string action, string queryString, string expectedResponse)
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}{1}", action, queryString)),
Method = HttpMethod.Post
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
string responseString = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResponse, responseString);
}
[Theory]
[InlineData("GetComplexTypeFromUri", "itemName=Tires&quantity=2&customer.Name=Sue", "Tires", 2, "Sue")]
public async Task Query_String_ComplexType_Type_Get(string action, string queryString, string itemName, int quantity, string customerName)
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}?{1}", action, queryString)),
Method = HttpMethod.Get
};
ModelBindOrder expectedItem = new ModelBindOrder()
{
ItemName = itemName,
Quantity = quantity,
Customer = new ModelBindCustomer { Name = customerName }
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
ModelBindOrder actualItem = await response.Content.ReadAsAsync<ModelBindOrder>();
Assert.Equal(expectedItem, actualItem, new ModelBindOrderEqualityComparer());
}
[Theory]
[InlineData("PostComplexTypeFromUri", "itemName=Tires&quantity=2&customer.Name=Bob", "Tires", 2, "Bob")]
public async Task Query_String_ComplexType_Type_Post(string action, string queryString, string itemName, int quantity, string customerName)
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}?{1}", action, queryString)),
Method = HttpMethod.Post
};
ModelBindOrder expectedItem = new ModelBindOrder()
{
ItemName = itemName,
Quantity = quantity,
Customer = new ModelBindCustomer { Name = customerName }
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
ModelBindOrder actualItem = await response.Content.ReadAsAsync<ModelBindOrder>();
Assert.Equal(expectedItem, actualItem, new ModelBindOrderEqualityComparer());
}
[Theory]
[InlineData("PostComplexTypeFromUriWithNestedCollection", "value.Numbers[0]=1&value.Numbers[1]=2", new[] { 1, 2 })]
public async Task Query_String_ComplexType_Type_Post_NestedCollection(string action, string queryString, int[] expectedValues)
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(BaseAddress + String.Format("ModelBinding/{0}?{1}", action, queryString)),
Method = HttpMethod.Post
};
// Act
HttpResponseMessage response = await Client.SendAsync(request);
// Assert
ComplexTypeWithNestedCollection actualResult = await response.Content.ReadAsAsync<ComplexTypeWithNestedCollection>();
int[] actualValues = actualResult.Numbers.ToArray();
Assert.Equal(expectedValues.Length, actualValues.Length);
for (int i = 0; i < expectedValues.Length; i++)
{
Assert.Equal(expectedValues[i], actualValues[i]);
}
}
}
}