WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 205 forks source link

Calling and Stubbing Multiple Services #490

Closed vermapraveen closed 4 years ago

vermapraveen commented 4 years ago

First of all, thanks for this very useful lib. Planning to use in LoadTests, IntegrationTests. I have started experiment with this, got question:

how to achieve multiple service call, as _mockServer.Urls gives only 1 url My application makes multiple http calls to diff endpoints. currently I am able to stub 1 api call like below. I want to stub all of them so that I can test my app in isolation. please suggest.

       [Fact]
        public async Task ShouldAbleToGetMockRequest()
        {
            WebApiCaller sut = new WebApiCaller(_mockServer.Urls.First());
            StubResponseGenerator();

            var response = await sut.ExecuteInWebApi();
            response.Should().NotBeNull();
            response.Id1.Should().NotBeNull();
        }

        private void StubResponseGenerator()
        {
            var canned = new WebApiResponse
            {
                Id1 = "324234234",
                Id2 = "sdfsdfsdf",
                Data = new InternalData
                {
                    details = new[] { new Detail() }
                }
            };

            _mockServer
                  .Given(Request
                    .Create().WithPath("/GetAllDetails")
                    // The canned response will *only* be sent to the 
                    // request with the corresponding userName
                    //.WithParam("param1")
                    .UsingGet())
                  .RespondWith(
                    Response.Create()
                      .WithStatusCode(200)
                      .WithHeader("Content-Type", "application/json")
                    .WithBodyAsJson(canned)
                  );
        }
StefH commented 4 years ago

@vermapraveen You can just use multiple URL's if you use the settings, see example: https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L49

StefH commented 4 years ago

@vermapraveen Does this answer your question?

vermapraveen commented 4 years ago

yes, thanks

On Sat, Aug 1, 2020 at 9:55 AM Stef Heyenrath notifications@github.com wrote:

@vermapraveen https://github.com/vermapraveen Does this answer your question?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/WireMock-Net/WireMock.Net/issues/490#issuecomment-667543922, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMDBFBSLEL7U5H5TXIPDRTR6QUH5ANCNFSM4PAROWJQ .

-- Regards, Praveen Verma