Can I build mappings with code and save them to JSON-file without starting server #480

Closed
opened 2025-12-29 15:24:46 +01:00 by adam · 2 comments
Owner

Originally created by @Eshva on GitHub (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?

Originally created by @Eshva on GitHub (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: ```csharp 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?
adam added the question label 2025-12-29 15:24:46 +01:00
adam closed this issue 2025-12-29 15:24:46 +01:00
Author
Owner

@StefH commented on GitHub (Dec 21, 2022):

I could maybe expose some internal logic to do this?

@StefH commented on GitHub (Dec 21, 2022): I could maybe expose some internal logic to do this?
Author
Owner

@Eshva commented on GitHub (Dec 22, 2022):

Yes, I think so. It makes the library more robust and versatile. In my mind
the Server class is responsible for too many things.

Stef Heyenrath @.***> 21 декабря 2022 г. 6:32:05 PM
написал:

I could maybe expose some internal logic to do this?

Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you modified the open/close state.Message
ID: @.***>

@Eshva commented on GitHub (Dec 22, 2022): Yes, I think so. It makes the library more robust and versatile. In my mind the Server class is responsible for too many things. Stef Heyenrath ***@***.***> 21 декабря 2022 г. 6:32:05 PM написал: > > I could maybe expose some internal logic to do this? > — > Reply to this email directly, view it on GitHub, or unsubscribe. > You are receiving this because you modified the open/close state.Message > ID: ***@***.***>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#480