-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathPactVerifierSource.cs
115 lines (101 loc) · 4.36 KB
/
PactVerifierSource.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
using System;
using PactNet.Exceptions;
using PactNet.Internal;
namespace PactNet.Verifier
{
/// <summary>
/// Native pact verifier source type state
/// </summary>
internal class PactVerifierSource : IPactVerifierSource
{
private readonly IVerifierProvider provider;
private readonly PactVerifierConfig config;
private TimeSpan timeout = TimeSpan.FromSeconds(5);
private bool disableSslVerification = false;
/// <summary>
/// Initialises a new instance of the <see cref="PactVerifierSource"/> class.
/// </summary>
/// <param name="provider">Pact verifier provider</param>
/// <param name="config">Verifier config</param>
internal PactVerifierSource(IVerifierProvider provider, PactVerifierConfig config)
{
this.provider = provider;
this.config = config;
}
/// <summary>
/// Set up the provider state setup URI so the service can configure states
/// </summary>
/// <param name="providerStateUri">Provider state URI</param>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithProviderStateUrl(Uri providerStateUri)
=> WithProviderStateUrl(providerStateUri, _ => { });
/// <summary>
/// Set up the provider state setup URL so the service can configure states
/// </summary>
/// <param name="providerStateUri">Provider state setup URI</param>
/// <param name="configure">Configure provider state options</param>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithProviderStateUrl(Uri providerStateUri, Action<IProviderStateOptions> configure)
{
Guard.NotNull(providerStateUri, nameof(providerStateUri));
var options = new ProviderStateOptions(this.provider, providerStateUri);
configure?.Invoke(options);
options.Apply();
return this;
}
/// <summary>
/// Filter the interactions to only those matching the given description and/or provider state
/// </summary>
/// <param name="description">Interaction description. All interactions are verified if this is null</param>
/// <param name="providerState">Provider state description. All provider states are verified if this is null</param>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithFilter(string description = null, string providerState = null)
{
// TODO: Allow env vars to specify description and provider state filters like the old version did
// TODO: Support the 'no state' option
this.provider.SetFilterInfo(description, providerState, null);
return this;
}
/// <summary>
/// Set the timeout for all requests to the provider
/// </summary>
/// <param name="timeout">Timeout</param>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithRequestTimeout(TimeSpan timeout)
{
this.timeout = timeout;
return this;
}
/// <summary>
/// Disable certificate verification for HTTPS requests
/// </summary>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithSslVerificationDisabled()
{
this.disableSslVerification = true;
return this;
}
/// <summary>
/// Add a header which will be used in all calls from the verifier to the provider, for example
/// an Authorization header with a valid auth token
/// </summary>
/// <param name="name">Header name</param>
/// <param name="value">Header value</param>
/// <returns>Fluent builder</returns>
public IPactVerifierSource WithCustomHeader(string name, string value)
{
this.provider.AddCustomHeader(name, value);
return this;
}
/// <summary>
/// Verify provider interactions
/// </summary>
/// <exception cref="PactFailureException">Verification failed</exception>
public void Verify()
{
this.provider.SetVerificationOptions(this.disableSslVerification, this.timeout);
this.config.WriteLine("Starting verification...");
this.provider.Execute();
}
}
}