Index: /home/michael/eclipse/workspace/HttpClient-Trunk/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java =================================================================== --- /home/michael/eclipse/workspace/HttpClient-Trunk/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java (revision 160249) +++ /home/michael/eclipse/workspace/HttpClient-Trunk/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java (working copy) @@ -31,19 +31,19 @@ import java.io.IOException; import junit.framework.Test; -import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.httpclient.FakeHttpMethod; import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpClientTestBase; +import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.NTCredentials; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.server.HttpService; import org.apache.commons.httpclient.server.RequestLine; -import org.apache.commons.httpclient.server.SimpleHttpServer; import org.apache.commons.httpclient.server.SimpleRequest; import org.apache.commons.httpclient.server.SimpleResponse; @@ -54,10 +54,10 @@ * @author Jeff Dever * @version $Id$ */ -public class TestNTLMAuth extends TestCase { +public class TestNTLMAuth extends HttpClientTestBase { // ------------------------------------------------------------ Constructor - public TestNTLMAuth(String testName) { + public TestNTLMAuth(String testName) throws IOException { super(testName); } @@ -150,18 +150,15 @@ public void testNTLMAuthenticationRetry() throws Exception { - // configure the server - SimpleHttpServer server = new SimpleHttpServer(); // use arbitrary port - server.setTestname(getName()); - server.setHttpService(new NTLMAuthService()); + this.server.setHttpService(new NTLMAuthService()); + // configure the client - HttpClient client = new HttpClient(); - client.getHostConfiguration().setHost( + this.client.getHostConfiguration().setHost( server.getLocalAddress(), server.getLocalPort(), Protocol.getProtocol("http")); - client.getState().setCredentials(AuthScope.ANY, + this.client.getState().setCredentials(AuthScope.ANY, new NTCredentials("username", "password", "host", "domain")); FakeHttpMethod httpget = new FakeHttpMethod("/"); @@ -172,7 +169,70 @@ } assertNull(httpget.getResponseHeader("WWW-Authenticate")); assertEquals(200, httpget.getStatusCode()); - server.destroy(); } + + private class PreemptiveNTLMAuthService implements HttpService { + + public PreemptiveNTLMAuthService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + RequestLine requestLine = request.getRequestLine(); + HttpVersion ver = requestLine.getHttpVersion(); + Header auth = request.getFirstHeader("Authorization"); + if (auth == null) { + response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST); + response.setBodyString("Authorization header missing"); + return true; + } else { + String authstr = auth.getValue(); + + if (authstr.indexOf("NTLM") != -1) { + response.setStatusLine(ver, HttpStatus.SC_OK); + return true; + } else if (authstr.indexOf("Basic") != -1) { + response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED); + response.addHeader(new Header("WWW-Authenticate", "Negotiate")); + response.addHeader(new Header("WWW-Authenticate", "NTLM")); + response.setBodyString("Authorization required"); + return true; + } else { + response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST); + response.setBodyString("Unknown auth type: " + authstr); + return true; + } + } + } + } + + /** + * Make sure preemptive authorization works when the server requires NLM. + * @throws Exception + */ + public void testPreemptiveAuthorization() throws Exception { + + NTCredentials creds = + new NTCredentials("testuser", "testpass", "host", "domain"); + + HttpState state = new HttpState(); + state.setCredentials(AuthScope.ANY, creds); + this.client.setState(state); + this.client.getParams().setAuthenticationPreemptive(true); + + this.server.setHttpService(new PreemptiveNTLMAuthService()); + + GetMethod httpget = new GetMethod("/test/"); + try { + this.client.executeMethod(httpget); + } finally { + httpget.releaseConnection(); + } + assertNotNull(httpget.getStatusLine()); + assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); + } + }