mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
appveyor
This commit is contained in:
236
README.md
236
README.md
@@ -1,2 +1,236 @@
|
||||
# WireMock.Net
|
||||
A C# .NET version based on https://github.com/alexvictoor/mock4net which tries to mimic the functionality from http://WireMock.org
|
||||
A C# .NET version based on https://github.com/alexvictoor/WireMock which tries to mimic the functionality from http://WireMock.org
|
||||
|
||||
[](https://ci.appveyor.com/project/StefH/wiremock-net)
|
||||
|
||||
[](https://www.nuget.org/packages/System.Linq.Dynamic.Core)
|
||||
|
||||
The following frameworks are supported:
|
||||
- net45
|
||||
|
||||
WireMock allows to get an HTTP server in a glance.
|
||||
|
||||
A fluent API allows to specify the behavior of the server and hence easily stub and mock webservices and REST ressources.
|
||||
```csharp
|
||||
server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Requests.WithUrl("/*")
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello world!""}")
|
||||
);
|
||||
```
|
||||
|
||||
Based on class HttpListener from the .net framework, it is very lightweight and have no external dependencies.
|
||||
|
||||
# WireMock API in a nutshell
|
||||
|
||||
## Start a server
|
||||
First thing first, to start a server it is as easy as calling a static method, and your done!
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
```
|
||||
You can pass as an argument a port number but if you do not an available port will be chosen for you. Hence the above line of code start aserver bounded to locahost a random port.
|
||||
To know on which port your server is listening, just use server property *Port*.
|
||||
|
||||
## Configure routes and behaviors
|
||||
By default the server returns a dummy message with a 404 status code for all requests. To define a route, you need to specify request for this route and the response you want the server to return. This can be done in a fluent way using classes *Requests* and *Responses* as shown in the example below:
|
||||
```csharp
|
||||
server
|
||||
.Given(
|
||||
Requests.WithUrl("/api").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""result"": ""Some data""}")
|
||||
);
|
||||
```
|
||||
|
||||
The above example is pretty simple. You can be much more selective routing requests according to url patterns, HTTP verbs, request headers and also body contents. Regarding responses, you can specify response headers, status code and body content.
|
||||
Below a more exhaustive example:
|
||||
```csharp
|
||||
server
|
||||
.Given(
|
||||
Requests
|
||||
.WithUrl("/api")
|
||||
.UsingPost()
|
||||
.WithHeader("X-version", "42")
|
||||
.WithBody(@"{ ""msg"": "Hello Body, I will be stripped anyway!!" }");
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("content-type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""Some data""}")
|
||||
);
|
||||
```
|
||||
|
||||
## Verify interactions
|
||||
The server keeps a log of the received requests. You can use this log to verify the interactions that have been done with the server during a test.
|
||||
To get all the request received by the server, you just need to read property *RequestLogs*:
|
||||
```csharp
|
||||
var allRequests = server.RequestLogs;
|
||||
```
|
||||
If you need to be more specific on the requests that have been send to the server, you can use the very same fluent API that allows to define routes:
|
||||
```csharp
|
||||
var customerReadRequests
|
||||
= server.SearchLogsFor(
|
||||
Requests
|
||||
.WithUrl("/api/customer*")
|
||||
.UsingGet()
|
||||
);
|
||||
```
|
||||
|
||||
# WireMock with your favourite test framework
|
||||
|
||||
Obviously you can use your favourite test framework and use WireMock within your tests. In order to avoid flaky tests you should:
|
||||
- let WireMock choose dynamicaly ports. It might seem common sens, avoid hard coded ports in your tests!
|
||||
- clean up the request log or shutdown the server at the end of each test
|
||||
|
||||
Below a simple example using Nunit and NFluent test assertion library:
|
||||
```csharp
|
||||
[SetUp]
|
||||
public void StartMockServer()
|
||||
{
|
||||
_server = FluentMockServer.Start();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async void Should_respond_to_request()
|
||||
{
|
||||
// given
|
||||
_sut = new SomeComponentDoingHttpCalls();
|
||||
|
||||
_server
|
||||
.Given(
|
||||
Requests
|
||||
.WithUrl("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello world!"" }")
|
||||
);
|
||||
|
||||
// when
|
||||
var response
|
||||
= _sut.DoSomething();
|
||||
|
||||
// then
|
||||
Check.That(response).IsEqualTo(EXPECTED_RESULT);
|
||||
// and optionnaly
|
||||
Check.That(_server.SearchLogsFor(Requests.WithUrl("/error*")).IsEmpty();
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
[TearDown]
|
||||
public void ShutdownServer()
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
# WireMock as a standalone process
|
||||
|
||||
This is quite straight forward to launch a mock server within a console application. Below a simple "main" method that takes as a parameter from the commandline a port number and then start a mock server that will respond "Hello World" on every request:
|
||||
```csharp
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int port;
|
||||
if (args.Length == 0 || !int.TryParse(args[0], out port))
|
||||
port = 8080;
|
||||
|
||||
var server = FluentMockServer.Start(port);
|
||||
Console.WriteLine("FluentMockServer running at {0}", server.Port);
|
||||
|
||||
server
|
||||
.Given(Request.WithUrl(u => u.Contains("x")).UsingGet())
|
||||
.RespondWith(Response
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""/x with FUNC 200""}"));
|
||||
|
||||
server
|
||||
.Given(Request.WithUrl("/*").UsingGet())
|
||||
.RespondWith(Response
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBody(@"{ ""msg"": ""Hello world!""}")
|
||||
);
|
||||
|
||||
server
|
||||
.Given(Request.WithUrl("/data").UsingPost().WithBody(b => b.Contains("e")))
|
||||
.RespondWith(Response
|
||||
.WithStatusCode(201)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""data posted with FUNC 201""}"));
|
||||
|
||||
server
|
||||
.Given(Request.WithUrl("/data").UsingPost())
|
||||
.RespondWith(Response
|
||||
.WithStatusCode(201)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""data posted with 201""}"));
|
||||
|
||||
server
|
||||
.Given(Request.WithUrl("/data").UsingDelete())
|
||||
.RespondWith(Response
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""data deleted with 200""}"));
|
||||
|
||||
Console.WriteLine("Press any key to stop the server");
|
||||
Console.ReadKey();
|
||||
|
||||
Console.WriteLine("Displaying all requests");
|
||||
var allRequests = server.RequestLogs;
|
||||
Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
|
||||
|
||||
Console.WriteLine("Press any key to quit");
|
||||
Console.ReadKey();
|
||||
}
|
||||
```
|
||||
|
||||
# SSL
|
||||
You can start a standalone mock server listening for HTTPS requests. To do so, there is just a flag to set when creating the server:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start(port: 8443, ssl: true);
|
||||
```
|
||||
Obviously you need a certificate registered on your box, properly associated with your application and the port number that will be used. This is not really specific to WireMock, not very straightforward and hence the following stackoverflow thread might come handy: [Httplistener with https support](http://stackoverflow.com/questions/11403333/httplistener-with-https-support)
|
||||
|
||||
# Simulating delays
|
||||
A server can be configured with a global delay that will be applied to all requests. To do so you need to call method FluentMockServer.AddRequestProcessingDelay() as below:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server.AddRequestProcessingDelay(TimeSpan.FromSeconds(30)); // add a delay of 30s for all requests
|
||||
```
|
||||
|
||||
Delays can also be configured at route level:
|
||||
```csharp
|
||||
server
|
||||
.Given(
|
||||
Requests
|
||||
.WithUrl("/slow")
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello I'am a little bit slow!"" }")
|
||||
.AfterDelay(TimeSpan.FromSeconds(10)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
# Simulating faults
|
||||
|
||||
Currently not done - need to get rid of HttpListener and use lower level TcpListener in order to be able to implement this properly
|
||||
|
||||
# Advanced usage
|
||||
|
||||
TBD
|
||||
@@ -7,6 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EF242EDF-713
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{197A0EE3-94E5-4807-BBCF-2F1BCA28A6AE}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
appveyor.yml = appveyor.yml
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
|
||||
34
appveyor.yml
Normal file
34
appveyor.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
os: Visual Studio 2015
|
||||
|
||||
version: 1.0.0.{build}
|
||||
|
||||
configuration:
|
||||
- Debug
|
||||
- Release
|
||||
|
||||
platform: Any CPU
|
||||
|
||||
init:
|
||||
- ps: $Env:LABEL = "CI" + $Env:APPVEYOR_BUILD_NUMBER.PadLeft(5, "0")
|
||||
|
||||
install:
|
||||
- ps: Start-FileDownload 'https://download.microsoft.com/download/0/A/3/0A372822-205D-4A86-BFA7-084D2CBE9EDF/DotNetCore.1.0.1-SDK.1.0.0.Preview2-003133-x64.exe'
|
||||
- cmd: DotNetCore.1.0.1-SDK.1.0.0.Preview2-003133-x64 /quiet
|
||||
|
||||
environment:
|
||||
PATH: $(PATH);$(PROGRAMFILES)\dotnet\
|
||||
|
||||
build_script:
|
||||
- appveyor-retry dotnet restore .\src\WireMock -v Minimal
|
||||
|
||||
- dotnet build .\src\WireMock\project.json -c %CONFIGURATION%
|
||||
- cmd: msbuild .\examples\WireMock.Net.ConsoleApplication\WireMock.Net.ConsoleApplication.csproj /p:Configuration=%CONFIGURATION% /p:Platform=AnyCPU
|
||||
- cmd: msbuild .\test\WireMock.Net.Tests\WireMock.Net.Tests.csproj /p:Configuration=%CONFIGURATION% /p:Platform=AnyCPU
|
||||
|
||||
- dotnet pack -c Release --no-build --version-suffix %LABEL% -o .\artifacts .\src\WireMock\project.json
|
||||
|
||||
artifacts:
|
||||
- path: artifacts\**\*.*
|
||||
|
||||
cache:
|
||||
- '%USERPROFILE%\.nuget\packages'
|
||||
Reference in New Issue
Block a user