SaveMappingToFile #27

This commit is contained in:
Stef Heyenrath committed 2017-05-12 22:21:55 +02:00
1 parent 23b795ab1e
commit 4ada6d3567
5 files changed
+151 -44

No files matched your search

@@ -6,10 +6,17 @@
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="__admin\mappings\826aff7c-6208-4a3c-923d-575248907db4.json" />
</ItemGroup>
<ItemGroup>
<Content Include="__admin\mappings\11111110-a633-40e8-a244-5cb80bc0ab66.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="__admin\mappings\826aff7c-6208-4a3c-923d-575248907db4.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
@@ -0,0 +1,41 @@
{
"Guid": "826aff7c-6208-4a3c-923d-575248907db4",
"Priority": 0,
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/earth/story/20170510-terrifying-20m-tall-rogue-waves-are-actually-real"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"Body": " <!DOCTYPE html> <html class=\" b-pw-1280\" lang=\"en\" > <head> <!-- Barlesque 3.21.13 --> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /> <meta name=\"description\" content=\"Breaking news, sport, TV, radio and a whole lot more. The BBC informs, educates and entertains - wherever you are, whatever your age.\" /> <meta name=\"keywords\" content=\"BBC, bbc.co.uk, bbc.com, Search, British Broadcasting Corporation, BBC iPlayer, BBCi\" /> <title>BBC - Homepage</title> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /> <meta property=\"fb:admins\" content=\"100004154058350\" /> <script type=\"text/javascript\">window.bbcredirection={geo:true}</script> \n<!--[if (gt IE 8) | (IEMobile)]><!-->\n<link rel=\"stylesheet\" href=\"http://static.bbci.co.uk/frameworks/barlesque/3.21.13/orb/4/style/orb.min.css\">\n<!--<![endif]-->\n\n<!--[if (lt IE 9) & (!IEMobile)]>\n<link rel=\"stylesheet\" href=\"http://static.bbci.co.uk/frameworks/barlesque/3.21.13/orb/4/style/orb-ie.min.css\">\n<![endif]-->\n\n <!--orb.ws.require.lib--> <script class=\"js-require-lib\" src=\"http://static.bbci.co.uk/frameworks/requirejs/lib.js\"></script> <script type=\"text/javascript\"> bbcRequireMap = {\"jquery-1\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.7.2\", \"jquery-1.4\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.4\", \"jquery-1.9\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.9.1\", \"jquery-1.12\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.12.0.min\", \"jquery-2.2\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-2.2.0.min\", \"istats-1\":\"//nav.files.bbci.co.uk/nav-analytics/0.1.0-43/js/istats-1\", \"swfobject-2\":\"http://static.bbci.co.uk/frameworks/swfobject/0.1.10/sharedmodules/swfobject-2\", \"demi-1\":\"http://static.bbci.co.uk/frameworks/demi/0.10.1/sharedmodules/demi-1\", \"gelui-1\":\"http://static.bbci.co.uk/frameworks/gelui/0.9.13/sharedmodules/gelui-1\", \"cssp!gelui-1/overlay\":\"http://static.bbci.co.uk/frameworks/gelui/0.9.13/sharedmodules/gelui-1/overlay.css\", \"relay-1\":\"http://static.bbci.co.uk/frameworks/relay/0.2.6/sharedmodules/relay-1\", \"clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/clock-1\", \"canvas-clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/canvas-clock-1\", \"cssp!clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/clock-1.css\", \"jssignals-1\":\"http://static.bbci.co.uk/frameworks/jssignals/0.3.6/modules/jssignals-1\", \"jcarousel-1\":\"http://static.bbci.co.uk/frameworks/jcarousel/0.1.10/modules/jcarousel-1\", \"bump-3\":\"//emp.bbci.co.uk/emp/bump-3/bump-3\", \"ads\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/ads\", \"app\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/app\", \"compiled\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/compiled\", \"homepage\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/homepage\", \"lib/core\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/core\", \"lib/module/base\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/module/base\", \"lib/module/manager\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/module/manager\", \"lib/timeInterval\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/timeInterval\", \"lib/util\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/util\", \"modules/header\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/header\", \"modules/images\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/images\", \"modules/media\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/media\", \"modules/video\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video\", \"modules/video/dataProvider\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/dataProvider\", \"modules/video/player\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/player\", \"modules/video/playlist\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/playlist\", \"modules/video/playlistBuilder\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/playlistBuilder\", \"modules/weather\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/weather\", \"vendor/pre-built/bbc-video-experience/continuousPlay/module\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/vendor/pre-built/bbc-video-experience/continuousPlay/module\", \"vendor/pre-built/vwo/mvt-snippet\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/vendor/pre-built/vwo/mvt-snippet\"}; require({ baseUrl: 'http://static.bbci.co.uk/', paths: bbcRequireMap, waitSeconds: 30 }); </script> <script type=\"text/javascript\">/*<![CDATA[*/ if (typeof bbccookies_flag === 'undefined') { bbccookies_flag = 'ON'; } showCTA_flag = true; cta_enabled = (showCTA_flag && (bbccookies_flag === 'ON')); (function(){var m=\"ckns_policy\",q=\"Thu, 01 Jan 1Line truncated
"BodyEncoding": {
"CodePage": 65001,
"EncodingName": "Unicode (UTF-8)",
"WebName": "utf-8"
},
"UseTransformer": false,
"Headers": {
"Set-Cookie": "BBC-UID=d5e9e11601d6a92baa56889831b1cedf3e0040fa733856b32b0ba4234e28e4740PostmanRuntime/3.0.11-hotfix.2; expires=Tue, 11-May-21 20:10:03 GMT; path=/; domain=.bbc.com",
"Cache-Control": "max-age=60, private",
"Vary": "X-CDN",
"X-Cache-Action": "HIT",
"Date": "Fri, 12 May 2017 20:10:03 GMT",
"ETag": "\"777c5af8426c21563a57166e437d820a\"",
"X-PAL-Host": "pal010.back.live.cwwtf.local:80",
"Connection": "keep-alive",
"X-Cache-Hits": "5",
"Server": "Apache",
"X-Cache-Age": "37",
"X-LB-NoCache": "true"
}
}
}
@@ -0,0 +1,41 @@
{
"Guid": "ab38efae-4e4d-4f20-8afe-635533ec2535",
"Title": "",
"Priority": 0,
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/earth/story/20170510-terrifying-20m-tall-rogue-waves-are-actually-real"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"Body": " <!DOCTYPE html> <html class=\" b-pw-1280\" lang=\"en\" > <head> <!-- Barlesque 3.21.13 --> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /> <meta name=\"description\" content=\"Breaking news, sport, TV, radio and a whole lot more. The BBC informs, educates and entertains - wherever you are, whatever your age.\" /> <meta name=\"keywords\" content=\"BBC, bbc.co.uk, bbc.com, Search, British Broadcasting Corporation, BBC iPlayer, BBCi\" /> <title>BBC - Homepage</title> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /> <meta property=\"fb:admins\" content=\"100004154058350\" /> <script type=\"text/javascript\">window.bbcredirection={geo:true}</script> \n<!--[if (gt IE 8) | (IEMobile)]><!-->\n<link rel=\"stylesheet\" href=\"http://static.bbci.co.uk/frameworks/barlesque/3.21.13/orb/4/style/orb.min.css\">\n<!--<![endif]-->\n\n<!--[if (lt IE 9) & (!IEMobile)]>\n<link rel=\"stylesheet\" href=\"http://static.bbci.co.uk/frameworks/barlesque/3.21.13/orb/4/style/orb-ie.min.css\">\n<![endif]-->\n\n <!--orb.ws.require.lib--> <script class=\"js-require-lib\" src=\"http://static.bbci.co.uk/frameworks/requirejs/lib.js\"></script> <script type=\"text/javascript\"> bbcRequireMap = {\"jquery-1\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.7.2\", \"jquery-1.4\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.4\", \"jquery-1.9\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.9.1\", \"jquery-1.12\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-1.12.0.min\", \"jquery-2.2\":\"http://static.bbci.co.uk/frameworks/jquery/0.4.1/sharedmodules/jquery-2.2.0.min\", \"istats-1\":\"//nav.files.bbci.co.uk/nav-analytics/0.1.0-43/js/istats-1\", \"swfobject-2\":\"http://static.bbci.co.uk/frameworks/swfobject/0.1.10/sharedmodules/swfobject-2\", \"demi-1\":\"http://static.bbci.co.uk/frameworks/demi/0.10.1/sharedmodules/demi-1\", \"gelui-1\":\"http://static.bbci.co.uk/frameworks/gelui/0.9.13/sharedmodules/gelui-1\", \"cssp!gelui-1/overlay\":\"http://static.bbci.co.uk/frameworks/gelui/0.9.13/sharedmodules/gelui-1/overlay.css\", \"relay-1\":\"http://static.bbci.co.uk/frameworks/relay/0.2.6/sharedmodules/relay-1\", \"clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/clock-1\", \"canvas-clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/canvas-clock-1\", \"cssp!clock-1\":\"http://static.bbci.co.uk/frameworks/clock/0.1.9/sharedmodules/clock-1.css\", \"jssignals-1\":\"http://static.bbci.co.uk/frameworks/jssignals/0.3.6/modules/jssignals-1\", \"jcarousel-1\":\"http://static.bbci.co.uk/frameworks/jcarousel/0.1.10/modules/jcarousel-1\", \"bump-3\":\"//emp.bbci.co.uk/emp/bump-3/bump-3\", \"ads\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/ads\", \"app\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/app\", \"compiled\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/compiled\", \"homepage\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/homepage\", \"lib/core\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/core\", \"lib/module/base\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/module/base\", \"lib/module/manager\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/module/manager\", \"lib/timeInterval\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/timeInterval\", \"lib/util\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/lib/util\", \"modules/header\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/header\", \"modules/images\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/images\", \"modules/media\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/media\", \"modules/video\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video\", \"modules/video/dataProvider\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/dataProvider\", \"modules/video/player\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/player\", \"modules/video/playlist\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/playlist\", \"modules/video/playlistBuilder\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/video/playlistBuilder\", \"modules/weather\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/modules/weather\", \"vendor/pre-built/bbc-video-experience/continuousPlay/module\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/vendor/pre-built/bbc-video-experience/continuousPlay/module\", \"vendor/pre-built/vwo/mvt-snippet\":\"http://static.bbci.co.uk/wwhp/1.114.0/modules/vendor/pre-built/vwo/mvt-snippet\"}; require({ baseUrl: 'http://static.bbci.co.uk/', paths: bbcRequireMap, waitSeconds: 30 }); </script> <script type=\"text/javascript\">/*<![CDATA[*/ if (typeof bbccookies_flag === 'undefined') { bbccookies_flag = 'ON'; } showCTA_flag = true; cta_enabled = (showCTA_flag && (bbccookies_flag === 'ON')); (function(){var m=\"ckns_policy\",q=\"Thu, 01 Jan 1Line truncated
"BodyEncoding": {
"CodePage": 65001,
"EncodingName": "Unicode (UTF-8)",
"WebName": "utf-8"
},
"UseTransformer": false,
"Headers": {
"X-LB-NoCache": "true",
"X-Cache-Age": "0",
"ETag": "\"07931511980b88249a64e043d5f5341d\"",
"Connection": "keep-alive",
"Cache-Control": "max-age=60, private",
"X-PAL-Host": "pal128.back.live.telhc.local:80",
"Date": "Fri, 12 May 2017 20:20:25 GMT",
"Server": "Apache",
"Vary": "X-CDN",
"Set-Cookie": "BBC-UID=c58921a6d13960c8991318b121db82a089b7948f332826838beba493b8b8c4880PostmanRuntime/3.0.11-hotfix.2; expires=Tue, 11-May-21 20:20:25 GMT; path=/; domain=.bbc.com",
"X-Cache-Action": "MISS"
}
}
}
+34 -35
View File
@@ -7,47 +7,46 @@ namespace WireMock.Http
{
internal static class HttpClientHelper
{
private static HttpClient client = new HttpClient();
public static async Task<ResponseMessage> SendAsync(RequestMessage requestMessage, string url)
{
using (var client = new HttpClient())
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
// Overwrite the host header
httpRequestMessage.Headers.Host = new Uri(url).Authority;
// Set headers if present
if (requestMessage.Headers != null)
{
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
// Overwrite the host header
httpRequestMessage.Headers.Host = new Uri(url).Authority;
// Set headers if present
if (requestMessage.Headers != null)
foreach (var headerName in requestMessage.Headers.Keys.Where(k => k.ToUpper() != "HOST"))
{
foreach (var headerName in requestMessage.Headers.Keys.Where(k => k.ToUpper() != "HOST"))
{
httpRequestMessage.Headers.Add(headerName, new[] { requestMessage.Headers[headerName] });
}
httpRequestMessage.Headers.Add(headerName, new[] { requestMessage.Headers[headerName] });
}
// Set Body if present
if (requestMessage.BodyAsBytes != null && requestMessage.BodyAsBytes.Length > 0)
{
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);
}
// Call the URL
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
// Transform response
var responseMessage = new ResponseMessage
{
StatusCode = (int)httpResponseMessage.StatusCode,
Body = await httpResponseMessage.Content.ReadAsStringAsync()
};
foreach (var header in httpResponseMessage.Headers)
{
responseMessage.AddHeader(header.Key, header.Value.FirstOrDefault());
}
return responseMessage;
}
// Set Body if present
if (requestMessage.BodyAsBytes != null && requestMessage.BodyAsBytes.Length > 0)
{
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);
}
// Call the URL
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
// Transform response
var responseMessage = new ResponseMessage
{
StatusCode = (int)httpResponseMessage.StatusCode,
Body = await httpResponseMessage.Content.ReadAsStringAsync()
};
foreach (var header in httpResponseMessage.Headers)
{
responseMessage.AddHeader(header.Key, header.Value.FirstOrDefault());
}
return responseMessage;
}
}
}
@@ -130,8 +130,22 @@ namespace WireMock.Server
{
var responseMessage = await HttpClientHelper.SendAsync(requestMessage, settings.Url);
var mapping = ToMapping(requestMessage, responseMessage);
SaveMappingToFile(mapping);
return responseMessage;
}
private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage)
{
var request = (Request)Request.Create();
request.WithPath(requestMessage.Path);
request.UsingVerb(requestMessage.Method);
var response = (Response)Response.Create(responseMessage);
return new Mapping(Guid.NewGuid(), string.Empty, request, response, 0);
}
#endregion
#region Settings
@@ -211,21 +225,26 @@ namespace WireMock.Server
#region Mappings
private ResponseMessage MappingsSave(RequestMessage requestMessage)
{
foreach (var mapping in Mappings.Where(m => !m.IsAdminInterface))
{
SaveMappingToFile(mapping);
}
return new ResponseMessage { Body = "Mappings saved to disk" };
}
private void SaveMappingToFile(Mapping mapping)
{
string folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder);
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
foreach (var mapping in Mappings.Where(m => !m.IsAdminInterface))
{
var model = ToMappingModel(mapping);
string json = JsonConvert.SerializeObject(model, _settings);
string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString();
var model = ToMappingModel(mapping);
string json = JsonConvert.SerializeObject(model, _settings);
string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString();
File.WriteAllText(Path.Combine(folder, filename + ".json"), json);
}
return new ResponseMessage { Body = "Mappings saved to disk" };
File.WriteAllText(Path.Combine(folder, filename + ".json"), json);
}
private static string SanitizeFileName(string name, char replaceChar = '_')