Skip to content

Commit

Permalink
Merge branch 'klimisa-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Oct 9, 2018
2 parents 87ca33e + 10e1281 commit d0c95d6
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 93 deletions.
75 changes: 60 additions & 15 deletions PactNet.Tests/Core/MockProviderHostConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace PactNet.Tests.Core
{
public class MockProviderHostConfigTests
{
private IPactCoreHostConfig GetSubject(int port = 2322, bool enableSsl = false, string consumerName = "My Test Consumer", string providerName = "My Test Provider", PactConfig pactConfig = null, IPAddress host = IPAddress.Loopback)
private IPactCoreHostConfig GetSubject(int port = 2322, bool enableSsl = false,
string consumerName = "My Test Consumer", string providerName = "My Test Provider",
PactConfig pactConfig = null, IPAddress host = IPAddress.Loopback, string sslCert = null, string sslKey = null)
{
return new MockProviderHostConfig(port, enableSsl, consumerName, providerName, pactConfig ?? new PactConfig(), host);
return new MockProviderHostConfig(port, enableSsl, consumerName, providerName,
pactConfig ?? new PactConfig(), host, sslCert, sslKey);
}

[Fact]
Expand All @@ -32,7 +35,8 @@ public void Ctor_WhenCalled_SetsTheCorrectArgs()

var expectedLogFilePath = BuildExpectedLogFilePath(pactConfig.LogDir, providerName);
var expectedPactDir = BuildExpectedPactDir(pactConfig.PactDir);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir, pactConfig.SpecificationVersion, consumerName, providerName);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName);

Assert.Equal(expectedArguments, config.Arguments);
}
Expand All @@ -50,7 +54,31 @@ public void Ctor_WhenCalledWithSsl_SetsTheCorrectArgs()

var expectedLogFilePath = BuildExpectedLogFilePath(pactConfig.LogDir, providerName);
var expectedPactDir = BuildExpectedPactDir(pactConfig.PactDir);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir, pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);

Assert.Equal(expectedArguments, config.Arguments);
}

[Fact]
public void Ctor_WhenCalledWithCustomSsl_SetsTheCorrectArgs()
{
var port = 9332;
var pactConfig = new PactConfig();
var consumerName = "Cons";
var providerName = "The best one";
var enableSsl = true;
var sslCert = "..\\cert\\localhost.cer";
var sslKey = "..\\cert\\localhost.key";

var config = GetSubject(port, enableSsl, consumerName, providerName, pactConfig, IPAddress.Loopback, sslCert, sslKey);

var expectedLogFilePath = BuildExpectedLogFilePath(pactConfig.LogDir, providerName);
var expectedPactDir = BuildExpectedPactDir(pactConfig.PactDir);
var expectedSslCert = BuildExpectedSslOption(sslCert);
var expectedSslKey = BuildExpectedSslOption(sslKey);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName, enableSsl, IPAddress.Loopback, expectedSslCert, expectedSslKey);

Assert.Equal(expectedArguments, config.Arguments);
}
Expand All @@ -69,7 +97,8 @@ public void Ctor_WhenCalledWithHost_SetsTheCorrectArgs()

var expectedLogFilePath = BuildExpectedLogFilePath(pactConfig.LogDir, providerName);
var expectedPactDir = BuildExpectedPactDir(pactConfig.PactDir);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir, pactConfig.SpecificationVersion, consumerName, providerName, enableSsl, host);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName, enableSsl, host);

Assert.Equal(expectedArguments, config.Arguments);
}
Expand All @@ -79,7 +108,7 @@ public void Ctor_WhenCalledWithNonDefaultLogDirectory_SetsTheCorrectArgs()
{
var port = 9332;
var logDir = "./test";
var pactConfig = new PactConfig { LogDir = logDir };
var pactConfig = new PactConfig {LogDir = logDir};
var consumerName = "Cons";
var providerName = "The best one";
var enableSsl = true;
Expand All @@ -88,7 +117,8 @@ public void Ctor_WhenCalledWithNonDefaultLogDirectory_SetsTheCorrectArgs()

var expectedLogFilePath = BuildExpectedLogFilePath(logDir + "\\", providerName);
var expectedPactDir = BuildExpectedPactDir(pactConfig.PactDir);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir, pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);

