forward proxy issue using wiremock using wiremock 1.47 version

274 views
Skip to first unread message

Nirmalbabu Polimetla

unread,
Jul 14, 2015, 4:46:28 AM7/14/15
to wiremo...@googlegroups.com
Hi 

Today i have integrated code of forward proxy in my code and i have to hit the webservice using forward proxy and when i run the test case i am getting and i am using eclipse to run the testcases and i don't start wire-mock using terminal 
" Connect to 127.0.0.1:8080 [/127.0.0.1] failed: Connection refused"

implementation class logic 

1) 
getMember(){
  CloseableHttpClient httpclient = HttpClients.createDefault();
   HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
            RequestConfig config = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();

            HttpGet getRequest = new HttpGet(url);
            getRequest.setConfig(config);
            getRequest .addHeader("accept", "application/json");
            getRequest .addHeader("Authorization", tokenFromAuthenticationAfer);
            CloseableHttpResponse response = httpclient.execute(getRequest);



}

2) 
from my wiremock junit class i am invoking the method 

___________________________________________________________________________

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:spring/test_aimia_service-context.xml" })
public class AimiaEnablerServiceWireMockTest {


 private static int wireMockPort = 8089;


    WireMockServer wireMockServer = new WireMockServer(wireMockConfig().proxyVia("127.0.0.1", 8080));

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(wireMockPort).notifier(
        new Log4jNotifier()));



 @Test
    public void testgetMember() throws AimiaServiceFaultException {
        GetMemberRequestDto request = new GetMemberRequestDto();
        request.setServiceId("success");
        GetMemberResponseDto response;
        response = jwsaimiaservice.getMember(request);
        logWriter.logDebug("response: " + response);
        if (response != null) {
            assertNotNull(response);
         //   assertEquals("ABC", response.getGetMemberResponse().getFirstName());

        }
    }


___________________________________________________________________________

3) 

mapping file 

{
  "request" : {
    "method" : "GET",
    "url" : "/RESTfulExample/rest/programs/OPTUS/getMemberDetails/token-type/UID/token/success"

  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "body-aimia.enabler.getMember.json",
    "headers" : {
      "Content-Type" : "application/json"
    }
  }
}


4) __files 

{
"success"
}





------------------------------------------------------------------------------------------------------------------------------------------


Exception Message and its failing the test case 

org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:8080 [/127.0.0.1] failed: Connection refused


Please help me how to achieve this forward proxy with wiremock 
and if i comment below lines of code wire mock work like champ 

// HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
  //          RequestConfig config = RequestConfig.custom()
    //                .setProxy(proxy)
      //              .build();


Regards,
Nirmal 


Tom Akehurst

unread,
Jul 14, 2015, 5:31:16 AM7/14/15
to wiremo...@googlegroups.com, pnb...@gmail.com
I'm not totally sure I understand what you're trying to do, but I think you need to start WireMock up this way:
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().enableBrowserProxying(true))

Nirmalbabu Polimetla

unread,
Jul 14, 2015, 6:14:54 AM7/14/15
to wiremo...@googlegroups.com, pnb...@gmail.com
Hi 

Thank you for your quick response and I am using Appache Http Client to hit the restful webservices.
i was told by my solution team that I have to hit the restful webservices via proxy therefore I added below redcolor lines in my code to meet the proxy 
and with out below red colour lines (proxy) of code  wiremock test cases are passed and its return HTTP CODE is 200 and i am getting the mock response from __files successfully.

but with below red color lines i am getting HTTP STATUS CODE - 404 
and finally I want to pass the test cases and is there any additional configuration am i missing in mapping files or do i need to add any additional code?? 


CloseableHttpClient httpclient = HttpClients.createDefault();
   HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
            RequestConfig config = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();

            HttpGet getRequest = new HttpGet(url);
            getRequest.setConfig(config);
            getRequest .addHeader("accept", "application/json");
            getRequest .addHeader("Authorization", tokenFromAuthenticationAfer);
            CloseableHttpResponse response = httpclient.execute(getRequest);


the below line does not change the output 
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().enableBrowserProxying(true))


Hope you understand my qestion. 

Nirmal

unread,
Jul 15, 2015, 2:39:06 AM7/15/15
to wiremo...@googlegroups.com
HI Tom,

I have figured it out and its now working fine.
and i am bypassing the proxy logic with some condition for junit test case

Thank you for your support.

Regards,
Nirmal

Albert Vesker

unread,
Sep 14, 2022, 9:22:51 AM9/14/22
to wiremock-user
How dud you solve this problem?

среда, 15 июля 2015 г. в 09:39:06 UTC+3, pnb...@gmail.com:
Reply all
Reply to author
Forward
0 new messages