forked from googleapis/google-api-dotnet-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterUtilsTest.cs
More file actions
391 lines (335 loc) · 15.8 KB
/
ParameterUtilsTest.cs
File metadata and controls
391 lines (335 loc) · 15.8 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright 2016 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google;
using Google.Apis.Requests;
using Google.Apis.Requests.Parameters;
using Google.Apis.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
namespace Google.Apis.Tests.Apis.Requests.Parameters
{
/// <summary>Tests for <see cref="Google.Apis.Requests.Parameters.ParameterUtils"/>.</summary>
public class ParameterUtilsTest
{
private class TestRequestUrl
{
[Google.Apis.Util.RequestParameterAttribute("first_query_param",
Google.Apis.Util.RequestParameterType.Query)]
public string FirstParam { get; set; }
[Google.Apis.Util.RequestParameterAttribute("second_query_param",
Google.Apis.Util.RequestParameterType.Query)]
public string SecondParam { get; set; }
[Google.Apis.Util.RequestParameterAttribute("query_param_attribute_name",
Google.Apis.Util.RequestParameterType.UserDefinedQueries)]
public List<KeyValuePair<string, string>> ParamsCollection { get; set; }
public System.Uri Build()
{
var builder = new RequestBuilder()
{
BaseUri = new System.Uri("//revokeTokenUrl")
};
ParameterUtils.InitParameters(builder, this);
return builder.BuildUri();
}
}
[Fact]
public void IterateParametersTest()
{
var request = new TestRequestUrl()
{
FirstParam = "firstOne",
SecondParam = "secondOne",
ParamsCollection = new List<KeyValuePair<string, string>>{
new KeyValuePair<string,string>("customParam1","customVal1"),
new KeyValuePair<string,string>("customParam2","customVal2")
}
};
var result = request.Build().AbsoluteUri;
//Parametera were given a name and a value, so both appear in the URI.
Assert.Contains("first_query_param=firstOne", result);
Assert.Contains("second_query_param=secondOne", result);
//Custom parameters are key value pairs representing parameter names and values respectively.
Assert.Contains("customParam1=customVal1", result);
Assert.Contains("customParam2=customVal2", result);
//The parameter name for the custom parameters does not carry through to the resulting URI.
Assert.DoesNotContain("query_param_attribute_name", result);
}
private class RepeatableEnumRequest
{
[RequestParameter("mode", RequestParameterType.Query)]
public FileMode? Mode { get; set; }
[RequestParameter("mode", RequestParameterType.Query)]
public Repeatable<FileMode> ModeList { get; set; }
}
[Fact]
public void RepeatableEnum_NoPropertiesSet()
{
var request = new RepeatableEnumRequest();
var dictionary = ParameterUtils.CreateParameterDictionary(request);
// For historical reasons, we end up with a null value from the "FileMode? Mode" property.
// That's fixable, but not worth the worry about whether it would break things.
var pair = Assert.Single(dictionary);
Assert.Equal("mode", pair.Key);
Assert.Null(pair.Value);
}
[Fact]
public void RepeatableEnum_SinglePropertySet()
{
var request = new RepeatableEnumRequest { Mode = FileMode.Open };
var dictionary = ParameterUtils.CreateParameterDictionary(request);
Assert.Equal(FileMode.Open, dictionary["mode"]);
}
[Fact]
public void RepeatableEnum_ListPropertySet()
{
var request = new RepeatableEnumRequest { ModeList = new[] { FileMode.Open, FileMode.Append } };
var dictionary = ParameterUtils.CreateParameterDictionary(request);
var pair = Assert.Single(dictionary);
Assert.Equal("mode", pair.Key);
var repeatable = (Repeatable<FileMode>) pair.Value;
Assert.Equal(new[] { FileMode.Open, FileMode.Append }, repeatable.ToArray());
}
[Fact]
public void RepeatableEnum_BothPropertiesSet()
{
var request = new RepeatableEnumRequest
{
Mode = FileMode.Open,
ModeList = new[] { FileMode.Create, FileMode.Append }
};
Assert.Throws<InvalidOperationException>(() => ParameterUtils.CreateParameterDictionary(request));
}
// Tests for InitParametersWithExpansion
private class TestRequestWithScalars
{
[RequestParameter("name", RequestParameterType.Query)]
public string Name { get; set; }
[RequestParameter("id", RequestParameterType.Path)]
public int Id { get; set; }
[RequestParameter("nullparam", RequestParameterType.Query)]
public string NullParam { get; set; }
}
[Fact]
public void InitParametersWithExpansion_ScalarParameters()
{
var request = new TestRequestWithScalars
{
Name = "test",
Id = 123,
NullParam = null
};
var builder = new RequestBuilder
{
BaseUri = new Uri("https://example.com/api"),
Path = "items/{id}" // Path template for path parameter
};
ParameterUtils.InitParametersWithExpansion(builder, request);
// Verify parameters were added by building the URI
var uri = builder.BuildUri();
Assert.Contains("name=test", uri.AbsoluteUri);
Assert.Contains("items/123", uri.AbsoluteUri); // Path parameter replaces {id}
// Null parameter should not be in the URI
Assert.DoesNotContain("nullparam", uri.AbsoluteUri);
}
private class TestRequestWithRepeatable
{
[RequestParameter("part", RequestParameterType.Query)]
public Repeatable<string> Part { get; set; }
}
[Fact]
public void InitParametersWithExpansion_RepeatableParameters()
{
var request = new TestRequestWithRepeatable
{
Part = new[] { "snippet", "contentDetails" }
};
var builder = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
ParameterUtils.InitParametersWithExpansion(builder, request);
// Should expand to multiple parameters with the same name
var uri = builder.BuildUri();
Assert.Contains("part=snippet", uri.AbsoluteUri);
Assert.Contains("part=contentDetails", uri.AbsoluteUri);
}
private class TestRequestWithDateTime
{
[RequestParameter("time", RequestParameterType.Query)]
public DateTime? MinTime { get; set; }
}
[Fact]
public void InitParametersWithExpansion_DateTimeConversion()
{
var request = new TestRequestWithDateTime
{
MinTime = new DateTime(2023, 1, 15, 10, 30, 0, DateTimeKind.Utc)
};
var builder = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
ParameterUtils.InitParametersWithExpansion(builder, request);
// DateTime should be converted to RFC3339 format
var uri = builder.BuildUri();
Assert.Contains("time=", uri.AbsoluteUri);
Assert.Contains("2023-01-15", uri.AbsoluteUri);
}
private class TestRequestWithEnum
{
[RequestParameter("mode", RequestParameterType.Query)]
public Repeatable<FileMode> ModeList { get; set; }
}
[Fact]
public void InitParametersWithExpansion_EnumConversion()
{
var request = new TestRequestWithEnum
{
ModeList = new[] { FileMode.Open, FileMode.Append }
};
var builder = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
ParameterUtils.InitParametersWithExpansion(builder, request);
// Should expand enum list and convert each value using StringValue attribute
var uri = builder.BuildUri();
Assert.Contains("mode=Open", uri.AbsoluteUri);
Assert.Contains("mode=Append", uri.AbsoluteUri);
}
private class TestRequestWithBoolean
{
[RequestParameter("flag", RequestParameterType.Query)]
public bool Flag { get; set; }
}
[Fact]
public void InitParametersWithExpansion_BooleanConversion()
{
var request = new TestRequestWithBoolean { Flag = true };
var builder = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
ParameterUtils.InitParametersWithExpansion(builder, request);
// Boolean should be lowercase
var uri = builder.BuildUri();
Assert.Contains("flag=true", uri.AbsoluteUri);
}
[Fact]
public void InitParametersWithExpansion_CacheUsage()
{
var originalState = ApplicationContext.EnableReflectionCache;
try
{
// Arrange - explicitly enable cache
ApplicationContext.EnableReflectionCache = true;
var request1 = new TestRequestWithScalars { Name = "test1", Id = 1 };
var request2 = new TestRequestWithScalars { Name = "test2", Id = 2 };
var builder1 = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
var builder2 = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
// First call - cache is populated
ParameterUtils.InitParametersWithExpansion(builder1, request1);
var properties1 = Google.Apis.Util.ReflectionCache.GetRequestParameterProperties(typeof(TestRequestWithScalars));
// Second call - should reuse cached PropertyInfo
ParameterUtils.InitParametersWithExpansion(builder2, request2);
var properties2 = Google.Apis.Util.ReflectionCache.GetRequestParameterProperties(typeof(TestRequestWithScalars));
// Verify same PropertyInfo instances are returned (object reference equality)
Assert.Equal(properties1.Length, properties2.Length);
for (int i = 0; i < properties1.Length; i++)
{
Assert.Same(properties1[i].Property, properties2[i].Property);
Assert.Same(properties1[i].Attribute, properties2[i].Attribute);
}
}
finally
{
ApplicationContext.EnableReflectionCache = originalState;
}
}
/// <summary>
/// Regression test for null handling in enumerable expansion.
/// Tests that null elements in an enumerable produce the same result as Utilities.ConvertToString(null).
/// This test does not hard-code expectations - it dynamically tests against the actual behavior.
/// </summary>
[Fact]
public void InitParametersWithExpansion_NullElementsInEnumerable()
{
// Get the expected behavior from Utilities.ConvertToString(null) - don't hard-code!
string expectedNullValue = Utilities.ConvertToString(null);
var request = new TestRequestWithRepeatable
{
Part = new[] { "value1", null, "value2" }
};
var builder = new RequestBuilder { BaseUri = new Uri("https://example.com/api") };
ParameterUtils.InitParametersWithExpansion(builder, request);
// Build the URI and check the result
var uri = builder.BuildUri();
var query = uri.Query;
// Count how many "part=" occurrences there are
int partCount = System.Text.RegularExpressions.Regex.Matches(query, "part=").Count;
// Verify behavior matches what Utilities.ConvertToString(null) produces
if (expectedNullValue == null)
{
// If ConvertToString(null) returns null, the null element should be filtered out
// by RequestBuilder (which doesn't add null-valued parameters)
// So we should only see 2 parameters: "value1" and "value2"
Assert.Equal(2, partCount);
Assert.Contains("part=value1", query);
Assert.Contains("part=value2", query);
}
else if (expectedNullValue == "")
{
// If ConvertToString(null) returns empty string, we should see 3 parameters
// including one with an empty value
Assert.Equal(3, partCount);
Assert.Contains("part=value1", query);
Assert.Contains("part=value2", query);
// One of the "part=" should have empty value (part=&)
}
else
{
// If ConvertToString(null) returns some other string, we should see 3 parameters
// with that string value
Assert.Equal(3, partCount);
Assert.Contains("part=value1", query);
Assert.Contains("part=value2", query);
Assert.Contains($"part={Uri.EscapeDataString(expectedNullValue)}", query);
}
}
[Theory]
[InlineData(false)] // Cache disabled (default)
[InlineData(true)] // Cache enabled
public void IterateParameters_WorksWithBothCacheModes(bool enableCache)
{
// Arrange
var originalState = ApplicationContext.EnableReflectionCache;
try
{
ApplicationContext.EnableReflectionCache = enableCache;
var request = new TestRequestUrl()
{
FirstParam = "firstOne",
SecondParam = "secondOne",
ParamsCollection = new List<KeyValuePair<string, string>>{
new KeyValuePair<string,string>("customParam1","customVal1"),
new KeyValuePair<string,string>("customParam2","customVal2")
}
};
// Act
var result = request.Build().AbsoluteUri;
// Assert - behavior should be identical regardless of cache setting
Assert.Contains("first_query_param=firstOne", result);
Assert.Contains("second_query_param=secondOne", result);
Assert.Contains("customParam1=customVal1", result);
Assert.Contains("customParam2=customVal2", result);
Assert.DoesNotContain("query_param_attribute_name", result);
}
finally
{
// Restore original state
ApplicationContext.EnableReflectionCache = originalState;
}
}
}
}