Assert.Equal(expectedArguments, config.Arguments);
}
Expand All @@ -98,7 +128,7 @@ public void Ctor_WhenCalledWithNonDefaultPactDirectory_SetsTheCorrectArgs()
{
var port = 9332;
var pactDir = "./test";
var pactConfig = new PactConfig { PactDir = pactDir };
var pactConfig = new PactConfig {PactDir = pactDir};
var consumerName = "Cons";
var providerName = "The best one";
var enableSsl = true;
Expand All @@ -107,7 +137,8 @@ public void Ctor_WhenCalledWithNonDefaultPactDirectory_SetsTheCorrectArgs()

var expectedLogFilePath = BuildExpectedLogFilePath(pactConfig.LogDir, providerName);
var expectedPactDir = BuildExpectedPactDir(pactDir + "\\");
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir, pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);
var expectedArguments = BuildExpectedArguments(port, expectedLogFilePath, expectedPactDir,
pactConfig.SpecificationVersion, consumerName, providerName, enableSsl);

Assert.Equal(expectedArguments, config.Arguments);
}
Expand All @@ -129,6 +160,11 @@ private string BuildExpectedPactDir(string pactDir)
{
return pactDir.Replace("\\", "/");
}

private string BuildExpectedSslOption(string fullName)
{
return $"{fullName}".Replace("\\", "/");
}

private string BuildExpectedArguments(
int port,
Expand All @@ -138,13 +174,22 @@ private string BuildExpectedArguments(
string consumerName,
string providerName,
bool enableSsl = false,
IPAddress host = IPAddress.Loopback)
IPAddress host = IPAddress.Loopback,
string sslCert = "",
string sslKey = ""
)
{

var sslOption = enableSsl ? " --ssl" : "";
var hostOption = host == IPAddress.Any ? " --host=0.0.0.0" : "";

return $"-p {port} -l \"{logFilePath}\" --pact-dir \"{pactFileDir}\" --pact-specification-version \"{pactSpecificationVersion}\" --consumer \"{consumerName}\" --provider \"{providerName}\"{sslOption}{hostOption}";
var hostOption = host == IPAddress.Any ? " --host=0.0.0.0" : "";
var sslCertOption = !string.IsNullOrEmpty(sslCert) ? $" --sslcert=\"{sslCert}\"" : string.Empty;
var sslKeyOption = !string.IsNullOrEmpty(sslKey) ? $" --sslkey=\"{sslKey}\"" : string.Empty;

return
$"-p {port} -l \"{logFilePath}\" " +
$"--pact-dir \"{pactFileDir}\" " +
$"--pact-specification-version \"{pactSpecificationVersion}\" " +
$"--consumer \"{consumerName}\" " +
$"--provider \"{providerName}\"{sslOption}{hostOption}{sslCertOption}{sslKeyOption}";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public FailureIntegrationTestsMyApiPact()
{
var pactConfig = new PactConfig();

PactBuilder = new PactBuilder((port, enableSsl, consumerName, providerName, host, jsonSerializerSettings) =>
new MockProviderService(
baseUri => new RubyHttpHost(baseUri, "MyConsumer", "MyApi", pactConfig, host),
port, enableSsl,
baseUri => new AdminHttpClient(baseUri, jsonSerializerSettings)))
PactBuilder = new PactBuilder(
(port, enableSsl, consumerName, providerName, host, jsonSerializerSettings, sslCert, sslKey) =>
new MockProviderService(
baseUri => new RubyHttpHost(baseUri, "MyConsumer", "MyApi", pactConfig, host),
port, enableSsl,
baseUri => new AdminHttpClient(baseUri, jsonSerializerSettings)))
.ServiceConsumer("FailureIntegrationTests")
.HasPactWith("MyApi");

Expand Down
11 changes: 6 additions & 5 deletions PactNet.Tests/IntegrationTests/IntegrationTestsMyApiPact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public IntegrationTestsMyApiPact()
{
var pactConfig = new PactConfig();

PactBuilder = new PactBuilder((port, enableSsl, consumerName, providerName, host, jsonSerializerSettings) =>
new MockProviderService(
baseUri => new RubyHttpHost(baseUri, "MyConsumer", "MyApi", pactConfig, host),
port, enableSsl,
baseUri => new AdminHttpClient(baseUri, jsonSerializerSettings)))
PactBuilder = new PactBuilder(
(port, enableSsl, consumerName, providerName, host, jsonSerializerSettings, sslCert, sslKey) =>
new MockProviderService(
baseUri => new RubyHttpHost(baseUri, "MyConsumer", "MyApi", pactConfig, host),
port, enableSsl,
baseUri => new AdminHttpClient(baseUri, jsonSerializerSettings)))
.ServiceConsumer("IntegrationTests")
.HasPactWith("MyApi");

Expand Down
Loading

0 comments on commit d0c95d6

Please sign in to comment.