mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
* added byte[] body
* updated readme
This commit is contained in:
214
README.md
214
README.md
@@ -5,29 +5,8 @@ A C# .NET version based on https://github.com/alexvictoor/WireMock which tries t
|
||||
|
||||
[](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
|
||||
var 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
|
||||
@@ -36,40 +15,84 @@ 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:
|
||||
## Stubbing
|
||||
A core feature of WireMock is the ability to return canned HTTP responses for requests matching criteria.
|
||||
|
||||
### Basic stubbing
|
||||
The following code will configure a response with a status of 200 to be returned when the relative URL exactly matches /some/thing (including query parameters). The body of the response will be “Hello world!” and a Content-Type header will be sent with a value of text-plain.
|
||||
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Requests.WithUrl("/api").UsingGet()
|
||||
Request.WithUrl("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
Response
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""result"": ""Some data""}")
|
||||
);
|
||||
.WithHeader("Content-Type", "text/plain")
|
||||
.WithBody("Hello world!")
|
||||
);
|
||||
```
|
||||
HTTP methods currently supported are: GET, POST, PUT, DELETE, HEAD. You can specify ANY (`.UsingAny`) if you want the stub mapping to match on any request method.
|
||||
|
||||
A response body in binary format can be specified as a `byte[]` via an overloaded `WithBody()`:
|
||||
|
||||
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
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Requests
|
||||
.WithUrl("/api")
|
||||
.UsingPost()
|
||||
.WithHeader("X-version", "42")
|
||||
.WithBody(@"{ ""msg"": "Hello Body, I will be stripped anyway!!" }");
|
||||
Request.WithUrl("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("content-type", "application/json")
|
||||
.WithBody(@"{ ""result"": ""Some data""}")
|
||||
);
|
||||
Response
|
||||
.WithBody(new byte[] { 48, 65, 6c, 6c, 6f })
|
||||
);
|
||||
```
|
||||
|
||||
## Verify interactions
|
||||
|
||||
### Response Templating
|
||||
Response headers and bodies can optionally be rendered using [Handlebars.Net](https://github.com/rexm/Handlebars.Net) templates.
|
||||
This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body. To use this functionality, add `.WithTransformer()` to the response builder.
|
||||
|
||||
Example:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.WithUrl("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Response
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "text/plain")
|
||||
.WithBody("Hello world! Your path is {{request.path}.")
|
||||
.WithTransformer()
|
||||
);
|
||||
```
|
||||
|
||||
#### The request model
|
||||
The model of the request is supplied to the header and body templates. The following request attributes are available:
|
||||
|
||||
* `request.url` - URL path and query
|
||||
* `request.path` - URL path
|
||||
* `request.path.[<n>]` - URL path segment (zero indexed) e.g. request.path.[2]
|
||||
* `request.query.<key>`- First value of a query parameter e.g. request.query.search
|
||||
* `request.query.<key>.[<n>]`- nth value of a query parameter (zero indexed) e.g. request.query.search.[5]
|
||||
* `request.headers.<key>` - First value of a request header e.g. request.headers.X-Request-Id
|
||||
* `request.headers.[<key>]` - Header with awkward characters e.g. request.headers.[$?blah]
|
||||
* `request.headers.<key>.[<n>]` - nth value of a header (zero indexed) e.g. request.headers.ManyThings.[1]
|
||||
* `request.cookies.<key>` - Value of a request cookie e.g. request.cookies.JSESSIONID
|
||||
* `request.body` - Request body text (avoid for non-text bodies)
|
||||
|
||||
##### Handlebars helpers
|
||||
All of the standard helpers (template functions) provided by the C# Handlebars implementation plus all of the string helpers are available e.g.
|
||||
`{{capitalize request.query.search}}`
|
||||
|
||||
### Stub priority
|
||||
*TODO*
|
||||
|
||||
### 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
|
||||
@@ -77,15 +100,41 @@ 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()
|
||||
);
|
||||
var customerReadRequests = server.SearchLogsFor(
|
||||
Request.WithUrl("/api/customer*").UsingGet()
|
||||
);
|
||||
```
|
||||
|
||||
# WireMock with your favourite test framework
|
||||
### 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();
|
||||
|
||||
// add a delay of 30 seconds for all requests
|
||||
server.AddRequestProcessingDelay(TimeSpan.FromSeconds(30));
|
||||
```
|
||||
|
||||
Delays can also be configured at route level:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(Request.WithUrl("/slow"))
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello I'm a little bit slow!"" }")
|
||||
.AfterDelay(TimeSpan.FromSeconds(10)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### Reset
|
||||
The WireMock server can be reset at any time, removing all stub mappings and deleting the request log. If you’re using either of the UnitTest rules this will happen automatically at the start of every test case. However you can do it yourself via a call to `server.Reset()`.
|
||||
|
||||
### Getting all currently registered stub mappings
|
||||
All stub mappings can be fetched in C# by calling `server.ListAllStubMappings()`.
|
||||
|
||||
## 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!
|
||||
@@ -102,42 +151,38 @@ public void StartMockServer()
|
||||
[Test]
|
||||
public async void Should_respond_to_request()
|
||||
{
|
||||
// given
|
||||
_sut = new SomeComponentDoingHttpCalls();
|
||||
// given
|
||||
_sut = new SomeComponentDoingHttpCalls();
|
||||
|
||||
_server
|
||||
.Given(
|
||||
Requests
|
||||
.WithUrl("/foo")
|
||||
.UsingGet())
|
||||
.RespondWith(
|
||||
Responses
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello world!"" }")
|
||||
);
|
||||
_server
|
||||
.Given(Request.WithUrl("/foo").UsingGet())
|
||||
.RespondWith(
|
||||
Response
|
||||
.WithStatusCode(200)
|
||||
.WithBody(@"{ ""msg"": ""Hello world!"" }")
|
||||
);
|
||||
|
||||
// when
|
||||
var response
|
||||
= _sut.DoSomething();
|
||||
// when
|
||||
var response = _sut.DoSomething();
|
||||
|
||||
// then
|
||||
Check.That(response).IsEqualTo(EXPECTED_RESULT);
|
||||
// and optionnaly
|
||||
Check.That(_server.SearchLogsFor(Requests.WithUrl("/error*")).IsEmpty();
|
||||
// then
|
||||
Check.That(response).IsEqualTo(EXPECTED_RESULT);
|
||||
|
||||
// and optionnaly
|
||||
Check.That(_server.SearchLogsFor(Request.WithUrl("/error*")).IsEmpty();
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
[TearDown]
|
||||
public void ShutdownServer()
|
||||
{
|
||||
_server.Stop();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
# WireMock as a standalone process
|
||||
|
||||
## 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)
|
||||
@@ -197,40 +242,13 @@ static void Main(string[] args)
|
||||
}
|
||||
```
|
||||
|
||||
# SSL
|
||||
## 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
|
||||
## 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
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
@@ -31,14 +32,14 @@ namespace WireMock
|
||||
/// </returns>
|
||||
public RequestMessage Map(HttpListenerRequest listenerRequest)
|
||||
{
|
||||
string path = listenerRequest.Url.AbsolutePath;
|
||||
string query = listenerRequest.Url.Query;
|
||||
Uri url = listenerRequest.Url;
|
||||
string verb = listenerRequest.HttpMethod;
|
||||
string body = GetRequestBody(listenerRequest);
|
||||
byte[] body = GetRequestBody(listenerRequest);
|
||||
string bodyAsString = body != null ? listenerRequest.ContentEncoding.GetString(body) : null;
|
||||
var listenerHeaders = listenerRequest.Headers;
|
||||
var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
|
||||
|
||||
return new RequestMessage(path, query, verb, body, headers);
|
||||
return new RequestMessage(url, verb, body, bodyAsString, headers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -50,20 +51,22 @@ namespace WireMock
|
||||
/// <returns>
|
||||
/// The <see cref="string"/>.
|
||||
/// </returns>
|
||||
private string GetRequestBody(HttpListenerRequest request)
|
||||
private byte[] GetRequestBody(HttpListenerRequest request)
|
||||
{
|
||||
if (!request.HasEntityBody)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using (var body = request.InputStream)
|
||||
using (var bodyStream = request.InputStream)
|
||||
{
|
||||
using (var reader = new StreamReader(body, request.ContentEncoding))
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
bodyStream.CopyTo(memoryStream);
|
||||
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,11 @@ namespace WireMock
|
||||
/// </summary>
|
||||
public class RequestBodySpec : ISpecifyRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// The bodyRegex.
|
||||
/// </summary>
|
||||
private readonly byte[] bodyData;
|
||||
|
||||
/// <summary>
|
||||
/// The bodyRegex.
|
||||
/// </summary>
|
||||
@@ -35,6 +40,11 @@ namespace WireMock
|
||||
/// </summary>
|
||||
private readonly Func<string, bool> bodyFunc;
|
||||
|
||||
/// <summary>
|
||||
/// The body data function
|
||||
/// </summary>
|
||||
private readonly Func<byte[], bool> bodyDataFunc;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
|
||||
/// </summary>
|
||||
@@ -47,6 +57,18 @@ namespace WireMock
|
||||
bodyRegex = new Regex(body);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body Regex pattern.
|
||||
/// </param>
|
||||
public RequestBodySpec([NotNull] byte[] body)
|
||||
{
|
||||
Check.NotNull(body, nameof(body));
|
||||
bodyData = body;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
|
||||
/// </summary>
|
||||
@@ -59,6 +81,18 @@ namespace WireMock
|
||||
bodyFunc = func;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
|
||||
/// </summary>
|
||||
/// <param name="func">
|
||||
/// The body func.
|
||||
/// </param>
|
||||
public RequestBodySpec([NotNull] Func<byte[], bool> func)
|
||||
{
|
||||
Check.NotNull(func, nameof(func));
|
||||
bodyDataFunc = func;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The is satisfied by.
|
||||
/// </summary>
|
||||
@@ -70,7 +104,19 @@ namespace WireMock
|
||||
/// </returns>
|
||||
public bool IsSatisfiedBy(RequestMessage requestMessage)
|
||||
{
|
||||
return bodyRegex?.IsMatch(requestMessage.Body) ?? bodyFunc(requestMessage.Body);
|
||||
if (bodyRegex != null)
|
||||
return bodyRegex.IsMatch(requestMessage.BodyAsString);
|
||||
|
||||
if (bodyData != null)
|
||||
return requestMessage.Body == bodyData;
|
||||
|
||||
if (bodyFunc != null)
|
||||
return bodyFunc(requestMessage.BodyAsString);
|
||||
|
||||
if (bodyDataFunc != null)
|
||||
return bodyDataFunc(requestMessage.Body);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,16 +17,38 @@ namespace WireMock.RequestBuilders
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
ISpecifyRequests WithBody(string body);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// The with body byte[].
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body function.
|
||||
/// The body as byte[].
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
ISpecifyRequests WithBody(byte[] body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body string func.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body string function.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
ISpecifyRequests WithBody(Func<string, bool> body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body byte[] func.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body byte[] function.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
ISpecifyRequests WithBody(Func<byte[], bool> body);
|
||||
}
|
||||
}
|
||||
@@ -210,6 +210,21 @@ namespace WireMock.RequestBuilders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body byte[].
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body as byte[].
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
public ISpecifyRequests WithBody(byte[] body)
|
||||
{
|
||||
_requestSpecs.Add(new RequestBodySpec(body));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
@@ -225,6 +240,21 @@ namespace WireMock.RequestBuilders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="func">
|
||||
/// The body function.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ISpecifyRequests"/>.
|
||||
/// </returns>
|
||||
public ISpecifyRequests WithBody(Func<byte[], bool> func)
|
||||
{
|
||||
_requestSpecs.Add(new RequestBodySpec(func));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with parameters.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Extensions;
|
||||
|
||||
[module:
|
||||
@@ -31,23 +32,31 @@ namespace WireMock
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||
/// </summary>
|
||||
/// <param name="path">
|
||||
/// The path.
|
||||
/// </param>
|
||||
/// <param name="query">
|
||||
/// The query.
|
||||
/// <param name="url">
|
||||
/// The original url.
|
||||
/// </param>
|
||||
/// <param name="verb">
|
||||
/// The verb.
|
||||
/// </param>
|
||||
/// <param name="body">
|
||||
/// The body.
|
||||
/// The body byte[].
|
||||
/// </param>
|
||||
/// <param name="bodyAsString">
|
||||
/// The body string.
|
||||
/// </param>
|
||||
/// <param name="headers">
|
||||
/// The headers.
|
||||
/// </param>
|
||||
public RequestMessage(string path, string query, string verb, string body, IDictionary<string, string> headers = null)
|
||||
public RequestMessage(Uri url, string verb, byte[] body, string bodyAsString, IDictionary<string, string> headers = null)
|
||||
{
|
||||
Url = url.ToString();
|
||||
Path = url.AbsolutePath;
|
||||
Verb = verb.ToLower();
|
||||
Body = body;
|
||||
BodyAsString = bodyAsString;
|
||||
Headers = headers;
|
||||
|
||||
string query = url.Query;
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
if (query.StartsWith("?"))
|
||||
@@ -83,28 +92,12 @@ namespace WireMock
|
||||
}
|
||||
Query = tmpDictionary.ToExpandoObject();
|
||||
}
|
||||
|
||||
Path = path;
|
||||
Headers = headers;
|
||||
Verb = verb.ToLower();
|
||||
Body = body;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url.
|
||||
/// </summary>
|
||||
public string Url
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!Parameters.Any())
|
||||
{
|
||||
return Path;
|
||||
}
|
||||
|
||||
return Path + "?" + string.Join("&", Parameters.SelectMany(kv => kv.Value.Select(value => kv.Key + "=" + value)));
|
||||
}
|
||||
}
|
||||
public string Url { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path.
|
||||
@@ -129,12 +122,18 @@ namespace WireMock
|
||||
/// <summary>
|
||||
/// Gets the query as object.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public dynamic Query { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the body.
|
||||
/// </summary>
|
||||
public string Body { get; }
|
||||
public byte[] Body { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the body.
|
||||
/// </summary>
|
||||
public string BodyAsString { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The get parameter.
|
||||
|
||||
@@ -11,20 +11,20 @@ using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||
"SA1101:PrefixLocalCallsWithThis",
|
||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||
"SA1101:PrefixLocalCallsWithThis",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
||||
SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1600:ElementsMustBeDocumented",
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1600:ElementsMustBeDocumented",
|
||||
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1633:FileMustHaveHeader",
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1633:FileMustHaveHeader",
|
||||
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
||||
// ReSharper disable ArrangeThisQualifier
|
||||
// ReSharper disable InconsistentNaming
|
||||
@@ -100,10 +100,12 @@ namespace WireMock.Net.Tests
|
||||
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar");
|
||||
|
||||
// then
|
||||
var result = _server.SearchLogsFor(Request.WithUrl("/b.*"));
|
||||
var result = _server.SearchLogsFor(Request.WithUrl("/b.*")).ToList();
|
||||
Check.That(result).HasSize(1);
|
||||
|
||||
var requestLogged = result.First();
|
||||
Check.That(requestLogged.Url).IsEqualTo("/bar");
|
||||
Check.That(requestLogged.Path).IsEqualTo("/bar");
|
||||
Check.That(requestLogged.Url).IsEqualTo("http://localhost:" + _server.Port + "/bar");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -9,23 +9,22 @@ using NUnit.Framework;
|
||||
using WireMock.Http;
|
||||
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||
"SA1101:PrefixLocalCallsWithThis",
|
||||
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
|
||||
"SA1101:PrefixLocalCallsWithThis",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
||||
SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||
"SA1309:FieldNamesMustNotBeginWithUnderscore",
|
||||
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1600:ElementsMustBeDocumented",
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1600:ElementsMustBeDocumented",
|
||||
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
|
||||
[module:
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1633:FileMustHaveHeader",
|
||||
SuppressMessage("StyleCop.CSharp.DocumentationRules",
|
||||
"SA1633:FileMustHaveHeader",
|
||||
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
|
||||
// ReSharper disable ArrangeThisQualifier
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
@@ -50,7 +49,7 @@ namespace WireMock.Net.Tests
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequestMessage).IsNotNull();
|
||||
Check.That(MapperServer.LastRequestMessage.Url).IsEqualTo("/toto");
|
||||
Check.That(MapperServer.LastRequestMessage.Path).IsEqualTo("/toto");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -78,7 +77,7 @@ namespace WireMock.Net.Tests
|
||||
|
||||
// then
|
||||
Check.That(MapperServer.LastRequestMessage).IsNotNull();
|
||||
Check.That(MapperServer.LastRequestMessage.Body).IsEqualTo("Hello!");
|
||||
Check.That(MapperServer.LastRequestMessage.BodyAsString).IsEqualTo("Hello!");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -146,7 +145,7 @@ namespace WireMock.Net.Tests
|
||||
var port = Ports.FindFreeTcpPort();
|
||||
UrlPrefix = "http://localhost:" + port + "/";
|
||||
var server = new MapperServer(
|
||||
UrlPrefix,
|
||||
UrlPrefix,
|
||||
context =>
|
||||
{
|
||||
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
|
||||
@@ -21,7 +23,9 @@ namespace WireMock.Net.Tests
|
||||
public void Should_handle_empty_query()
|
||||
{
|
||||
// given
|
||||
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(request.GetParameter("foo")).IsEmpty();
|
||||
@@ -31,7 +35,9 @@ namespace WireMock.Net.Tests
|
||||
public void Should_parse_query_params()
|
||||
{
|
||||
// given
|
||||
var request = new RequestMessage("/foo", "foo=bar&multi=1&multi=2", "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(request.GetParameter("foo")).Contains("bar");
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using NFluent;
|
||||
using NUnit.Framework;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -25,7 +27,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -38,7 +42,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo*");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -51,7 +57,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/bar", string.Empty, "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -64,7 +72,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithPath("/foo");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", "?param=1", "blabla", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -77,7 +87,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -90,7 +102,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingPost();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -103,7 +117,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingGet();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "GET", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -116,7 +132,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingDelete();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "DELETE", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -129,7 +147,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingHead();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "HEAD", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -142,7 +162,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "POST", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -155,7 +177,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/bar").UsingPut();
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string>());
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -168,7 +192,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -181,7 +207,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tatata");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "tata" } });
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -194,7 +222,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "abc", false);
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "ABC" } });
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -207,7 +237,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithHeader("X-toto", "tata*");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "whatever", new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
|
||||
string bodyAsString = "whatever";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTaTa" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -220,7 +252,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -233,7 +267,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "Hello world!", new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -246,7 +282,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(" Hello world! ");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
string bodyAsString = "xxx";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tatata" } });
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
@@ -259,7 +297,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithPath("/foo").WithParam("bar", "1", "2");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -272,7 +312,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithPath("/foo").WithParam(p => p.ContainsKey("bar") && (p["bar"].Contains("1") || p["bar"].Contains("2")));
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", "bar=1&bar=2", "Get", "Hello world!", new Dictionary<string, string>());
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
|
||||
@@ -285,7 +327,9 @@ namespace WireMock.Net.Tests
|
||||
var spec = Request.WithPath("/foo").WithParam("bar", "1");
|
||||
|
||||
// when
|
||||
var request = new RequestMessage("/foo", string.Empty, "PUT", "XXXXXXXXXXX", new Dictionary<string, string>());
|
||||
string bodyAsString = "Hello world!";
|
||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||
var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", body, bodyAsString);
|
||||
|
||||
// then
|
||||
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
|
||||
|
||||
Reference in New Issue
Block a user