Skip to content

Commit

Permalink
Refactoring to use HttpVerb instead of HttpMethod on public interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Oct 25, 2014
1 parent a681559 commit 4a58f0b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
10 changes: 5 additions & 5 deletions .nuget/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenCover" version="4.5.3207" />
<package id="ReportGenerator" version="1.9.1.0" />
<package id="xunit.runners" version="1.9.2" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenCover" version="4.5.3207" />
<package id="ReportGenerator" version="1.9.1.0" />
<package id="xunit.runners" version="1.9.2" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net.Http;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Nancy;

namespace PactNet.Tests.IntegrationTests
Expand All @@ -19,7 +20,8 @@ public FailureIntegrationTestsMyApiPact()
new MockProviderService(
baseUri => new NancyHttpHost(baseUri, new IntegrationTestingMockProviderNancyBootstrapper()),
port, enableSsl,
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) }))
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) },
new HttpMethodMapper()))
.ServiceConsumer("FailureIntegrationTests")
.HasPactWith("MyApi");

Expand Down
4 changes: 3 additions & 1 deletion PactNet.Tests/IntegrationTests/IntegrationTestsMyApiPact.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net.Http;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Nancy;

namespace PactNet.Tests.IntegrationTests
Expand All @@ -19,7 +20,8 @@ public IntegrationTestsMyApiPact()
new MockProviderService(
baseUri => new NancyHttpHost(baseUri, new IntegrationTestingMockProviderNancyBootstrapper()),
port, enableSsl,
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) }))
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) },
new HttpMethodMapper()))
.ServiceConsumer("IntegrationTests")
.HasPactWith("MyApi");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using PactNet.Configuration.Json;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Tests.Fakes;
using Xunit;
Expand All @@ -32,7 +33,8 @@ private IMockProviderService GetSubject(int port = 1234, bool enableSsl = false)
},
port,
enableSsl,
baseUri => new HttpClient(_fakeHttpMessageHandler) { BaseAddress = new Uri(baseUri) });
baseUri => new HttpClient(_fakeHttpMessageHandler) { BaseAddress = new Uri(baseUri) },
new HttpMethodMapper());
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions PactNet.Tests/PactBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Net.Http;
using NSubstitute;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Models;
using Xunit;

Expand Down Expand Up @@ -191,7 +191,7 @@ public void Build_WhenCalledWithTheMockProviderServiceIsInitialised_CallsSendAdm

pactBuilder.Build();

mockProviderService.Received(1).SendAdminHttpRequest(HttpMethod.Post, Constants.PactPath, Arg.Is<PactDetails>(x => x.Consumer.Name == consumerName && x.Provider.Name == providerName));
mockProviderService.Received(1).SendAdminHttpRequest(HttpVerb.Post, Constants.PactPath, Arg.Is<PactDetails>(x => x.Consumer.Name == consumerName && x.Provider.Name == providerName));
}

[Fact]
Expand Down
3 changes: 1 addition & 2 deletions PactNet/Mocks/MockHttpService/IMockProviderService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.Http;
using PactNet.Mocks.MockHttpService.Models;

namespace PactNet.Mocks.MockHttpService
Expand All @@ -11,6 +10,6 @@ public interface IMockProviderService : IMockProvider<IMockProviderService>
void Stop();
void ClearInteractions();
void VerifyInteractions();
void SendAdminHttpRequest<T>(HttpMethod method, string path, T requestContent) where T : class;
void SendAdminHttpRequest<T>(HttpVerb method, string path, T requestContent) where T : class;
}
}
23 changes: 14 additions & 9 deletions PactNet/Mocks/MockHttpService/MockProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using Newtonsoft.Json;
using PactNet.Configuration.Json;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Mocks.MockHttpService.Nancy;

Expand All @@ -14,7 +15,8 @@ public class MockProviderService : IMockProviderService
{
private readonly Func<Uri, IHttpHost> _hostFactory;
private IHttpHost _host;
private readonly HttpClient _httpClient;
private readonly HttpClient _httpClient;
private readonly IHttpMethodMapper _httpMethodMapper;

private string _providerState;
private string _description;
Expand All @@ -27,19 +29,22 @@ internal MockProviderService(
Func<Uri, IHttpHost> hostFactory,
int port,
bool enableSsl,
Func<string, HttpClient> httpClientFactory)
Func<string, HttpClient> httpClientFactory,
IHttpMethodMapper httpMethodMapper)
{
_hostFactory = hostFactory;
BaseUri = String.Format("{0}://localhost:{1}", enableSsl ? "https" : "http", port);
_httpClient = httpClientFactory(BaseUri);
_httpMethodMapper = httpMethodMapper;
}

public MockProviderService(int port, bool enableSsl, string pactFileDirectory = null)
: this(
baseUri => new NancyHttpHost(baseUri, pactFileDirectory),
port,
enableSsl,
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) })
baseUri => new HttpClient { BaseAddress = new Uri(baseUri) },
new HttpMethodMapper())
{
}

Expand Down Expand Up @@ -93,7 +98,7 @@ public void WillRespondWith(ProviderServiceResponse response)

public void VerifyInteractions()
{
SendAdminHttpRequest(HttpMethod.Get, Constants.InteractionsVerificationPath);
SendAdminHttpRequest(HttpVerb.Get, Constants.InteractionsVerificationPath);
}

private void RegisterInteraction()
Expand Down Expand Up @@ -121,7 +126,7 @@ private void RegisterInteraction()
Response = _response
};

SendAdminHttpRequest(HttpMethod.Post, Constants.InteractionsPath, interaction);
SendAdminHttpRequest(HttpVerb.Post, Constants.InteractionsPath, interaction);

ClearTrasientState();
}
Expand All @@ -143,11 +148,11 @@ public void ClearInteractions()
{
if (_host != null)
{
SendAdminHttpRequest(HttpMethod.Delete, Constants.InteractionsPath);
SendAdminHttpRequest(HttpVerb.Delete, Constants.InteractionsPath);
}
}

public void SendAdminHttpRequest<T>(HttpMethod method, string path, T requestContent) where T : class
public void SendAdminHttpRequest<T>(HttpVerb method, string path, T requestContent) where T : class
{
if (_host == null)
{
Expand All @@ -156,7 +161,7 @@ public void SendAdminHttpRequest<T>(HttpMethod method, string path, T requestCon

var responseContent = String.Empty;

var request = new HttpRequestMessage(method, path);
var request = new HttpRequestMessage(_httpMethodMapper.Convert(method), path);
request.Headers.Add(Constants.AdministrativeRequestHeaderKey, "true");

if (requestContent != null)
Expand All @@ -182,7 +187,7 @@ public void SendAdminHttpRequest<T>(HttpMethod method, string path, T requestCon
}
}

private void SendAdminHttpRequest(HttpMethod method, string path)
private void SendAdminHttpRequest(HttpVerb method, string path)
{
SendAdminHttpRequest<object>(method, path, null);
}
Expand Down
4 changes: 2 additions & 2 deletions PactNet/PactBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Net.Http;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Models;
using PactNet.Models;

namespace PactNet
Expand Down Expand Up @@ -89,7 +89,7 @@ private void PersistPactFile()
Consumer = new Party { Name = ConsumerName }
};

_mockProviderService.SendAdminHttpRequest(HttpMethod.Post, Constants.PactPath, pactDetails);
_mockProviderService.SendAdminHttpRequest(HttpVerb.Post, Constants.PactPath, pactDetails);
}
}
}

0 comments on commit 4a58f0b

Please sign in to comment.