Java Code Examples for com.mashape.unirest.http.Unirest#setProxy()

The following examples show how to use com.mashape.unirest.http.Unirest#setProxy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MitmproxyJavaTest.java    From mitmproxy-java with Apache License 2.0 8 votes vote down vote up
@Test
public void SimpleTest() throws InterruptedException, TimeoutException, IOException, UnirestException {
    List<InterceptedMessage> messages = new ArrayList<>();

    MitmproxyJava proxy = new MitmproxyJava(MITMDUMP_PATH, (InterceptedMessage m) -> {
        messages.add(m);
        return m;
    });
    proxy.start();

    Unirest.setProxy(new HttpHost("localhost", 8080));
    Unirest.get("http://appium.io").header("myTestHeader", "myTestValue").asString();

    proxy.stop();
    final InterceptedMessage firstMessage = messages.get(0);

    assertThat(firstMessage.getRequest().getUrl()).startsWith("http://appium.io");
    assertThat(firstMessage.getRequest().getHeaders()).containsOnlyOnce(new String[]{"myTestHeader", "myTestValue"});
    assertThat(firstMessage.getResponse().getStatusCode()).isEqualTo(200);
}
 
Example 2
Source File: MitmproxyJavaTest.java    From mitmproxy-java with Apache License 2.0 6 votes vote down vote up
@Test
public void NullInterceptorReturnTest() throws InterruptedException, TimeoutException, IOException, UnirestException {
    List<InterceptedMessage> messages = new ArrayList<>();

    MitmproxyJava proxy = new MitmproxyJava(MITMDUMP_PATH, (InterceptedMessage m) -> {
        messages.add(m);
        return null;
    }, 8087, null);
    proxy.start();

    Unirest.setProxy(new HttpHost("localhost", 8087));
    Unirest.get("http://appium.io").header("myTestHeader", "myTestValue").asString();

    proxy.stop();

    assertThat(messages).isNotEmpty();

    final InterceptedMessage firstMessage = messages.get(0);

    assertThat(firstMessage.getRequest().getUrl()).startsWith("http://appium.io");
    assertThat(firstMessage.getRequest().getHeaders()).containsOnlyOnce(new String[]{"myTestHeader", "myTestValue"});
    assertThat(firstMessage.getResponse().getStatusCode()).isEqualTo(200);
}
 
Example 3
Source File: MitmproxyJavaTest.java    From mitmproxy-java with Apache License 2.0 6 votes vote down vote up
@Test
public void ResponseModificationTest() throws InterruptedException, TimeoutException, IOException, UnirestException {
    List<InterceptedMessage> messages = new ArrayList<>();

    MitmproxyJava proxy = new MitmproxyJava(MITMDUMP_PATH, (InterceptedMessage m) -> {
        messages.add(m);
        m.getResponse().setBody("Hi from Test".getBytes(StandardCharsets.UTF_8));
        m.getResponse().getHeaders().add(new String[]{"myTestResponseHeader", "myTestResponseHeaderValue"});
        m.getResponse().setStatusCode(208);
        return m;
    });
    proxy.start();

    Unirest.setProxy(new HttpHost("localhost", 8080));
    HttpResponse<String> response = Unirest.get("http://appium.io").header("myTestHeader", "myTestValue").asString();
    proxy.stop();

    assertThat(response.getBody()).isEqualTo("Hi from Test");

    final InterceptedMessage firstMessage = messages.get(0);

    assertThat(firstMessage.getRequest().getUrl()).startsWith("http://appium.io");
    assertThat(firstMessage.getRequest().getHeaders()).containsOnlyOnce(new String[]{"myTestHeader", "myTestValue"});
    assertThat(firstMessage.getResponse().getHeaders()).containsOnlyOnce(new String[]{"myTestResponseHeader", "myTestResponseHeaderValue"});
    assertThat(firstMessage.getResponse().getStatusCode()).isEqualTo(208);
}
 
Example 4
Source File: Airtable.java    From airtable.java with MIT License 5 votes vote down vote up
/**
 * Set Proxy manually.
 *
 * @param proxy the proxy.
 */
public void setProxy(String proxy) {

    this.config.setProxy(proxy);
    if (proxy == null) {
        Unirest.setProxy(null);
    } else {
        Unirest.setProxy(HttpHost.create(this.config.getProxy()));
    }

}
 
Example 5
Source File: ApiServiceFactoryTest.java    From pagerduty-client with MIT License 5 votes vote down vote up
@Test
public void apiServiceFactoryWithProxyParamsProducesRightDefaultApiServiceImpl() {
    String eventApi = "eventApi";
    String proxyHost = "localhost";
    Integer proxyPort = 8080;
    ApiServiceFactory apiServiceFactory = new ApiServiceFactory(eventApi, proxyHost, proxyPort);

    ApiService apiService = apiServiceFactory.getDefault();
    HttpApiServiceImpl httpApiService = new HttpApiServiceImpl(eventApi, proxyHost, proxyPort);
    assertThat(apiService).isExactlyInstanceOf(HttpApiServiceImpl.class);
    assertThat(apiService).isEqualTo(httpApiService);
    // reset uni rest settings
    Unirest.setProxy(null);
}
 
Example 6
Source File: UnirestDownloader.java    From gecco with MIT License 5 votes vote down vote up
@Override
public HttpResponse download(HttpRequest request) throws DownloaderException {
	if(log.isDebugEnabled()) {
		log.debug("downloading..." + request.getUrl());
	}
	try {
		HttpHost proxy = Proxys.getProxy();
		if(proxy != null) {
			Unirest.setProxy(proxy);
		} else {
			Unirest.setProxy(null);
		}
		request.addHeader("User-Agent", UserAgent.getUserAgent());
		com.mashape.unirest.http.HttpResponse<String> response = null;
		if(request instanceof HttpPostRequest) {
			HttpPostRequest post = (HttpPostRequest)request;
			HttpRequestWithBody httpRequestWithBody = Unirest.post(post.getUrl());
			httpRequestWithBody.headers(post.getHeaders());
			httpRequestWithBody.fields(post.getFields());
			response = httpRequestWithBody.asString();
		} else {
			response = Unirest.get(request.getUrl()).headers(request.getHeaders()).asString();
		}
		String contentType = response.getHeaders().getFirst("Content-Type");
		HttpResponse resp = new HttpResponse();
		resp.setStatus(response.getStatus());
		resp.setRaw(response.getRawBody());
		resp.setContent(response.getBody());
		resp.setContentType(contentType);
		resp.setCharset(getCharset(request, contentType));
		return resp;
	} catch (UnirestException e) {
		throw new DownloaderException(e);
	}
}
 
Example 7
Source File: HttpApiServiceImpl.java    From pagerduty-client with MIT License 4 votes vote down vote up
private void initUnirestWithProxy(String proxyHost, Integer proxyPort) {
    initUnirest();
    Unirest.setProxy(new HttpHost(proxyHost, proxyPort));
}