mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 15:23:47 +01:00
* added byte[] body
* updated readme
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user