mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 16:01:39 +02:00
Update ProxyAndRecord
ProxyAndRecord does not save query-parameters, headers and body (#57)
This commit is contained in:
@@ -12,12 +12,13 @@ namespace WireMock.Net.Console.Record.NETCoreApp
|
|||||||
{
|
{
|
||||||
Urls = new[] { "http://localhost:9091/", "https://localhost:9443/" },
|
Urls = new[] { "http://localhost:9091/", "https://localhost:9443/" },
|
||||||
StartAdminInterface = true,
|
StartAdminInterface = true,
|
||||||
ReadStaticMappings = true,
|
ReadStaticMappings = false,
|
||||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
{
|
{
|
||||||
Url = "https://www.google.com",
|
Url = "https://www.google.com",
|
||||||
//X509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)",
|
//X509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)",
|
||||||
SaveMapping = true
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ namespace WireMock.Net.StandAlone
|
|||||||
{
|
{
|
||||||
Url = proxyURL,
|
Url = proxyURL,
|
||||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||||
|
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||||
X509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("X509Certificate2ThumbprintOrSubjectName")
|
X509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("X509Certificate2ThumbprintOrSubjectName")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,25 +13,16 @@ namespace WireMock
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the unique identifier.
|
/// Gets the unique identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
|
||||||
/// The unique identifier.
|
|
||||||
/// </value>
|
|
||||||
public Guid Guid { get; }
|
public Guid Guid { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the unique title.
|
/// Gets the unique title.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
|
||||||
/// The unique title.
|
|
||||||
/// </value>
|
|
||||||
public string Title { get; }
|
public string Title { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the priority.
|
/// Gets the priority.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
|
||||||
/// The priority.
|
|
||||||
/// </value>
|
|
||||||
public int Priority { get; }
|
public int Priority { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ namespace WireMock.Serialization
|
|||||||
Funcs = Map(pm.Funcs)
|
Funcs = Map(pm.Funcs)
|
||||||
}).ToList() : null,
|
}).ToList() : null,
|
||||||
|
|
||||||
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Count(m => m == "get") == 1 ? null : new BodyModel
|
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
|
||||||
{
|
{
|
||||||
Matcher = bodyMatcher != null ? Map(bodyMatcher.Matcher) : null,
|
Matcher = bodyMatcher != null ? Map(bodyMatcher.Matcher) : null,
|
||||||
Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
|
Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
|
||||||
@@ -115,14 +115,16 @@ namespace WireMock.Serialization
|
|||||||
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
|
mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
|
||||||
mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached;
|
mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached;
|
||||||
mappingModel.Response.UseTransformer = response.UseTransformer;
|
mappingModel.Response.UseTransformer = response.UseTransformer;
|
||||||
mappingModel.Response.BodyEncoding = response.ResponseMessage.BodyEncoding != null
|
|
||||||
? new EncodingModel
|
if (response.ResponseMessage.BodyEncoding != null)
|
||||||
|
{
|
||||||
|
mappingModel.Response.BodyEncoding = new EncodingModel
|
||||||
{
|
{
|
||||||
EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
|
EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
|
||||||
CodePage = response.ResponseMessage.BodyEncoding.CodePage,
|
CodePage = response.ResponseMessage.BodyEncoding.CodePage,
|
||||||
WebName = response.ResponseMessage.BodyEncoding.WebName
|
WebName = response.ResponseMessage.BodyEncoding.WebName
|
||||||
}
|
};
|
||||||
: null;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappingModel;
|
return mappingModel;
|
||||||
|
|||||||
@@ -130,11 +130,11 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region Proxy and Record
|
#region Proxy and Record
|
||||||
private HttpClient httpClientForProxy;
|
private HttpClient _httpClientForProxy;
|
||||||
|
|
||||||
private void InitProxyAndRecord(ProxyAndRecordSettings settings)
|
private void InitProxyAndRecord(ProxyAndRecordSettings settings)
|
||||||
{
|
{
|
||||||
httpClientForProxy = HttpClientHelper.CreateHttpClient(settings.X509Certificate2ThumbprintOrSubjectName);
|
_httpClientForProxy = HttpClientHelper.CreateHttpClient(settings.X509Certificate2ThumbprintOrSubjectName);
|
||||||
Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
|
Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,12 +144,17 @@ namespace WireMock.Server
|
|||||||
var proxyUri = new Uri(settings.Url);
|
var proxyUri = new Uri(settings.Url);
|
||||||
var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
|
var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
|
||||||
|
|
||||||
var responseMessage = await HttpClientHelper.SendAsync(httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri);
|
var responseMessage = await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri);
|
||||||
|
|
||||||
if (settings.SaveMapping)
|
if (settings.SaveMapping)
|
||||||
{
|
{
|
||||||
var mapping = ToMapping(requestMessage, responseMessage);
|
var mapping = ToMapping(requestMessage, responseMessage);
|
||||||
SaveMappingToFile(mapping);
|
_options.Mappings.Add(mapping);
|
||||||
|
|
||||||
|
if (settings.SaveMappingToFile)
|
||||||
|
{
|
||||||
|
SaveMappingToFile(mapping);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return responseMessage;
|
return responseMessage;
|
||||||
@@ -157,11 +162,20 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage)
|
private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage)
|
||||||
{
|
{
|
||||||
var request = (Request)Request.Create();
|
var request = Request.Create();
|
||||||
request.WithPath(requestMessage.Path);
|
request.WithPath(requestMessage.Path);
|
||||||
request.UsingVerb(requestMessage.Method);
|
request.UsingVerb(requestMessage.Method);
|
||||||
|
|
||||||
var response = (Response)Response.Create(responseMessage);
|
requestMessage.Query.Loop((key, value) => request.WithParam(key, value.ToArray()));
|
||||||
|
requestMessage.Headers.Loop((key, value) => request.WithHeader(key, new ExactMatcher(value.ToArray())));
|
||||||
|
requestMessage.Cookies.Loop((key, value) => request.WithCookie(key, new ExactMatcher(value)));
|
||||||
|
|
||||||
|
if (requestMessage.Body != null)
|
||||||
|
{
|
||||||
|
request.WithBody(new ExactMatcher(requestMessage.Body));
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = Response.Create(responseMessage);
|
||||||
|
|
||||||
return new Mapping(Guid.NewGuid(), string.Empty, request, response, 0, null, null, null);
|
return new Mapping(Guid.NewGuid(), string.Empty, request, response, 0, null, null, null);
|
||||||
}
|
}
|
||||||
@@ -530,8 +544,9 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
|
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
|
||||||
if (pathModel?.Matchers != null)
|
if (pathModel?.Matchers != null)
|
||||||
requestBuilder =
|
{
|
||||||
requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).ToArray());
|
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(MappingConverter.Map).ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,8 +561,9 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
|
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
|
||||||
if (urlModel?.Matchers != null)
|
if (urlModel?.Matchers != null)
|
||||||
requestBuilder =
|
{
|
||||||
requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).ToArray());
|
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(MappingConverter.Map).ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,10 +11,15 @@
|
|||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Save the mapping for each request/response.
|
/// Save the mapping for each request/response to the internal Mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool SaveMapping { get; set; } = true;
|
public bool SaveMapping { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save the mapping for each request/response to also file. (Note that SaveMapping must also be set to true.)
|
||||||
|
/// </summary>
|
||||||
|
public bool SaveMappingToFile { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
33
src/WireMock.Net/Util/DictionaryExtensions.cs
Normal file
33
src/WireMock.Net/Util/DictionaryExtensions.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
|
namespace WireMock.Util
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Some IDictionary Extensions
|
||||||
|
/// </summary>
|
||||||
|
public static class DictionaryExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Loops the dictionary and executes the specified action.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TKey">The type of the key.</typeparam>
|
||||||
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
||||||
|
/// <param name="dictionary">The dictionary to loop (can be null).</param>
|
||||||
|
/// <param name="action">The action.</param>
|
||||||
|
public static void Loop<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] Action<TKey, TValue> action)
|
||||||
|
{
|
||||||
|
Check.NotNull(action, nameof(action));
|
||||||
|
|
||||||
|
if (dictionary != null)
|
||||||
|
{
|
||||||
|
foreach (var entry in dictionary)
|
||||||
|
{
|
||||||
|
action(entry.Key, entry.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user