Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I have a web service running on my dev box implemented using Spring-MVC 3.0. I have various JUnits that test against that service using RestTemplate. What I would like to do is have JMeter pick up those JUnits REST requests when I run them. However, to do that, I need to have Spring's RestTemplate send them to the proxy that I'm running JMeter on. So, the question is, how can I do that?

I've done something similar with CXF and their http:conduit and http:client stuff, but I really have no idea how to do this with Spring-MVC.

回答1:

The accepted answer works for simple use cases, where you want all requests to use the same proxy. If you need some requests through restTemplate to use the proxy, and others to not, though, you may find this more useful. (Or if you just like doing it programmatically more than you like mucking with system properties!)

@Bean public RestTemplate restTemplate() {     SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();      Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress("my.host.com", 8080));     requestFactory.setProxy(proxy);      return new RestTemplate(requestFactory); } 

You should be able to create a copy of the restTemplate bean that way, and another one the normal way, so you can send requests with and without the proxy.



回答2:

Sadly, this was really easy.

 Properties props = System.getProperties(); props.put("http.proxyHost", "localhost"); props.put("http.proxyPort", "9080"); 


回答3:

Spring has a good documentation using a Customizer to determine different proxy

public class ProxyCustomizer implements RestTemplateCustomizer {      @Override     public void customize(RestTemplate restTemplate) {         final String proxyUrl = "proxy.example.com";         final int port = 3128;          HttpHost proxy = new HttpHost(proxyUrl, port);         HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {             @Override             protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context)                     throws HttpException {                 if (target.getHostName().equals("gturnquist-quoters.cfapps.io")) {                     return super.determineProxy(target, request, context);                 }                 return null;             }         }).build();         restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));      }  } 

and the call to apply the ProxyCustomizer is

@Bean public RestTemplate restTemplate(RestTemplateBuilder builder) {     return builder.additionalCustomizers(new ProxyCustomizer()).build(); } 


回答4:

put these lines before calling your get or post method. so proxy get set .

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();     DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();     HttpHost proxy = new HttpHost("proxtserver", port);     httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);     restTemplate.setRequestFactory(requestFactory); 


回答5:

If understand correctly, you want the Jmeter script to Reset based on a JUnit RESET request, correct?

If so, might this be as simple as sending a JMeter a command line telling it to stop/start?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!