Skip to content

Commit eec2a85

Browse files
Adopt body-optionality and spread scenarios (Azure#49622)
* Adopt body-optionality scenarios * Add spread tests
1 parent cfc4765 commit eec2a85

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.ClientModel;
2+
using System.Threading.Tasks;
3+
using Azure;
4+
using NUnit.Framework;
5+
using Parameters.BodyOptionality;
6+
7+
namespace TestProjects.Spector.Tests.Http.Parameters.BodyOptionality
8+
{
9+
public class BodyOptionalityTests : SpectorTestBase
10+
{
11+
[SpectorTest]
12+
public Task Parameters_BodyOptionality_requiredExplicit() => Test(async (host) =>
13+
{
14+
Response response = await new BodyOptionalityClient(host, null).RequiredExplicitAsync(new BodyModel("foo"));
15+
Assert.AreEqual(204, response.Status);
16+
});
17+
18+
[SpectorTest]
19+
public Task Parameters_BodyOptionality_OptionalExplicit() => Test(async (host) =>
20+
{
21+
var client = new BodyOptionalityClient(host, null).GetOptionalExplicitClient();
22+
Response response = await client.SetAsync(new BodyModel("foo"));
23+
Assert.AreEqual(204, response.Status);
24+
25+
response = await client.OmitAsync();
26+
Assert.AreEqual(204, response.Status);
27+
});
28+
29+
[SpectorTest]
30+
public Task Parameters_BodyOptionality_requiredImplicit() => Test(async (host) =>
31+
{
32+
Response response = await new BodyOptionalityClient(host, null).RequiredImplicitAsync("foo");
33+
Assert.AreEqual(204, response.Status);
34+
});
35+
}
36+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.ClientModel;
6+
using System.ClientModel.Primitives;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using Azure;
13+
using Azure.Core;
14+
using NUnit.Framework;
15+
using Parameters.Spread;
16+
using Parameters.Spread.Models;
17+
18+
namespace TestProjects.Spector.Tests.Http.Parameters.Spread
19+
{
20+
public class SpreadTests : SpectorTestBase
21+
{
22+
[Test]
23+
public void VerifySpreadParameterWithInnerModelMethod()
24+
{
25+
var expected = new[]
26+
{
27+
(typeof(string), "id", true),
28+
(typeof(string), "xMsTestHeader", true),
29+
(typeof(string), "name", true),
30+
};
31+
ValidateConvenienceMethod(typeof(Alias), "SpreadParameterWithInnerModel", expected);
32+
}
33+
34+
[Test]
35+
public void VerifySpreadParameterWithInnerAliasMethod()
36+
{
37+
var expected = new[]
38+
{
39+
(typeof(string), "id", true),
40+
(typeof(string), "xMsTestHeader", true),
41+
(typeof(string), "name", true),
42+
(typeof(int), "age", true)
43+
};
44+
ValidateConvenienceMethod(typeof(Alias), "SpreadParameterWithInnerAlias", expected);
45+
}
46+
47+
[Test]
48+
public void VerifySpreadAsRequestBodyInModelMethod()
49+
{
50+
var expected = new[]
51+
{
52+
(typeof(string), "name", true),
53+
};
54+
ValidateConvenienceMethod(typeof(Model), "SpreadAsRequestBody", expected);
55+
}
56+
57+
[Test]
58+
public void VerifySpreadAsRequestBodyInAliasMethod()
59+
{
60+
var expected = new[]
61+
{
62+
(typeof(string), "name", true),
63+
};
64+
ValidateConvenienceMethod(typeof(Alias), "SpreadAsRequestBody", expected);
65+
}
66+
67+
[SpectorTest]
68+
public Task Model_SpreadAsRequestBody() => Test(async (host) =>
69+
{
70+
Response response = await new SpreadClient(host, null).GetModelClient().SpreadAsRequestBodyAsync("foo");
71+
Assert.AreEqual(204, response.Status);
72+
});
73+
74+
[SpectorTest]
75+
public Task Model_SpreadCompositeRequest() => Test(async (host) =>
76+
{
77+
Response response = await new SpreadClient(host, null).GetModelClient().SpreadCompositeRequestAsync("foo", "bar", new BodyParameter("foo"));
78+
Assert.AreEqual(204, response.Status);
79+
});
80+
81+
[SpectorTest]
82+
public Task Model_SpreadCompositeRequestMix() => Test(async (host) =>
83+
{
84+
Response response = await new SpreadClient(host, null).GetModelClient().SpreadCompositeRequestMixAsync("foo", "bar", "foo");
85+
Assert.AreEqual(204, response.Status);
86+
});
87+
88+
[SpectorTest]
89+
public Task Model_SpreadCompositeRequestOnlyWithBody() => Test(async (host) =>
90+
{
91+
Response response = await new SpreadClient(host, null).GetModelClient().SpreadCompositeRequestOnlyWithBodyAsync(new BodyParameter("foo"));
92+
Assert.AreEqual(204, response.Status);
93+
});
94+
95+
[SpectorTest]
96+
public Task Model_SpreadCompositeRequestOnlyWithoutBody() => Test(async (host) =>
97+
{
98+
Response response = await new SpreadClient(host, null).GetModelClient().SpreadCompositeRequestWithoutBodyAsync("foo", "bar");
99+
Assert.AreEqual(204, response.Status);
100+
});
101+
102+
[SpectorTest]
103+
public Task Alias_SpreadAsRequestBody() => Test(async (host) =>
104+
{
105+
Response response = await new SpreadClient(host, null).GetAliasClient().SpreadAsRequestBodyAsync("foo");
106+
Assert.AreEqual(204, response.Status);
107+
});
108+
109+
[SpectorTest]
110+
public Task Alias_SpreadAsRequestParameter() => Test(async (host) =>
111+
{
112+
Response response = await new SpreadClient(host, null).GetAliasClient().SpreadAsRequestParameterAsync("1", "bar", "foo");
113+
Assert.AreEqual(204, response.Status);
114+
});
115+
116+
[SpectorTest]
117+
public Task Alias_SpreadWithMultipleParameters() => Test(async (host) =>
118+
{
119+
Response response = await new SpreadClient(host, null).GetAliasClient().SpreadWithMultipleParametersAsync("1", "bar", "foo", new[] { 1, 2 }, 1, new[] { "foo", "bar" });
120+
Assert.AreEqual(204, response.Status);
121+
});
122+
123+
[SpectorTest]
124+
public Task Alias_SpreadWithModel() => Test(async (host) =>
125+
{
126+
Response response = await new SpreadClient(host, null).GetAliasClient().SpreadParameterWithInnerModelAsync("1", "bar", "foo");
127+
Assert.AreEqual(204, response.Status);
128+
});
129+
130+
[SpectorTest]
131+
public Task Alias_SpreadAliasinAlias() => Test(async (host) =>
132+
{
133+
Response response = await new SpreadClient(host, null).GetAliasClient().SpreadParameterWithInnerAliasAsync("1", "bar", "foo", 1);
134+
Assert.AreEqual(204, response.Status);
135+
});
136+
137+
private static void ValidateConvenienceMethod(Type clientType, string methodName, IEnumerable<(Type ParameterType, string Name, bool IsRequired)> expected)
138+
{
139+
var methods = FindMethods(clientType, methodName);
140+
141+
foreach (var method in methods)
142+
{
143+
ValidateConvenienceMethodParameters(method, expected);
144+
}
145+
}
146+
147+
private static IEnumerable<MethodInfo> FindMethods(Type clientType, string methodName)
148+
{
149+
var asyncMethodName = $"{methodName}Async";
150+
var methods = clientType.GetMethods();
151+
152+
return methods.Where(m => m.Name.Equals(methodName) || m.Name.Equals(asyncMethodName));
153+
}
154+
155+
private static void ValidateConvenienceMethodParameters(MethodInfo method, IEnumerable<(Type ParameterType, string Name, bool IsRequired)> expected)
156+
{
157+
if (IsProtocolMethod(method))
158+
return;
159+
160+
expected = expected.Append((typeof(CancellationToken), "cancellationToken", false));
161+
162+
var parameters = method.GetParameters().Where(p => !p.ParameterType.Equals(typeof(RequestOptions)));
163+
var parameterTypes = parameters.Select(p => p.ParameterType);
164+
var parameterNames = parameters.Select(p => p.Name);
165+
var parameterRequiredness = parameters.Select(p => !p.IsOptional);
166+
var expectedTypes = expected.Select(p => p.ParameterType);
167+
var expectedNames = expected.Select(p => p.Name);
168+
var expectedRequiredness = expected.Select(p => p.IsRequired);
169+
170+
CollectionAssert.AreEqual(expectedTypes, parameterTypes);
171+
CollectionAssert.AreEqual(expectedNames, parameterNames);
172+
CollectionAssert.AreEqual(expectedRequiredness, parameterRequiredness);
173+
}
174+
175+
private static bool IsProtocolMethod(MethodInfo method)
176+
=> method.GetParameters().Any(parameter => parameter.ParameterType.Equals(typeof(RequestContent)));
177+
}
178+
}

0 commit comments

Comments
 (0)