[Question] What is the correct configuration for wiremock running in proxy mode #331

Closed
opened 2025-12-29 15:21:07 +01:00 by adam · 7 comments
Owner

Originally created by @genesispvtltd on GitHub (Feb 5, 2021).

Hi I am new to wiremock but I need to know the answers to the following questions. I have a automation test suite running against an api. Sometimes this api is not available then I need automation test to be run against wiremock server and this should happen automatically I mean wiremock should be intelligent enough to understand the live url is down and it will call the stubs allready created. I tried wiremock standalone version and this is not happening. I started the wiremock on proxy mode and the live url is up and running I sent a request and it does hit live url, and then next I block the live url using the windows host file. I restareted wiremock and I again sent the request to wiremock server running in proxy mode but it fails and send me "No connection could be made because the target machine actively refused it." please help me to understand why I am facing this issue.Basically when the live url is down then i need 12345 to be return. Also the live url endpoint is nothing but it will create a new random number and will return that number. Below is my code also please understand I am using csharp code to write my stubs and i want them to be added as static mappings as well so I am doing it in the code like below

using Microsoft.Extensions.Configuration;
using System;
using System.Diagnostics;
using System.IO;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Net.StandAlone;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Settings;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
           
            var mode = "Proxying";
            var url = "http://localhost:7777";
            var proxyUrl = "https://secureApi.com";

            WireMockServerSettings settings = null;
            
                     settings = new WireMockServerSettings
                    {
                        Urls = new[] { url },
                        StartAdminInterface = true,
                        
                        AllowPartialMapping =true,
                        Logger = new WireMockConsoleLogger(),
                         ProxyAndRecordSettings = new ProxyAndRecordSettings
                         {
                             Url = proxyUrl,
                            // SaveMapping = false,
                            // SaveMappingToFile = false,
                            // SaveMappingForStatusCodePattern = "2xx"
                         }
                     };
                    
            

            var wiremockServer = StandAloneApp.Start(settings);

            wiremockServer            
              .Given(              
              Request.Create().WithPath("/Accounts/api/securityhelper/getauthtoken")
              .UsingGet()                          
              .WithCookie("paycorAuth", new RegexMatcher(".*"))
            
              )
              //.AtPriority(100)
              .RespondWith(
                Response.Create()
                 .WithProxy(proxyUrl)
                 .WithBody("12345")
              );

            wiremockServer.SaveStaticMappings();

         
            Console.WriteLine("Press any key to stop the server");
            Console.ReadKey();

        }
    }
}

Originally created by @genesispvtltd on GitHub (Feb 5, 2021). Hi I am new to wiremock but I need to know the answers to the following questions. I have a automation test suite running against an api. Sometimes this api is not available then I need automation test to be run against wiremock server and this should happen automatically I mean wiremock should be intelligent enough to understand the live url is down and it will call the stubs allready created. I tried wiremock standalone version and this is not happening. I started the wiremock on proxy mode and the live url is up and running I sent a request and it does hit live url, and then next I block the live url using the windows host file. I restareted wiremock and I again sent the request to wiremock server running in proxy mode but it fails and send me "No connection could be made because the target machine actively refused it." please help me to understand why I am facing this issue.Basically when the live url is down then i need 12345 to be return. Also the live url endpoint is nothing but it will create a new random number and will return that number. Below is my code also please understand I am using csharp code to write my stubs and i want them to be added as static mappings as well so I am doing it in the code like below ``` c# using Microsoft.Extensions.Configuration; using System; using System.Diagnostics; using System.IO; using WireMock.Logging; using WireMock.Matchers; using WireMock.Net.StandAlone; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Settings; namespace ConsoleApp6 { class Program { static void Main(string[] args) { var mode = "Proxying"; var url = "http://localhost:7777"; var proxyUrl = "https://secureApi.com"; WireMockServerSettings settings = null; settings = new WireMockServerSettings { Urls = new[] { url }, StartAdminInterface = true, AllowPartialMapping =true, Logger = new WireMockConsoleLogger(), ProxyAndRecordSettings = new ProxyAndRecordSettings { Url = proxyUrl, // SaveMapping = false, // SaveMappingToFile = false, // SaveMappingForStatusCodePattern = "2xx" } }; var wiremockServer = StandAloneApp.Start(settings); wiremockServer .Given( Request.Create().WithPath("/Accounts/api/securityhelper/getauthtoken") .UsingGet() .WithCookie("paycorAuth", new RegexMatcher(".*")) ) //.AtPriority(100) .RespondWith( Response.Create() .WithProxy(proxyUrl) .WithBody("12345") ); wiremockServer.SaveStaticMappings(); Console.WriteLine("Press any key to stop the server"); Console.ReadKey(); } } } ```
adam added the question label 2025-12-29 15:21:07 +01:00
adam closed this issue 2025-12-29 15:21:08 +01:00
Author
Owner

@genesispvtltd commented on GitHub (Feb 6, 2021):

Basically i need to know the setting for .net can you please tell me the correct setting for the below in .net version. This serves my purpose because when the live url is down wiremock knows to call its mappings and return them. Plus when live url is up it will call the live url automatically

@genesispvtltd commented on GitHub (Feb 6, 2021): Basically i need to know the setting for .net can you please tell me the correct setting for the below in .net version. This serves my purpose because when the live url is down wiremock knows to call its mappings and return them. Plus when live url is up it will call the live url automatically
Author
Owner

@StefH commented on GitHub (Feb 6, 2021):

What you mean is that you actually want a "smart" proxy mode:

Scenario 1
In case the real endpoint is up (we need to determine the criteria for up), proxy the request + response to the real endpoint.

Scenario 2
In case the real endpoint is down, try to find a configured mapping:

If the mapping is found, return that mapping.
If the mapping is not found, just return default 404 'mapping not found'.


About the Record-Mode for proxy : I'm not sure yet if this fits in this solution....

@StefH commented on GitHub (Feb 6, 2021): What you mean is that you actually want a "smart" proxy mode: **Scenario 1** In case the real endpoint is up (we need to determine the criteria for _up_), proxy the request + response to the real endpoint. **Scenario 2** In case the real endpoint is down, try to find a configured mapping: If the mapping is found, return that mapping. If the mapping is not found, just return default 404 'mapping not found'. --- About the Record-Mode for proxy : I'm not sure yet if this fits in this solution....
Author
Owner

@genesispvtltd commented on GitHub (Feb 6, 2021):

@StefH

Reason I am asking that most of the developers want their automation test suite to always hit the live url , but when the live url is down it is ok to return the wiremock stubs already created maching the request. But we don't need the automation test always running and returning the stubs which we have created in wiremock when the live url is up. So I am simply asking do you have a solution which serve my purpose in wiremock.net Thanks. Appreciate for your time on this matter.

@genesispvtltd commented on GitHub (Feb 6, 2021): @StefH Reason I am asking that most of the developers want their automation test suite to always hit the live url , but when the live url is down it is ok to return the wiremock stubs already created maching the request. But we don't need the automation test always running and returning the stubs which we have created in wiremock when the live url is up. So I am simply asking do you have a solution which serve my purpose in wiremock.net Thanks. Appreciate for your time on this matter.
Author
Owner

@StefH commented on GitHub (Feb 6, 2021):

Hello @genesispvtltd,

Short answer: WireMock.Net does not yet support a "Smart Proxy" functionality as you describe.

Long answer: This can probably be implemented as defined here:
Scenario 1
In case the real endpoint is up (we need to determine the criteria for up, like != 200 OK), proxy the request + response to the real endpoint.

Scenario 2
In case the real endpoint is down, try to find a configured mapping:

If the mapping is found, return that mapping.
If the mapping is not found, just return default 404 'mapping not found'.

@StefH commented on GitHub (Feb 6, 2021): Hello @genesispvtltd, Short answer: WireMock.Net does not yet support a "Smart Proxy" functionality as you describe. Long answer: This can probably be implemented as defined here: Scenario 1 In case the real endpoint is up (we need to determine the criteria for up, like != 200 OK), proxy the request + response to the real endpoint. Scenario 2 In case the real endpoint is down, try to find a configured mapping: If the mapping is found, return that mapping. If the mapping is not found, just return default 404 'mapping not found'.
Author
Owner

@genesispvtltd commented on GitHub (Feb 6, 2021):

@StefH thanks for the response I am good.cool

@genesispvtltd commented on GitHub (Feb 6, 2021): @StefH thanks for the response I am good.cool
Author
Owner

@StefH commented on GitHub (Feb 6, 2021):

Can this question be closed for now?

@StefH commented on GitHub (Feb 6, 2021): Can this question be closed for now?
Author
Owner

@genesispvtltd commented on GitHub (Feb 6, 2021):

yeah sure

@genesispvtltd commented on GitHub (Feb 6, 2021): yeah sure
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#331