|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
| 14 | +package com.ning.http.client.async; |
| 15 | + |
| 16 | +import com.ning.http.client.AsyncHttpClient; |
| 17 | +import com.ning.http.client.AsyncHttpClientConfig; |
| 18 | +import com.ning.http.client.ProxyServer; |
| 19 | +import com.ning.http.client.Realm.AuthScheme; |
| 20 | +import com.ning.http.client.Request; |
| 21 | +import com.ning.http.client.RequestBuilder; |
| 22 | +import com.ning.http.client.Response; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.UnknownHostException; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.Future; |
| 28 | + |
| 29 | +import javax.servlet.ServletException; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import javax.servlet.http.HttpServletResponse; |
| 32 | + |
| 33 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 34 | +import org.testng.Assert; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +public abstract class NTLMProxyTest extends AbstractBasicTest { |
| 38 | + |
| 39 | + public static class NTLMProxyHandler extends AbstractHandler { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void handle(String pathInContext, org.eclipse.jetty.server.Request request, HttpServletRequest httpRequest, |
| 43 | + HttpServletResponse httpResponse) throws IOException, ServletException { |
| 44 | + |
| 45 | + String authorization = httpRequest.getHeader("Proxy-Authorization"); |
| 46 | + if (authorization == null) { |
| 47 | + httpResponse.setStatus(407); |
| 48 | + httpResponse.setHeader("Proxy-Authenticate", "NTLM"); |
| 49 | + |
| 50 | + } else if (authorization.equals("NTLM TlRMTVNTUAABAAAAAYIIogAAAAAoAAAAAAAAACgAAAAFASgKAAAADw==")) { |
| 51 | + httpResponse.setStatus(407); |
| 52 | + httpResponse.setHeader("Proxy-Authenticate", "NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA=="); |
| 53 | + |
| 54 | + } else if (authorization |
| 55 | + .equals("NTLM TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABQAFAB4AAAADAAMAIwAAAASABIAmAAAAAAAAACqAAAAAYIAAgUBKAoAAAAPrYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADTVQBSAFMAQQAtAE0ASQBOAE8AUgBaAGEAcABoAG8AZABMAGkAZwBoAHQAQwBpAHQAeQA=")) { |
| 56 | + httpResponse.setStatus(200); |
| 57 | + } else { |
| 58 | + httpResponse.setStatus(401); |
| 59 | + } |
| 60 | + httpResponse.setContentLength(0); |
| 61 | + httpResponse.getOutputStream().flush(); |
| 62 | + httpResponse.getOutputStream().close(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public AbstractHandler configureHandler() throws Exception { |
| 68 | + return new NTLMProxyHandler(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void ntlmProxyTest() throws IOException, InterruptedException, ExecutionException { |
| 73 | + |
| 74 | + AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build(); |
| 75 | + |
| 76 | + try (AsyncHttpClient client = getAsyncHttpClient(config)) { |
| 77 | + Request request = new RequestBuilder("GET").setProxyServer(ntlmProxy()).setUrl(getTargetUrl()).build(); |
| 78 | + Future<Response> responseFuture = client.executeRequest(request); |
| 79 | + int status = responseFuture.get().getStatusCode(); |
| 80 | + Assert.assertEquals(status, 200); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private ProxyServer ntlmProxy() throws UnknownHostException |
| 85 | + { |
| 86 | + ProxyServer proxyServer = new ProxyServer("127.0.0.1", port2, "Zaphod", "Beeblebrox").setNtlmDomain("Ursa-Minor"); |
| 87 | + proxyServer.setNtlmHost("LightCity"); |
| 88 | + proxyServer.setScheme(AuthScheme.NTLM); |
| 89 | + return proxyServer; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments