diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java index 4b2d38ee1ce..6ad029d4a83 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java @@ -18,18 +18,25 @@ package org.eclipse.jetty.test; -import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import java.security.MessageDigest; import java.util.Collections; +import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.AuthenticationStore; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.client.util.BytesContentProvider; +import org.eclipse.jetty.client.util.DigestAuthentication; +import org.eclipse.jetty.client.util.StringContentProvider; +import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.HashLoginService; @@ -217,68 +224,60 @@ public class DigestPostTest @Test public void testServerWithHttpClientStringContent() throws Exception { + String srvUrl = "http://127.0.0.1:" + ((NetworkConnector)_server.getConnectors()[0]).getLocalPort() + "/test/"; HttpClient client = new HttpClient(); - client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); - client.setRealmResolver(new SimpleRealmResolver(new TestRealm())); - client.start(); - String srvUrl = "http://127.0.0.1:" + _server.getConnectors()[0].getLocalPort() + "/test/"; + try + { + AuthenticationStore authStore = client.getAuthenticationStore(); + authStore.addAuthentication(new DigestAuthentication(srvUrl, "test", "testuser", "password")); + client.start(); - ContentExchange ex = new ContentExchange(); - ex.setMethod(HttpMethods.POST); - ex.setURL(srvUrl); - ex.setRequestContent(new ByteArrayBuffer(__message,"UTF-8")); - - _received=null; - client.send(ex); - ex.waitForDone(); - - Assert.assertEquals(__message,_received); - Assert.assertEquals(200,ex.getResponseStatus()); + Request request = client.newRequest(srvUrl); + request.method(HttpMethod.POST); + request.content(new BytesContentProvider(__message.getBytes("UTF8"))); + _received=null; + ContentResponse response = request.send().get(5, TimeUnit.SECONDS); + Assert.assertEquals(__message,_received); + Assert.assertEquals(200,response.getStatus()); + } + finally + { + client.stop(); + } } - + @Test public void testServerWithHttpClientStreamContent() throws Exception { + String srvUrl = "http://127.0.0.1:" + ((NetworkConnector)_server.getConnectors()[0]).getLocalPort() + "/test/"; HttpClient client = new HttpClient(); - client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); - client.setRealmResolver(new SimpleRealmResolver(new TestRealm())); - client.start(); - - String srvUrl = "http://127.0.0.1:" + _server.getConnectors()[0].getLocalPort() + "/test/"; - - ContentExchange ex = new ContentExchange(); - ex.setMethod(HttpMethods.POST); - ex.setURL(srvUrl); - ex.setRequestContentSource(new BufferedInputStream(new FileInputStream("src/test/resources/message.txt"))); - - _received=null; - client.send(ex); - ex.waitForDone(); - - String sent = IO.toString(new FileInputStream("src/test/resources/message.txt")); - Assert.assertEquals(sent,_received); - Assert.assertEquals(200,ex.getResponseStatus()); - } - - public static class TestRealm implements Realm - { - public String getPrincipal() + try { - return "testuser"; + AuthenticationStore authStore = client.getAuthenticationStore(); + authStore.addAuthentication(new DigestAuthentication(srvUrl, "test", "testuser", "password")); + client.start(); + + String sent = IO.toString(new FileInputStream("src/test/resources/message.txt")); + + Request request = client.newRequest(srvUrl); + request.method(HttpMethod.POST); + request.content(new StringContentProvider(sent)); + _received=null; + ContentResponse response = request.send().get(5, TimeUnit.SECONDS); + + Assert.assertEquals(200,response.getStatus()); + Assert.assertEquals(sent,_received); + } - - public String getId() + finally { - return "test"; - } - - public String getCredentials() - { - return "password"; + client.stop(); } } + + public static class PostServlet extends HttpServlet { private static final long serialVersionUID = 1L; diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/rawhttp/HttpRequestTester.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/rawhttp/HttpRequestTester.java index c37d79b7d1a..eb32f29e76f 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/rawhttp/HttpRequestTester.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/rawhttp/HttpRequestTester.java @@ -18,20 +18,18 @@ package org.eclipse.jetty.test.support.rawhttp; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.ByteBuffer; import javax.servlet.http.Cookie; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpGenerator; -import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.http.HttpVersions; +import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.io.Buffer; -import org.eclipse.jetty.io.ByteArrayBuffer; -import org.eclipse.jetty.io.SimpleBuffers; -import org.eclipse.jetty.io.View; -import org.eclipse.jetty.io.bio.StringEndPoint; +import org.eclipse.jetty.util.BufferUtil; + /** * Assist in Generating Proper Raw HTTP Requests. If you want ultimate control @@ -162,16 +160,6 @@ public class HttpRequestTester fields.addDateField(name,date); } - /** - * @param name - * @param value - * @see org.eclipse.jetty.http.HttpFields#addLongField(java.lang.String, - * long) - */ - public void addLongHeader(String name, long value) - { - fields.addLongField(name,value); - } /** * @param cookie @@ -185,44 +173,63 @@ public class HttpRequestTester public String generate() throws IOException { - charset = defaultCharset; - Buffer contentTypeBuffer = fields.get(HttpHeaders.CONTENT_TYPE_BUFFER); - if (contentTypeBuffer != null) + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteBuffer header = null; + ByteBuffer chunk = null; + ByteBuffer content = null; + HttpVersion httpVersion = null; + if (version == null) { - String calcCharset = MimeTypes.getCharsetFromContentType(contentTypeBuffer); - if (calcCharset != null) + httpVersion = HttpVersion.HTTP_1_1; + } + else + { + httpVersion = httpVersion.fromString(version); + } + + HttpGenerator.RequestInfo info = new HttpGenerator.RequestInfo(httpVersion,fields,0,method,uri); + + HttpGenerator generator = new HttpGenerator(); + loop: while(!generator.isEnd()) + { + HttpGenerator.Result result = generator.generateRequest(info, header, chunk, content, true); + switch(result) { - this.charset = calcCharset; + case NEED_HEADER: + header=BufferUtil.allocate(8192); + continue; + + case NEED_CHUNK: + chunk=BufferUtil.allocate(HttpGenerator.CHUNK_SIZE); + continue; + + case NEED_INFO: + throw new IllegalStateException(); + + case FLUSH: + if (BufferUtil.hasContent(header)) + { + out.write(BufferUtil.toArray(header)); + BufferUtil.clear(header); + } + if (BufferUtil.hasContent(chunk)) + { + out.write(BufferUtil.toArray(chunk)); + BufferUtil.clear(chunk); + } + if (BufferUtil.hasContent(content)) + { + out.write(BufferUtil.toArray(content)); + BufferUtil.clear(content); + } + break; + + case SHUTDOWN_OUT: + break loop; } } - Buffer bb = new ByteArrayBuffer(32 * 1024 + (content != null?content.length:0)); - Buffer sb = new ByteArrayBuffer(4 * 1024); - StringEndPoint endp = new StringEndPoint(charset); - HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp); - - if (method != null) - { - generator.setRequest(getMethod(),getURI()); - if (version == null) - { - generator.setVersion(HttpVersions.HTTP_1_1_ORDINAL); - } - else - { - generator.setVersion(HttpVersions.CACHE.getOrdinal(HttpVersions.CACHE.lookup(version))); - } - - generator.completeHeader(fields,false); - - if (content != null) - { - generator.addContent(new View(new ByteArrayBuffer(content)),false); - } - } - - generator.complete(); - generator.flushBuffer(); - return endp.getOutput(); + return out.toString(); } }