You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2010/03/12 19:43:40 UTC
svn commit: r922386 -
/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Author: olegk
Date: Fri Mar 12 18:43:40 2010
New Revision: 922386
URL: http://svn.apache.org/viewvc?rev=922386&view=rev
Log:
Added a test case for content integrity
Modified:
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java?rev=922386&r1=922385&r2=922386&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java Fri Mar 12 18:43:40 2010
@@ -27,7 +27,12 @@
package org.apache.http.protocol;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
@@ -42,6 +47,7 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
+import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultHttpClientConnection;
@@ -594,4 +600,166 @@ public class TestHttpServiceAndExecutor
}
}
+ static class RepeatingEntity extends AbstractHttpEntity {
+
+ private final byte[] raw;
+ private int n;
+
+ public RepeatingEntity(final String content, String charset, int n) {
+ super();
+ if (content == null) {
+ throw new IllegalArgumentException("Content may not be null");
+ }
+ if (n <= 0) {
+ throw new IllegalArgumentException("N may not be negative or zero");
+ }
+ if (charset == null) {
+ charset = "US-ASCII";
+ }
+ byte[] b;
+ try {
+ b = content.getBytes(charset);
+ } catch (UnsupportedEncodingException ex) {
+ b = content.getBytes();
+ }
+ this.raw = b;
+ this.n = n;
+ }
+
+ public InputStream getContent() throws IOException, IllegalStateException {
+ throw new IllegalStateException("This method is not implemented");
+ }
+
+ public long getContentLength() {
+ return (this.raw.length + 2) * this.n;
+ }
+
+ public boolean isRepeatable() {
+ return true;
+ }
+
+ public boolean isStreaming() {
+ return false;
+ }
+
+ public void writeTo(final OutputStream outstream) throws IOException {
+ for (int i = 0; i < this.n; i++) {
+ outstream.write(this.raw);
+ outstream.write('\r');
+ outstream.write('\n');
+ }
+ outstream.flush();
+ }
+
+ }
+
+ public void testHttpContent() throws Exception {
+
+ String[] patterns = {
+
+ "0123456789ABCDEF",
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" +
+ "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that"
+ };
+
+ // Initialize the server-side request handler
+ this.server.registerHandler("*", new HttpRequestHandler() {
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+
+ if (request instanceof HttpEntityEnclosingRequest) {
+ int n = 1;
+ String s = request.getRequestLine().getUri();
+ if (s.startsWith("/?n=")) {
+ s = s.substring(4);
+ try {
+ n = Integer.parseInt(s);
+ if (n <= 0) {
+ throw new HttpException("Invalid request: " +
+ "number of repetitions cannot be negative or zero");
+ }
+ } catch (NumberFormatException ex) {
+ throw new HttpException("Invalid request: " +
+ "number of repetitions is invalid");
+ }
+ }
+
+ HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+ String line = EntityUtils.toString(incoming);
+ String charset = EntityUtils.getContentCharSet(incoming);
+
+ RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
+ outgoing.setChunked(n % 2 == 0);
+ response.setEntity(outgoing);
+ } else {
+ throw new HttpException("Invalid request: POST request expected");
+ }
+ }
+
+ });
+
+ this.server.start();
+ DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+ HttpHost host = new HttpHost("localhost", this.server.getPort());
+
+ try {
+ for (int i = 0; i < patterns.length; i++) {
+ String pattern = patterns[i];
+ for (int n = 1000; n < 1020; n++) {
+ if (!conn.isOpen()) {
+ Socket socket = new Socket(host.getHostName(), host.getPort());
+ conn.bind(socket, this.client.getParams());
+ }
+
+ BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest(
+ "POST", "/?n=" + n);
+ StringEntity outgoing = new StringEntity(pattern);
+ outgoing.setChunked(n % 2 == 0);
+ post.setEntity(outgoing);
+
+ HttpResponse response = this.client.execute(post, host, conn);
+ HttpEntity incoming = response.getEntity();
+ assertNotNull(incoming);
+ InputStream instream = incoming.getContent();
+ String charset = EntityUtils.getContentCharSet(incoming);
+ if (charset == null) {
+ charset = "US-ASCII";
+ }
+ assertNotNull(instream);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
+
+ String line;
+ int count = 0;
+ while ((line = reader.readLine()) != null) {
+ assertEquals(pattern, line);
+ count++;
+ }
+ assertEquals(n, count);
+ if (!this.client.keepAlive(response)) {
+ conn.close();
+ }
+ }
+ }
+ } finally {
+ conn.close();
+ this.server.shutdown();
+ }
+ }
+
}