Code Samples for WireMock.Net Walkthrough
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using RestSharp; | |
using Newtonsoft.Json; | |
namespace WireMockSample | |
{ | |
public class SimpleServiceCaller | |
{ | |
private readonly string _baseUrl; | |
public SimpleServiceCaller(string baseUrl){ | |
_baseUrl = baseUrl; | |
} | |
public SimpleLoginResponse DoSimpleLogin(string userName) | |
{ | |
var client = new RestClient(_baseUrl); | |
var request = new RestRequest("/player/startSession", Method.POST); | |
request.RequestFormat = DataFormat.Json; | |
request.AddBody(new SimpleLoginRequest | |
{ | |
userName = userName | |
}); | |
var response = (RestResponse)client.Execute(request); | |
SimpleLoginResponse loginResponse = null; | |
if (response.IsSuccessful) | |
{ | |
loginResponse = JsonConvert.DeserializeObject<SimpleLoginResponse>(response.Content); | |
} | |
else | |
{ | |
if (response.StatusCode.Equals(HttpStatusCode.Forbidden)) | |
{ | |
loginResponse = new SimpleLoginResponse | |
{ | |
error = "Forbidden User" | |
}; | |
} | |
} | |
return loginResponse; | |
} | |
} | |
public class SimpleLoginRequest | |
{ | |
public string userName; | |
} | |
public class SimpleLoginResponse | |
{ | |
public int id; | |
public string userName; | |
public string error; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using NUnit.Framework; | |
using WireMock.Matchers; | |
using WireMock.Server; | |
using WireMock.RequestBuilders; | |
using WireMock.ResponseBuilders; | |
namespace WireMockSample | |
{ | |
[TestFixture] | |
public class SimpleServiceCallerTest | |
{ | |
private static FluentMockServer _mockServer; | |
private static SimpleServiceCaller _simpleCaller; | |
private static bool _isStarted; | |
// Because my environment is setup for Unity development, | |
// I don't have the latest nunit.framework available with | |
// [TestFixtureSetup], but that would be where the initial | |
// server startup would go in a more up-to-date environment | |
[SetUp] | |
public void SetUpWireMock() | |
{ | |
if (!_isStarted) | |
{ | |
_mockServer = FluentMockServer.Start(); | |
// WireMock selects its own ports, so just grab the first | |
// generated URL string | |
_simpleCaller = new SimpleServiceCaller(_mockServer.Urls.First()); | |
_isStarted = true; | |
} | |
} | |
private static void StubLogin(string testUser, int expectedId) | |
{ | |
var canned = new SimpleLoginResponse | |
{ | |
userName = testUser, | |
id = expectedId | |
}; | |
_mockServer | |
.Given(Request | |
.Create().WithPath("/player/startSession") | |
// The canned response will *only* be sent to the | |
// request with the corresponding userName | |
.WithBody(new JsonMatcher("{ \"userName\": \"" + testUser + "\" }")) | |
.UsingPost()) | |
.RespondWith( | |
Response.Create() | |
.WithStatusCode(200) | |
.WithHeader("Content-Type", "application/json") | |
.WithBodyAsJson(canned) | |
); | |
} | |
[Test] | |
[Ignore("only works on my machine")] | |
/* | |
* This will only work if you happen to | |
* have a version of the target service | |
* running at localhost port 8787 | |
*/ | |
public void TestSimpleLogin() | |
{ | |
var caller = new SimpleServiceCaller("http://localhost:8787"); | |
var userId = Guid.NewGuid().ToString().Replace("-", ""); | |
var response = caller.DoSimpleLogin(userId); | |
Assert.AreEqual(userId, response.userName); | |
} | |
[Test] | |
/* | |
* This will work anytime, anywhere! | |
*/ | |
public void TestSimpleLoginWireMock() | |
{ | |
var userId = Guid.NewGuid().ToString(); | |
StubLogin(userId, 42); | |
var response = _simpleCaller.DoSimpleLogin(userId); | |
Assert.AreEqual(userId, response.userName); | |
// predictable id! | |
Assert.AreEqual(42, response.id); | |
} | |
[Test] | |
/* | |
* The mock service can handle multiple requests in one instance | |
* and can give different answers depending on the request | |
*/ | |
public void TestSimpleLoginWireMockMultiple() | |
{ | |
var rand = new Random(); | |
for (var i = 1; i <= 5; i++) | |
{ | |
var user = "testUser" + i; | |
StubLogin(user, i*33); | |
} | |
for (var x = 0; x < 10; x++) | |
{ | |
var testNum = rand.Next() % 5; | |
var testUser = "testUser" + (testNum + 1); | |
var response = _simpleCaller.DoSimpleLogin(testUser); | |
//check that userId is matched with expected response id | |
Assert.AreEqual(testUser, response.userName); | |
Assert.AreEqual((testNum+1)*33, response.id); | |
} | |
} | |
[Test] | |
/* | |
* Mocking error conditions is simple | |
*/ | |
public void TestLoginFailureWireMock() | |
{ | |
// Note that because the failure is tied to a specific userName, | |
// the other tests will still function properly | |
_mockServer | |
.Given(Request | |
.Create().WithPath("/player/startSession") | |
// The canned response will *only* be sent to the | |
// request with the corresponding userName | |
.WithBody(new JsonMatcher("{ \"userName\": \"BADWOLF\" }")) | |
.UsingPost()) | |
.RespondWith( | |
Response.Create() | |
.WithStatusCode(HttpStatusCode.Forbidden) | |
); | |
var response = _simpleCaller.DoSimpleLogin("BADWOLF"); | |
Assert.AreEqual("Forbidden User", response.error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment