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

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

Closed
genesispvtltd opened this issue Feb 5, 2021 · 7 comments
Labels

Comments

@genesispvtltd
Copy link

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

        }
    }
}
@genesispvtltd
Copy link
Author

genesispvtltd commented 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

@StefH
Copy link
Collaborator

StefH commented 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....

@genesispvtltd
Copy link
Author

genesispvtltd commented 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.

@StefH
Copy link
Collaborator

StefH commented 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'.

@genesispvtltd
Copy link
Author

@StefH thanks for the response I am good.cool

@StefH
Copy link
Collaborator

StefH commented Feb 6, 2021

Can this question be closed for now?

@genesispvtltd
Copy link
Author

yeah sure

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