Skip to content
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

Closed
Eshva opened this issue Dec 21, 2022 · 9 comments
Assignees
Labels

Comments

@Eshva
Copy link

Eshva commented Dec 21, 2022

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:

    private async Task CreateAndSaveMappings()
    {
        using var server = WireMockServer.Start(9999);
        server.Given(Request.Create().WithPath("/api/client").UsingGet())
            .RespondWith(Response.Create()
                .WithStatusCode(HttpStatusCode.OK)
                .WithHeader("Content-Type", "text/plain")
                .WithBody("client 1"));
        server.Given(Request.Create().WithPath("/api/client/2").UsingGet())
            .RespondWith(Response.Create()
                .WithStatusCode(HttpStatusCode.OK)
                .WithHeader("Content-Type", "text/plain")
                .WithBody("client 2"));

        foreach (var mappingModel in server.MappingModels)
        {
            var mappingJson = JsonConvert.SerializeObject(mappingModel);
            await File.WriteAllTextAsync(Path.Combine(_sourceMappingsFolderPath, mappingModel.Guid.ToString()),
                mappingJson);
        }

        server.Stop();
    }

It works but I have to start a server. Can I do it without starting a server somehow?

@StefH StefH transferred this issue from WireMock-Net/WireMock.Net-docker Dec 21, 2022
@StefH StefH self-assigned this Dec 22, 2022
@StefH StefH added the feature label Dec 22, 2022
@StefH
Copy link
Collaborator

StefH commented Dec 22, 2022

@Eshva
Can you try preview 1.5.13-ci-16878 ? (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

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();

@StefH
Copy link
Collaborator

StefH commented Dec 23, 2022

#869

@StefH
Copy link
Collaborator

StefH commented Dec 31, 2022

@Eshva Did you have time to test that preview version?

@Eshva
Copy link
Author

Eshva commented Jan 2, 2023 via email

@Eshva
Copy link
Author

Eshva commented Jan 3, 2023

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
  }
]

@StefH
Copy link
Collaborator

StefH commented Jan 4, 2023

@Eshva
Thank you for testing.

This is very strange behavior: the json you see is not the MappingModel json but the internal json.

Can you please retry preview version 1.5.13-ci-16899 ?

@StefH
Copy link
Collaborator

StefH commented Jan 4, 2023

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
  }
]

@Eshva
Copy link
Author

Eshva commented Jan 5, 2023

With 1.5.13-ci-16899 version it works as required. Before I used 1.5.13-ci-16878 version as you suggested.

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
  }
]

@StefH
Copy link
Collaborator

StefH commented Jan 6, 2023

@Eshva Thanks for testing. I'll merge this change to main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants