392237 - Port test-integration to jetty-9

This commit is contained in:
Jan Bartel 2012-11-19 18:47:15 +11:00
parent 4bd36e3d38
commit 3642de3b8a
2 changed files with 106 additions and 100 deletions

View File

@ -18,18 +18,25 @@
package org.eclipse.jetty.test; package org.eclipse.jetty.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient; 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.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService; import org.eclipse.jetty.security.HashLoginService;
@ -217,67 +224,59 @@ public class DigestPostTest
@Test @Test
public void testServerWithHttpClientStringContent() throws Exception public void testServerWithHttpClientStringContent() throws Exception
{ {
String srvUrl = "http://127.0.0.1:" + ((NetworkConnector)_server.getConnectors()[0]).getLocalPort() + "/test/";
HttpClient client = new HttpClient(); HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setRealmResolver(new SimpleRealmResolver(new TestRealm())); try
{
AuthenticationStore authStore = client.getAuthenticationStore();
authStore.addAuthentication(new DigestAuthentication(srvUrl, "test", "testuser", "password"));
client.start(); client.start();
String srvUrl = "http://127.0.0.1:" + _server.getConnectors()[0].getLocalPort() + "/test/"; Request request = client.newRequest(srvUrl);
request.method(HttpMethod.POST);
ContentExchange ex = new ContentExchange(); request.content(new BytesContentProvider(__message.getBytes("UTF8")));
ex.setMethod(HttpMethods.POST);
ex.setURL(srvUrl);
ex.setRequestContent(new ByteArrayBuffer(__message,"UTF-8"));
_received=null; _received=null;
client.send(ex); ContentResponse response = request.send().get(5, TimeUnit.SECONDS);
ex.waitForDone();
Assert.assertEquals(__message,_received); Assert.assertEquals(__message,_received);
Assert.assertEquals(200,ex.getResponseStatus()); Assert.assertEquals(200,response.getStatus());
}
finally
{
client.stop();
}
} }
@Test @Test
public void testServerWithHttpClientStreamContent() throws Exception public void testServerWithHttpClientStreamContent() throws Exception
{ {
String srvUrl = "http://127.0.0.1:" + ((NetworkConnector)_server.getConnectors()[0]).getLocalPort() + "/test/";
HttpClient client = new HttpClient(); HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); try
client.setRealmResolver(new SimpleRealmResolver(new TestRealm())); {
AuthenticationStore authStore = client.getAuthenticationStore();
authStore.addAuthentication(new DigestAuthentication(srvUrl, "test", "testuser", "password"));
client.start(); 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")); 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); Assert.assertEquals(sent,_received);
Assert.assertEquals(200,ex.getResponseStatus());
}
finally
{
client.stop();
}
} }
public static class TestRealm implements Realm
{
public String getPrincipal()
{
return "testuser";
}
public String getId()
{
return "test";
}
public String getCredentials()
{
return "password";
}
}
public static class PostServlet extends HttpServlet public static class PostServlet extends HttpServlet
{ {

View File

@ -18,20 +18,18 @@
package org.eclipse.jetty.test.support.rawhttp; package org.eclipse.jetty.test.support.rawhttp;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpGenerator; import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.HttpVersions;
import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.util.BufferUtil;
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;
/** /**
* Assist in Generating Proper Raw HTTP Requests. If you want ultimate control * Assist in Generating Proper Raw HTTP Requests. If you want ultimate control
@ -162,16 +160,6 @@ public class HttpRequestTester
fields.addDateField(name,date); 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 * @param cookie
@ -185,44 +173,63 @@ public class HttpRequestTester
public String generate() throws IOException public String generate() throws IOException
{ {
charset = defaultCharset;
Buffer contentTypeBuffer = fields.get(HttpHeaders.CONTENT_TYPE_BUFFER);
if (contentTypeBuffer != null)
{
String calcCharset = MimeTypes.getCharsetFromContentType(contentTypeBuffer);
if (calcCharset != null)
{
this.charset = calcCharset;
}
}
Buffer bb = new ByteArrayBuffer(32 * 1024 + (content != null?content.length:0)); ByteArrayOutputStream out = new ByteArrayOutputStream();
Buffer sb = new ByteArrayBuffer(4 * 1024); ByteBuffer header = null;
StringEndPoint endp = new StringEndPoint(charset); ByteBuffer chunk = null;
HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp); ByteBuffer content = null;
HttpVersion httpVersion = null;
if (method != null)
{
generator.setRequest(getMethod(),getURI());
if (version == null) if (version == null)
{ {
generator.setVersion(HttpVersions.HTTP_1_1_ORDINAL); httpVersion = HttpVersion.HTTP_1_1;
} }
else else
{ {
generator.setVersion(HttpVersions.CACHE.getOrdinal(HttpVersions.CACHE.lookup(version))); httpVersion = httpVersion.fromString(version);
} }
generator.completeHeader(fields,false); HttpGenerator.RequestInfo info = new HttpGenerator.RequestInfo(httpVersion,fields,0,method,uri);
if (content != null) HttpGenerator generator = new HttpGenerator();
loop: while(!generator.isEnd())
{ {
generator.addContent(new View(new ByteArrayBuffer(content)),false); HttpGenerator.Result result = generator.generateRequest(info, header, chunk, content, true);
switch(result)
{
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;
} }
} }
generator.complete(); return out.toString();
generator.flushBuffer();
return endp.getOutput();
} }
} }