Skip to content

Commit

Permalink
feat: Custom verification headers (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Mar 9, 2022
1 parent a599f25 commit cc4c5ae
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/PactNet.Abstractions/Verifier/IPactVerifierSource.cs
Expand Up @@ -36,6 +36,15 @@ public interface IPactVerifierSource
/// <returns>Fluent builder</returns>
IPactVerifierSource WithSslVerificationDisabled();

/// <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>
IPactVerifierSource WithCustomHeader(string name, string value);

/// <summary>
/// Verify provider interactions
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/PactNet/Interop/NativeInterop.cs
Expand Up @@ -135,6 +135,9 @@ internal static class NativeInterop
[DllImport(DllName, EntryPoint = "pactffi_verifier_set_consumer_filters")]
public static extern void VerifierSetConsumerFilters(IntPtr handle, string[] consumerFilters, ushort consumerFiltersLength);

[DllImport(DllName, EntryPoint = "pactffi_verifier_add_custom_header")]
public static extern void AddCustomHeader(IntPtr handle, string name, string value);

[DllImport(DllName, EntryPoint = "pactffi_verifier_add_file_source")]
public static extern void VerifierAddFileSource(IntPtr handle, string file);

Expand Down
9 changes: 9 additions & 0 deletions src/PactNet/Verifier/IVerifierProvider.cs
Expand Up @@ -67,6 +67,15 @@ internal interface IVerifierProvider : IDisposable
/// <param name="consumerFilters">Consumer filters</param>
void SetConsumerFilters(ICollection<string> consumerFilters);

/// <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>
void AddCustomHeader(string name, string value);

/// <summary>
/// Add a file source
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/PactNet/Verifier/InteropVerifierProvider.cs
Expand Up @@ -120,6 +120,18 @@ public void SetConsumerFilters(ICollection<string> consumerFilters)
NativeInterop.VerifierSetConsumerFilters(this.handle, consumerFilters.ToArray(), (ushort)consumerFilters.Count);
}

/// <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 void AddCustomHeader(string name, string value)
{
NativeInterop.AddCustomHeader(this.handle, name, value);
}

/// <summary>
/// Add a file source
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/PactNet/Verifier/PactVerifierSource.cs
Expand Up @@ -77,6 +77,19 @@ public IPactVerifierSource WithSslVerificationDisabled()
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>
Expand Down
Expand Up @@ -34,6 +34,7 @@ public void HappyPathIntegrationTest()
provider.SetVerificationOptions(false, TimeSpan.FromMilliseconds(100));
provider.SetPublishOptions("1.2.3", new Uri("https://ci.example.org/builds/12345"), new[] { "tags" }, "branch");
provider.SetFilterInfo("description", "state", false);
provider.AddCustomHeader("Authorization", "Bearer abcdef1234567890");

provider.AddFileSource(new FileInfo("data/v2-consumer-integration.json"));
provider.AddDirectorySource(new DirectoryInfo("data"));
Expand Down
9 changes: 9 additions & 0 deletions tests/PactNet.Tests/Verifier/PactVerifierSourceTests.cs
Expand Up @@ -67,6 +67,15 @@ public void WithSslVerificationDisabled_WhenCalled_DisablesSslVerification()
this.mockProvider.Verify(p => p.SetVerificationOptions(true, It.IsAny<TimeSpan>()));
}

[Fact]
public void WithCustomHeader_WhenCalled_AddsCustomHeader()
{
this.verifier.WithCustomHeader("Authorization", "Bearer abcdef0123456789");
this.verifier.Verify();

this.mockProvider.Verify(p => p.AddCustomHeader("Authorization", "Bearer abcdef0123456789"));
}

[Fact]
public void Verify_WithoutRequestTimeout_UsesDefaultTimeout()
{
Expand Down

0 comments on commit cc4c5ae

Please sign in to comment.