-
-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I build mappings with code and save them to JSON-file without starting server #867
Comments
@Eshva Use it like: var mappingBuilder = new MappingBuilder();
mappingBuilder
.Given(Request
.Create()
.WithPath(new WildcardMatcher("/param2", true))
.WithParam("key", "test")
.UsingGet())
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new { result = "param2" }));
var json = mappingBuilder.ToJson(); |
@Eshva Did you have time to test that preview version? |
Sorry for late response. Not yet. New year time, I was busy, but now we have long holydays so I will test it. I will be back soon with review.
|
I've tasted it with a test: public class given_mapping_builder
{
[Fact]
public void when_build_mapping_in_code_it_should_produce_expected_json()
{
var searchResultForFelino = ReadSubject("SearchResultForFelino.json");
var responseHeaders = JsonSerializer.Deserialize<Dictionary<string, string>>(ReadSubject("SearchResultHeaders.json"));
var builder = new MappingBuilder();
builder.Given(
Request.Create()
.UsingGet()
.WithPath("/locations/v3/search")
.WithParam("q", "Felino")
.WithParam("langid", "1033")
.WithHeader("X-RapidAPI-Key", new WildcardMatcher("*"))
.WithHeader("X-RapidAPI-Host", "hotels4.p.rapidapi.com"))
.WithTitle("Search for a location")
.RespondWith(
Response.Create()
.WithBody(searchResultForFelino)
.WithHeaders(responseHeaders!));
var mapping = builder.ToJson();
mapping.Should().BeValidJson();
}
private string ReadSubject(string subjectFileName) => File.ReadAllText(Path.Combine(_outputFolderPath, "Subjects", subjectFileName));
private readonly string _outputFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
} The result doesn't contain the request specification, it's empty. [
{
"Guid": "75e8fa25-1009-4045-adad-e33b22a4f7d0",
"UpdatedAt": "2023-01-03T00:40:57.0763261Z",
"Title": "Search for a location",
"Priority": 0,
"StateTimes": 1,
"RequestMatcher": {},
"Provider": {
"UseTransformer": false,
"TransformerType": 0,
"UseTransformerForBodyAsFile": false,
"TransformerReplaceNodeOptions": 0,
"ResponseMessage": {
"Headers": {
"access-control-allow-credentials": [
"true"
],
"access-control-allow-headers": [
"ver"
],
"access-control-allow-methods": [
"GET, POST"
],
"access-control-allow-origin": [
"*"
],
"content-length": [
"2805"
],
"content-type": [
"application/json"
],
"date": [
"Tue, 03 Jan 2023 00:02:55 GMT"
]
},
"BodyDestination": "SameAsSource",
"BodyData": {
"Encoding": {
"BodyName": "utf-8",
"EncodingName": "Unicode (UTF-8)",
"HeaderName": "utf-8",
"WebName": "utf-8",
"WindowsCodePage": 1200,
"IsBrowserDisplay": true,
"IsBrowserSave": true,
"IsMailNewsDisplay": true,
"IsMailNewsSave": true,
"IsSingleByte": false,
"EncoderFallback": {
"DefaultString": "�",
"MaxCharCount": 1
},
"DecoderFallback": {
"DefaultString": "�",
"MaxCharCount": 1
},
"IsReadOnly": true,
"CodePage": 65001
},
"BodyAsString": "{\n \"q\": \"felino\",\n \"rid\": \"27c495e8bde54f23afd1c6ccbdcb4b4a\",\n \"rc\": \"OK\",\n \"sr\": [\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"0\",\n \"gaiaId\": \"6315061\",\n \"type\": \"POI\",\n \"regionNames\": {\n \"fullName\": \"Felino Salami Museum, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Felino Salami Museum\",\n \"displayName\": \"Felino Salami Museum, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Felino Salami Museum\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Felino Salami Museum\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"6315061\"\n },\n \"coordinates\": {\n \"lat\": \"44.68496\",\n \"long\": \"10.23605\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"1\",\n \"gaiaId\": \"6343245\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Felino\",\n \"displayName\": \"Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Felino\",\n \"secondaryDisplayName\": \"Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Felino\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"6343245\"\n },\n \"coordinates\": {\n \"lat\": \"44.680509\",\n \"long\": \"10.239658\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"2\",\n \"gaiaId\": \"553248621532498117\",\n \"type\": \"POI\",\n \"regionNames\": {\n \"fullName\": \"Museo del Ciclismo, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Museo del Ciclismo\",\n \"displayName\": \"Museo del Ciclismo, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Museo del Ciclismo\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Museo del Ciclismo\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248621532498117\"\n },\n \"coordinates\": {\n \"lat\": \"44.69545\",\n \"long\": \"10.23515\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"3\",\n \"gaiaId\": \"553248635149841736\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Poggio, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Poggio\",\n \"displayName\": \"Poggio, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Poggio\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Poggio\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248635149841736\"\n },\n \"coordinates\": {\n \"lat\": \"44.660066\",\n \"long\": \"10.196403\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"4\",\n \"gaiaId\": \"553248635975850278\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Barbiano, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Barbiano\",\n \"displayName\": \"Barbiano, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Barbiano\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Barbiano\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248635975850278\"\n },\n \"coordinates\": {\n \"lat\": \"44.66830204316714\",\n \"long\": \"10.238364542381836\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n }\n ]\n}\n",
"DetectedBodyType": 1
},
"FaultType": 0
},
"WithCallbackUsed": false
},
"Settings": {
"StartTimeout": 10000,
"CustomCertificateDefined": false,
"ClientCertificateMode": 0,
"AcceptAnyClientCertificate": false,
"UseRegexExtended": true,
"Culture": "ru-RU"
},
"IsStartState": true,
"IsAdminInterface": false,
"IsProxy": false,
"LogMapping": true,
"UseWebhooksFireAndForget": false
}
] |
@Eshva This is very strange behavior: the json you see is not the MappingModel json but the internal json. Can you please retry preview version |
See also my unit test : https://github.com/WireMock-Net/WireMock.Net/blob/mappng-builder/test/WireMock.Net.Tests/MappingBuilderTests.cs#L59 Which returns: [
{
"Guid": "41372914-1838-4c67-916b-b9aacdd096ce",
"UpdatedAt": "2023-01-04T07:50:18.7825017Z",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/foo",
"IgnoreCase": false
}
]
},
"Methods": [
"GET"
]
},
"Response": {
"BodyDestination": "SameAsSource",
"Body": "{ msg: \"Hello world!\"}"
},
"UseWebhooksFireAndForget": false
}
] |
With Thank you! [
{
"Guid": "8d61eee3-3ba3-4b6d-844f-c3f1e40195bf",
"UpdatedAt": "2023-01-05T00:28:39.4918461Z",
"Title": "Search for a location",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/locations/v3/search",
"IgnoreCase": false
}
]
},
"Methods": [
"GET"
],
"Headers": [
{
"Name": "X-RapidAPI-Key",
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "*",
"IgnoreCase": false
}
]
},
{
"Name": "X-RapidAPI-Host",
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "hotels4.p.rapidapi.com",
"IgnoreCase": true
}
]
}
],
"Params": [
{
"Name": "q",
"Matchers": [
{
"Name": "ExactMatcher",
"Pattern": "Felino",
"IgnoreCase": false
}
]
},
{
"Name": "langid",
"Matchers": [
{
"Name": "ExactMatcher",
"Pattern": "1033",
"IgnoreCase": false
}
]
}
]
},
"Response": {
"BodyDestination": "SameAsSource",
"Body": "{\n \"q\": \"felino\",\n \"rid\": \"27c495e8bde54f23afd1c6ccbdcb4b4a\",\n \"rc\": \"OK\",\n \"sr\": [\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"0\",\n \"gaiaId\": \"6315061\",\n \"type\": \"POI\",\n \"regionNames\": {\n \"fullName\": \"Felino Salami Museum, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Felino Salami Museum\",\n \"displayName\": \"Felino Salami Museum, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Felino Salami Museum\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Felino Salami Museum\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"6315061\"\n },\n \"coordinates\": {\n \"lat\": \"44.68496\",\n \"long\": \"10.23605\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"1\",\n \"gaiaId\": \"6343245\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Felino\",\n \"displayName\": \"Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Felino\",\n \"secondaryDisplayName\": \"Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Felino\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"6343245\"\n },\n \"coordinates\": {\n \"lat\": \"44.680509\",\n \"long\": \"10.239658\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"2\",\n \"gaiaId\": \"553248621532498117\",\n \"type\": \"POI\",\n \"regionNames\": {\n \"fullName\": \"Museo del Ciclismo, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Museo del Ciclismo\",\n \"displayName\": \"Museo del Ciclismo, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Museo del Ciclismo\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Museo del Ciclismo\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248621532498117\"\n },\n \"coordinates\": {\n \"lat\": \"44.69545\",\n \"long\": \"10.23515\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"3\",\n \"gaiaId\": \"553248635149841736\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Poggio, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Poggio\",\n \"displayName\": \"Poggio, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Poggio\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Poggio\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248635149841736\"\n },\n \"coordinates\": {\n \"lat\": \"44.660066\",\n \"long\": \"10.196403\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n },\n {\n \"@type\": \"gaiaRegionResult\",\n \"index\": \"4\",\n \"gaiaId\": \"553248635975850278\",\n \"type\": \"CITY\",\n \"regionNames\": {\n \"fullName\": \"Barbiano, Felino, Emilia-Romagna, Italy\",\n \"shortName\": \"Barbiano\",\n \"displayName\": \"Barbiano, Felino, Emilia-Romagna, Italy\",\n \"primaryDisplayName\": \"Barbiano\",\n \"secondaryDisplayName\": \"Felino, Emilia-Romagna, Italy\",\n \"lastSearchName\": \"Barbiano\"\n },\n \"essId\": {\n \"sourceName\": \"GAI\",\n \"sourceId\": \"553248635975850278\"\n },\n \"coordinates\": {\n \"lat\": \"44.66830204316714\",\n \"long\": \"10.238364542381836\"\n },\n \"hierarchyInfo\": {\n \"country\": {\n \"name\": \"Italy\",\n \"isoCode2\": \"IT\",\n \"isoCode3\": \"ITA\"\n }\n }\n }\n ]\n}\n",
"Headers": {
"access-control-allow-credentials": "true",
"access-control-allow-headers": "ver",
"access-control-allow-methods": "GET, POST",
"access-control-allow-origin": "*",
"content-length": "2805",
"content-type": "application/json",
"date": "Tue, 03 Jan 2023 00:02:55 GMT"
}
},
"UseWebhooksFireAndForget": false
}
] |
@Eshva Thanks for testing. I'll merge this change to main branch. |
I want prepare mappings with code, save them as JSON-files and use them latter as static mappings. Currently I've found such a way to do it:
It works but I have to start a server. Can I do it without starting a server somehow?
The text was updated successfully, but these errors were encountered: