-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathdpg-customization.cs
175 lines (151 loc) · 7.52 KB
/
dpg-customization.cs
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
using System;
using System.Linq;
using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using Azure;
using Azure.Core;
using Azure.Core.Tests;
using dpg_customization_LowLevel;
using dpg_customization_LowLevel.Models;
using NUnit.Framework;
namespace AutoRest.TestServer.Tests
{
public class DpgCustomizationTest : TestServerLowLevelTestBase
{
[Test]
public Task GetRawModel() => Test(async (host) =>
{
Response result = await new DPGClient(host, Key, null).GetModelAsync("raw", new());
JsonData responseBody = JsonData.FromBytes(result.Content.ToMemory());
Assert.AreEqual("raw", (string)responseBody["received"]);
});
[Test]
public Task GetHandwrittenModel() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
var scopes = diagnosticListener.Scopes;
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Response<Product> result = await new DPGClient(host, Key, null).GetModelValueAsync("model");
Assert.AreEqual(1, scopes.Count);
Assert.AreEqual("DPGClient.GetModelValue", scopes[0].Name);
Assert.True(scopes[0].IsCompleted);
Assert.AreEqual("model", $"{result.Value.Received}");
});
[Test]
public Task PostRawModel() => Test(async (host) =>
{
var value = new
{
hello = "world!"
};
Response result = await new DPGClient(host, Key, null).PostModelAsync("raw", RequestContent.Create(value));
Assert.AreEqual(200, result.Status);
JsonData responseBody = JsonData.FromBytes(result.Content.ToMemory());
Assert.AreEqual("raw", (string)responseBody["received"]);
});
[Test]
public Task PostHandwrittenModel() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
var scopes = diagnosticListener.Scopes;
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Input input = new Input("world!");
Response<Product> result = await new DPGClient(host, Key, null).PostModelAsync("model", input);
Assert.True(scopes.Count == 1);
Assert.AreEqual(scopes[0].Name, "DPGClient.PostModel");
Assert.True(scopes[0].IsCompleted);
Assert.AreEqual("model", $"{result.Value.Received}");
});
[Test]
public Task GetRawPages() => Test(async (host) =>
{
AsyncPageable<BinaryData> allPages = new DPGClient(host, Key, null).GetPagesAsync("raw", new());
await foreach (Page<BinaryData> page in allPages.AsPages())
{
var firstItem = JsonData.FromBytes(page.Values.First());
Assert.AreEqual("raw", (string)firstItem["received"]);
}
});
[Test]
public Task GetHandwrittenModelPages() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
AsyncPageable<Product> allPages = new DPGClient(host, Key, null).GetPageValuesAsync("model");
var pagesCount = 0;
await foreach (Page<Product> page in allPages.AsPages())
{
pagesCount++;
Assert.AreEqual("model", $"{page.Values.First().Received}");
}
Assert.AreEqual(pagesCount, diagnosticListener.Scopes.Count);
});
[Test]
public Task RawLRO() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Operation<BinaryData> result = await new DPGClient(host, Key, null).LroAsync(WaitUntil.Started, "raw", new());
diagnosticListener.AssertAndRemoveScope("DPGClient.Lro");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
await result.WaitForCompletionAsync();
diagnosticListener.AssertAndRemoveScope("DPGClient.Lro.WaitForCompletion");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
JsonData responseBody = JsonData.FromBytes(result.Value.ToMemory());
Assert.AreEqual("raw", (string)responseBody["received"]);
});
[Test]
public Task HandwrittenModelLro() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Operation<Product> lro = await new DPGClient(host, Key, null).LroValueAsync(WaitUntil.Started, "model");
diagnosticListener.AssertAndRemoveScope("DPGClient.LroValue");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
await lro.WaitForCompletionAsync();
diagnosticListener.AssertAndRemoveScope("DPGClient.LroValue.WaitForCompletion");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Assert.AreEqual("model", $"{lro.Value.Received}");
});
[Test]
public Task HandwrittenModelLro_ManualIteration() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Operation<Product> lro = await new DPGClient(host, Key, null).LroValueAsync(WaitUntil.Started, "model");
diagnosticListener.AssertAndRemoveScope("DPGClient.LroValue");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
while (!lro.HasCompleted)
{
await lro.UpdateStatusAsync();
diagnosticListener.AssertAndRemoveScope("DPGClient.LroValue.UpdateStatus");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
}
});
[Test]
public Task HandwrittenModelLro_WaitUntilCompleted() => Test(async (host) =>
{
using var diagnosticListener = new ClientDiagnosticListener("dpg_customization_LowLevel", asyncLocal: true);
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Operation<Product> lro = await new DPGClient(host, Key, null).LroValueAsync(WaitUntil.Completed, "model");
diagnosticListener.AssertAndRemoveScope("DPGClient.LroValue");
CollectionAssert.IsEmpty(diagnosticListener.Scopes);
Assert.AreEqual("model", $"{lro.Value.Received}");
});
[Test]
public Task DPGGlassBreaker() => Test(async (host) =>
{
var pipeline = new DPGClient(host, Key, null).Pipeline;
HttpMessage message = pipeline.CreateMessage();
Request request = message.Request;
request.Method = RequestMethod.Get;
var uri = new RawRequestUriBuilder();
uri.Reset(host);
uri.AppendPath("/servicedriven/glassbreaker", false);
request.Uri = uri;
request.Headers.Add("Accept", "application/json");
Response result = await pipeline.ProcessMessageAsync(message, null).ConfigureAwait(false);
Assert.AreEqual(200, result.Status);
});
}
}