11/**
22 * The MIT License
3- *
3+ * <p>
44 * Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5- *
5+ * <p>
66 * Permission is hereby granted, free of charge, to any person obtaining
77 * a copy of this software and associated documentation files (the
88 * "Software"), to deal in the Software without restriction, including
99 * without limitation the rights to use, copy, modify, merge, publish,
1010 * distribute, sublicense, and/or sell copies of the Software, and to
1111 * permit persons to whom the Software is furnished to do so, subject to
1212 * the following conditions:
13- *
13+ * <p>
1414 * The above copyright notice and this permission notice shall be
1515 * included in all copies or substantial portions of the Software.
16- *
16+ * <p>
1717 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1818 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1919 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3232import kong .unirest .core .Unirest ;
3333
3434import java .io .IOException ;
35- import java .net .InetSocketAddress ;
36- import java .net .ProxySelector ;
37- import java .net .SocketAddress ;
38- import java .net .URI ;
35+ import java .net .*;
3936import java .util .List ;
4037
4138import static java .net .Proxy .Type .HTTP ;
@@ -84,7 +81,7 @@ void canUseNonAuthProxyWithEasyMethod() {
8481 void canUseSelector () {
8582 JankyProxy .runServer ("localhost" , 4567 , 7777 );
8683
87- Unirest .config ().proxy (new ProxySelector (){
84+ Unirest .config ().proxy (new ProxySelector () {
8885 @ Override
8986 public List <java .net .Proxy > select (URI uri ) {
9087 var address = InetSocketAddress .createUnresolved ("localhost" , 7777 );
@@ -106,7 +103,7 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
106103 }
107104
108105 @ Test
109- void canSetAuthenticatedProxy (){
106+ void canSetAuthenticatedProxy () {
110107 JankyProxy .runServer ("localhost" , 4567 , 7777 );
111108
112109 Unirest .config ().proxy ("localhost" , 7777 , "username" , "password1!" );
@@ -120,8 +117,9 @@ void canSetAuthenticatedProxy(){
120117 }
121118
122119 @ Test
123- @ Disabled // there is some weird conflict between jetty and unirest here
124- void canFlagTheClientsToUseSystemProperties (){
120+ @ Disabled
121+ // there is some weird conflict between jetty and unirest here
122+ void canFlagTheClientsToUseSystemProperties () {
125123 JankyProxy .runServer ("localhost" , 4567 , 7777 );
126124
127125 System .setProperty ("http.proxyHost" , "localhost" );
@@ -137,7 +135,44 @@ void canFlagTheClientsToUseSystemProperties(){
137135 assertTrue (JankyProxy .wasUsed ());
138136 }
139137
140- // @Test @Disabled // https://free-proxy-list.net/
138+ @ Test
139+ void multipleOfEverything () {
140+ JankyProxy .runServer ("localhost" , 4567 , 7777 );
141+
142+ Unirest .config ()
143+ .proxy (new ProxySelector () {
144+ @ Override
145+ public List <java .net .Proxy > select (URI uri ) {
146+ if (uri .getHost ().equals ("homestarrunner.com" )) {
147+ return List .of (new java .net .Proxy (HTTP , InetSocketAddress .createUnresolved ("proxy-sad.com" , 7777 )));
148+ }
149+
150+ return List .of (new java .net .Proxy (HTTP , InetSocketAddress .createUnresolved ("default.com" , 7777 )));
151+ }
152+
153+ @ Override
154+ public void connectFailed (URI uri , SocketAddress sa , IOException ioe ) {
155+
156+ }
157+ })
158+
159+ .authenticator (new Authenticator () {
160+ @ Override
161+ public PasswordAuthentication requestPasswordAuthenticationInstance (String host , InetAddress addr ,
162+ int port , String protocol ,
163+ String prompt , String scheme ,
164+ URL url , RequestorType reqType ) {
165+ // Please don't hardcode passwords in your code :D
166+ if (host .equals ("homestarrunner.com" )) {
167+ return new PasswordAuthentication ("strongbad" , "password" .toCharArray ());
168+ }
169+ return new PasswordAuthentication ("default" , "password" .toCharArray ());
170+ }
171+ });
172+
173+ }
174+
175+ // @Test @Disabled // https://free-proxy-list.net/
141176// void callSomethingRealThroughARealProxy() {
142177// Unirest.config().proxy("18.222.230.116",8080);
143178// //Unirest.config().proxy("34.73.62.46",3128, "myuser","pass1!");
0 commit comments