2121
2222import org .apache .mina .core .service .IoConnector ;
2323import org .apache .mina .proxy .ProxyConnector ;
24+ import org .apache .mina .proxy .handlers .http .HttpProxyRequest ;
25+ import org .apache .mina .proxy .handlers .http .HttpSmartProxyHandler ;
2426import org .apache .mina .proxy .session .ProxyIoSession ;
2527import org .apache .mina .transport .socket .SocketConnector ;
2628import org .apache .mina .util .AvailablePortFinder ;
2729import org .junit .Test ;
2830import quickfix .ConfigError ;
2931
3032import java .net .InetSocketAddress ;
33+ import java .util .Base64 ;
34+ import java .util .List ;
35+ import java .util .Map ;
3136
37+ import static org .junit .Assert .assertEquals ;
38+ import static org .junit .Assert .assertNotNull ;
3239import static org .junit .Assert .assertNull ;
40+ import static org .junit .Assert .assertTrue ;
41+ import static quickfix .mina .ProtocolFactory .PROXY_AUTHORIZATION_HEADER ;
3342
3443public class ProtocolFactoryTest {
3544
@@ -46,4 +55,95 @@ public void shouldCreateProxyConnectorWithoutPreferredAuthOrder() throws ConfigE
4655 ProxyIoSession proxySession = proxyConnector .getProxyIoSession ();
4756 assertNull (proxySession .getPreferedOrder ());
4857 }
58+
59+ @ Test
60+ public void shouldSetBasicAuthorizationHeaderForHttpProxy () throws ConfigError {
61+ InetSocketAddress address = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
62+ InetSocketAddress proxyAddress = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
63+
64+ IoConnector connector = ProtocolFactory .createIoConnector (address );
65+ ProxyConnector proxyConnector = ProtocolFactory
66+ .createIoProxyConnector ((SocketConnector ) connector , address , proxyAddress , "http" , "1.0" , "testuser" ,
67+ "testpassword" , null , null );
68+
69+ ProxyIoSession proxySession = proxyConnector .getProxyIoSession ();
70+
71+ // Verify HttpSmartProxyHandler is set
72+ assertNotNull ("Handler should be set" , proxySession .getHandler ());
73+ assertTrue ("Handler should be HttpSmartProxyHandler" ,
74+ proxySession .getHandler () instanceof HttpSmartProxyHandler );
75+
76+ HttpProxyRequest request = (HttpProxyRequest ) proxySession .getRequest ();
77+
78+ Map <String , List <String >> headers = request .getHeaders ();
79+ assertNotNull ("Headers should not be null" , headers );
80+ assertTrue ("Headers should contain Proxy-Authorization" , headers .containsKey (PROXY_AUTHORIZATION_HEADER ));
81+
82+ List <String > authHeaders = headers .get (PROXY_AUTHORIZATION_HEADER );
83+ assertNotNull ("Proxy-Authorization header should not be null" , authHeaders );
84+ assertEquals ("Should have exactly one Proxy-Authorization header" , 1 , authHeaders .size ());
85+
86+ String authHeader = authHeaders .get (0 );
87+ assertTrue ("Auth header should start with 'Basic '" , authHeader .startsWith ("Basic " ));
88+
89+ // Verify the encoded credentials
90+ String encodedPart = authHeader .substring ("Basic " .length ());
91+ String decoded = new String (Base64 .getDecoder ().decode (encodedPart ));
92+ assertEquals ("Decoded credentials should match" , "testuser:testpassword" , decoded );
93+ }
94+
95+ @ Test
96+ public void shouldNotSetAuthorizationHeaderForNTLMAuthentication () throws ConfigError {
97+ InetSocketAddress address = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
98+ InetSocketAddress proxyAddress = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
99+
100+ IoConnector connector = ProtocolFactory .createIoConnector (address );
101+ ProxyConnector proxyConnector = ProtocolFactory
102+ .createIoProxyConnector ((SocketConnector ) connector , address , proxyAddress , "http" , "1.0" , "testuser" ,
103+ "testpassword" , "TESTDOMAIN" , "TESTWORKSTATION" );
104+
105+ ProxyIoSession proxySession = proxyConnector .getProxyIoSession ();
106+
107+ // Verify HttpSmartProxyHandler is set for NTLM too
108+ assertNotNull ("Handler should be set" , proxySession .getHandler ());
109+ assertTrue ("Handler should be HttpSmartProxyHandler for NTLM" ,
110+ proxySession .getHandler () instanceof HttpSmartProxyHandler );
111+
112+ HttpProxyRequest request = (HttpProxyRequest ) proxySession .getRequest ();
113+
114+ Map <String , List <String >> headers = request .getHeaders ();
115+ // NTLM requires multi-step handshake, so Proxy-Authorization header should not be set upfront
116+ assertTrue ("Headers should be null or not contain Proxy-Authorization for NTLM" ,
117+ headers == null || !headers .containsKey (PROXY_AUTHORIZATION_HEADER ));
118+
119+ // Verify NTLM properties are set correctly for the MINA proxy handler to use
120+ Map <String , String > props = request .getProperties ();
121+ assertNotNull ("Properties should not be null" , props );
122+ assertTrue ("Properties should contain user credentials" , props .size () >= 4 );
123+ }
124+
125+ @ Test
126+ public void shouldNotSetAuthorizationHeaderWhenCredentialsNotProvided () throws ConfigError {
127+ InetSocketAddress address = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
128+ InetSocketAddress proxyAddress = new InetSocketAddress (AvailablePortFinder .getNextAvailable ());
129+
130+ IoConnector connector = ProtocolFactory .createIoConnector (address );
131+ ProxyConnector proxyConnector = ProtocolFactory
132+ .createIoProxyConnector ((SocketConnector ) connector , address , proxyAddress , "http" , "1.0" , null ,
133+ null , null , null );
134+
135+ ProxyIoSession proxySession = proxyConnector .getProxyIoSession ();
136+
137+ // Verify HttpSmartProxyHandler is set even when no credentials provided
138+ assertNotNull ("Handler should be set" , proxySession .getHandler ());
139+ assertTrue ("Handler should be HttpSmartProxyHandler when no credentials" ,
140+ proxySession .getHandler () instanceof HttpSmartProxyHandler );
141+
142+ HttpProxyRequest request = (HttpProxyRequest ) proxySession .getRequest ();
143+
144+ Map <String , List <String >> headers = request .getHeaders ();
145+ // Headers should either be null or not contain Proxy-Authorization
146+ assertTrue ("Headers should be null or empty when no credentials provided" ,
147+ headers == null || !headers .containsKey (PROXY_AUTHORIZATION_HEADER ));
148+ }
49149}
0 commit comments