-
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathRespondWithAProvider.cs
More file actions
417 lines (345 loc) · 10.8 KB
/
RespondWithAProvider.cs
File metadata and controls
417 lines (345 loc) · 10.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright © WireMock.Net and mock4net by Alexandre Victoor
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System.Net;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.ResponseProviders;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.Server;
/// <summary>
/// The RespondWithAProvider.
/// </summary>
internal class RespondWithAProvider : IRespondWithAProvider
{
private readonly RegistrationCallback _registrationCallback;
private readonly IRequestMatcher _requestMatcher;
private readonly WireMockServerSettings _settings;
private readonly IDateTimeUtils _dateTimeUtils;
private readonly IGuidUtils _guidUtils;
private readonly bool _saveToFile;
private int _priority;
private string? _title;
private string? _description;
private string? _path;
private string? _executionConditionState;
private string? _nextState;
private string? _scenario;
private int _timesInSameState = 1;
private bool? _useWebhookFireAndForget;
private double? _probability;
private bool _isDisabled = false;
private GraphQLSchemaDetails? _graphQLSchemaDetails; // Future Use.
public Guid Guid { get; private set; }
public IWebhook[]? Webhooks { get; private set; }
public ITimeSettings? TimeSettings { get; private set; }
public object? Data { get; private set; }
public IdOrTexts? ProtoDefinition { get; private set; }
internal RespondWithAProvider(
RegistrationCallback registrationCallback,
IRequestMatcher requestMatcher,
WireMockServerSettings settings,
IGuidUtils guidUtils,
IDateTimeUtils dateTimeUtils,
bool saveToFile = false
)
{
_registrationCallback = Guard.NotNull(registrationCallback);
_requestMatcher = Guard.NotNull(requestMatcher);
_settings = Guard.NotNull(settings);
_dateTimeUtils = Guard.NotNull(dateTimeUtils);
_guidUtils = Guard.NotNull(guidUtils);
_saveToFile = saveToFile;
Guid = guidUtils.NewGuid();
}
/// <inheritdoc />
public void RespondWith(IResponseProvider provider)
{
if (provider is Response response && response.WebSocketBuilder != null)
{
// If the provider is a Response with a WebSocketBuilder, we need to use a WebSocketResponseProvider instead.
provider = new WebSocketResponseProvider(
response.WebSocketBuilder,
_guidUtils,
_dateTimeUtils
);
}
var mapping = new Mapping
(
Guid,
_dateTimeUtils.UtcNow,
_title,
_description,
_path,
_settings,
_requestMatcher,
provider,
_priority,
_scenario,
_executionConditionState,
_nextState,
_timesInSameState,
Webhooks,
_useWebhookFireAndForget,
TimeSettings,
Data
);
if (_probability != null)
{
mapping.WithProbability(_probability.Value);
}
if (_isDisabled)
{
mapping.IsDisabled = true;
}
if (ProtoDefinition != null)
{
mapping.WithProtoDefinition(ProtoDefinition.Value);
}
_registrationCallback(mapping, _saveToFile);
}
/// <inheritdoc />
public void ThenRespondWith(Action<IResponseBuilder> action)
{
var responseBuilder = Response.Create();
action(responseBuilder);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithOK()
{
var responseBuilder = Response.Create();
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(int code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(string code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public void ThenRespondWithStatusCode(HttpStatusCode code)
{
var responseBuilder = Response.Create().WithStatusCode(code);
RespondWith(responseBuilder);
}
/// <inheritdoc />
public IRespondWithAProvider WithData(object data)
{
Data = data;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithGuid(string guid)
{
return WithGuid(Guid.Parse(guid));
}
/// <inheritdoc />
public IRespondWithAProvider WithGuid(Guid guid)
{
Guid = guid;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider DefineGuid(Guid guid)
{
return WithGuid(guid);
}
/// <inheritdoc />
public IRespondWithAProvider DefineGuid(string guid)
{
return WithGuid(guid);
}
/// <inheritdoc />
public IRespondWithAProvider WithTitle(string title)
{
_title = title;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithDescription(string description)
{
_description = description;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithPath(string path)
{
_path = path;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider AtPriority(int priority)
{
_priority = priority;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider InScenario(string scenario)
{
_scenario = Guard.NotNullOrWhiteSpace(scenario);
return this;
}
/// <inheritdoc />
public IRespondWithAProvider InScenario(int scenario)
{
return InScenario(scenario.ToString());
}
/// <inheritdoc />
public IRespondWithAProvider WhenStateIs(string state)
{
if (string.IsNullOrEmpty(_scenario))
{
throw new NotSupportedException("Unable to set state condition when no scenario is defined.");
}
_executionConditionState = state;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WhenStateIs(int state)
{
return WhenStateIs(state.ToString());
}
/// <inheritdoc />
public IRespondWithAProvider WillSetStateTo(string state, int? times = 1)
{
if (string.IsNullOrEmpty(_scenario))
{
throw new NotSupportedException("Unable to set next state when no scenario is defined.");
}
_nextState = state;
_timesInSameState = times ?? 1;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WillSetStateTo(int state, int? times = 1)
{
return WillSetStateTo(state.ToString(), times);
}
/// <inheritdoc />
public IRespondWithAProvider WithTimeSettings(ITimeSettings timeSettings)
{
TimeSettings = Guard.NotNull(timeSettings);
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithWebhook(params IWebhook[] webhooks)
{
Guard.HasNoNulls(webhooks);
Webhooks = webhooks;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithWebhook(
string url,
string method = "post",
IDictionary<string, WireMockList<string>>? headers = null,
string? body = null,
bool useTransformer = true,
TransformerType transformerType = TransformerType.Handlebars)
{
Guard.NotNull(url);
Guard.NotNull(method);
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];
if (body != null)
{
Webhooks[0].Request.BodyData = new BodyData
{
BodyAsString = body,
DetectedBodyType = BodyType.String,
DetectedBodyTypeFromContentType = BodyType.String
};
}
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithWebhook(
string url,
string method = "post",
IDictionary<string, WireMockList<string>>? headers = null,
object? body = null,
bool useTransformer = true,
TransformerType transformerType = TransformerType.Handlebars)
{
Guard.NotNull(url);
Guard.NotNull(method);
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];
if (body != null)
{
Webhooks[0].Request.BodyData = new BodyData
{
BodyAsJson = body,
DetectedBodyType = BodyType.Json,
DetectedBodyTypeFromContentType = BodyType.Json
};
}
return this;
}
public IRespondWithAProvider WithWebhookFireAndForget(bool useWebhooksFireAndForget)
{
_useWebhookFireAndForget = useWebhooksFireAndForget;
return this;
}
public IRespondWithAProvider WithProbability(double probability)
{
_probability = Guard.Condition(probability, p => p is >= 0 and <= 1.0);
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithIsDisabled(bool isDisabled)
{
_isDisabled = isDisabled;
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinitionOrId)
{
Guard.NotNull(protoDefinitionOrId);
ProtoDefinition = ProtoDefinitionUtils.GetIdOrTexts(_settings, protoDefinitionOrId);
return this;
}
/// <inheritdoc />
public IRespondWithAProvider WithGraphQLSchema(string graphQLSchemaOrId, IDictionary<string, Type>? customScalars = null)
{
Guard.NotNullOrWhiteSpace(graphQLSchemaOrId);
if (_settings.GraphQLSchemas?.TryGetValue(graphQLSchemaOrId, out _graphQLSchemaDetails) != true)
{
_graphQLSchemaDetails = new GraphQLSchemaDetails
{
SchemaAsString = graphQLSchemaOrId,
CustomScalars = customScalars
};
}
return this;
}
private static IWebhook InitWebhook(
string url,
string method,
IDictionary<string, WireMockList<string>>? headers,
bool useTransformer,
TransformerType transformerType
)
{
return new Webhook
{
Request = new WebhookRequest
{
Url = url,
Method = method,
Headers = headers,
UseTransformer = useTransformer,
TransformerType = transformerType
}
};
}
}