Adven27/grpc-wiremock

How to connect to wiremock server instance to add mappings?

Closed this issue · 3 comments

Hi,
let's say I don't want to add mappings on start of the docker container by using "-v $(pwd)/example/wiremock:/wiremock" but i would rather add mappings dynamically from code.

I have a wiremock server running on localhost:8888, if I hit http://localhost:8888/__admin/mappings from my browser I can see there are no mappings

{ "mappings" : [ ], "meta" : { "total" : 0 } }

I would like to add some mappings now so I am using the following code (first I am trying to fetch existing mappings expecting an empty list)

[Test]
        public async Task Test1()
        {
            // Create an implementation of the IWireMockAdminApi and pass in the base URL for the API.
            var api = RestClient.For<IWireMockAdminApi>("http://localhost:8888");

            var mappings = await api.GetMappingsAsync();
            Console.WriteLine($"mappings = {JsonConvert.SerializeObject(mappings)}");
        }

The above code fails on

Newtonsoft.Json.JsonSerializationException : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[WireMock.Admin.Mappings.MappingModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'mappings', line 2, position 14.

   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at RestEase.JsonResponseDeserializer.Deserialize[T](String content, HttpResponseMessage response, ResponseDeserializerInfo info)
   at RestEase.Implementation.Requester.Deserialize[T](String content, HttpResponseMessage response, IRequestInfo requestInfo)
   at RestEase.Implementation.Requester.RequestAsync[T](IRequestInfo requestInfo)
   at WireMockTest.Tests.Setup() in /Users/martinco/Work/WireMockTest/WireMockTest/WireMockTest/UnitTest1.cs:line 26
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaitable)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
   at NUnit.Framework.Internal.Commands.SetUpTearDownItem.RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method)
   at NUnit.Framework.Internal.Commands.SetUpTearDownItem.RunSetUp(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.<>c__DisplayClass0_0.<.ctor>b__0(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

csproj details

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NUnit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="WireMock.Net.RestClient" Version="1.4.5" />
    <PackageReference Include="WireMock.Net.Abstractions" Version="1.4.5" />
  </ItemGroup>

</Project>

I tried other methods such as GetSettingsAsync() with the same result.
What I am missing here please? The documentation says, it uses wrapper around Wiremock server so I assume WireMock.Net.RestClient should work?

Thanks for clarification in advance.

I found the answer WireMock-Net/WireMock.Net#588 meaning I won't be able to use this from .Net project

I found the answer WireMock-Net/WireMock.Net#588 meaning I won't be able to use this from .Net project

Hi @mcopjan ! Thanks for the question. If I got it right you can't use WireMock.Net client library but you still can interact with the Wiremock API directly through the regular HTTP client.

@Adven27 that's right, I could not use the mentioned library because for some reason it uses a custom WireMock model which is not in line with the one from Wiremock.org (the one being used by this project as well). I used NSwag and WireMock openAPI spec from http://wiremock.org/assets/js/wiremock-admin-api.json and generated c# client hence I can communicate with your solution and get/set stubs which is what I needed.