You are viewing a plain text version of this content. The canonical link for it is here.
Posted to awf-commits@incubator.apache.org by jm...@apache.org on 2012/02/12 20:24:36 UTC
svn commit: r1243316 [6/8] - in /incubator/deft/sandbox/jmeehan: ./
awf-core/ awf-core/.settings/ awf-core/src/ awf-core/src/main/
awf-core/src/main/assembly/ awf-core/src/main/java/
awf-core/src/main/java/org/ awf-core/src/main/java/org/apache/ awf-co...
Added: incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTest.java?rev=1243316&view=auto
==============================================================================
--- incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTest.java (added)
+++ incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTest.java Sun Feb 12 20:24:30 2012
@@ -0,0 +1,1216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.awf.web;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.ConnectException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.UnresolvedAddressException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.Response;
+
+import org.apache.awf.configuration.Configuration;
+import org.apache.awf.io.IOLoop;
+import org.apache.awf.io.timeout.Timeout;
+import org.apache.awf.web.Application;
+import org.apache.awf.web.AsyncCallback;
+import org.apache.awf.web.AsyncResult;
+import org.apache.awf.web.HttpServer;
+import org.apache.awf.web.SystemTestHandlers.*;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.client.AsynchronousHttpClient;
+import org.apache.awf.web.http.protocol.HttpStatus;
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.ProtocolVersion;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import static org.apache.awf.web.SystemTestHandlers.expectedPayload;
+
+/**
+ * General tests of server operation.
+ */
+public class SystemTest {
+
+ private static final int PORT = 8081;
+
+ @BeforeClass
+ public static void setup() {
+
+ final Map<String, RequestHandler> reqHandlers = new HashMap<String, RequestHandler>();
+ reqHandlers.put("/", new ExampleRequestHandler());
+ reqHandlers.put("/w", new WRequestHandler());
+ reqHandlers.put("/ww", new WWRequestHandler());
+ reqHandlers.put("/wwfw", new WWFWRequestHandler());
+ reqHandlers.put("/wfwf", new WFWFRequestHandler());
+ reqHandlers.put("/wfffwfff", new WFFFWFFFRequestHandler());
+ reqHandlers.put("/delete", new DeleteRequestHandler());
+ reqHandlers.put("/post", new PostRequestHandler());
+ reqHandlers.put("/put", new PutRequestHandler());
+ reqHandlers.put("/capturing/([0-9]+)", new CapturingRequestRequestHandler());
+ reqHandlers.put("/throw", new ThrowingHttpExceptionRequestHandler());
+ reqHandlers.put("/async_throw", new AsyncThrowingHttpExceptionRequestHandler());
+ reqHandlers.put("/no_body", new NoBodyRequestHandler());
+ reqHandlers.put("/moved_perm", new MovedPermanentlyRequestHandler());
+ reqHandlers.put("/static_file_handler", new UserDefinedStaticContentHandler());
+ reqHandlers.put("/keyvalue", new KeyValueStoreExampleRequestHandler());
+ reqHandlers.put("/450kb_body", new _450KBResponseEntityRequestHandler());
+ reqHandlers.put("/echo", new EchoingPostBodyRequestHandler());
+ reqHandlers.put("/authenticated", new AuthenticatedRequestHandler());
+ reqHandlers.put("/query_params", new QueryParamsRequestHandler());
+ reqHandlers.put("/chunked", new ChunkedRequestHandler());
+
+ /*
+ * Start server instance from a new thread because the start invocation
+ * is blocking (invoking thread will be I/O loop thread).
+ */
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+
+ Configuration configuration = new Configuration();
+ configuration.setCreateETags(true);
+ configuration.setStaticDirectory("src/test/resources");
+
+ HttpServer server = new HttpServer(configuration) {
+ protected Application createApplication(String packageName) {
+ return new Application(reqHandlers);
+ }
+ };
+ server.listen(PORT);
+ IOLoop.INSTANCE.start();
+ }
+ }).start();
+ }
+
+ @AfterClass
+ public static void tearDown() throws InterruptedException {
+
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ @Override
+ public void onCallback() {
+ IOLoop.INSTANCE.stop();
+ }
+ });
+ Thread.sleep(300);
+ }
+
+ @Test
+ public void simpleGetRequestTest() throws ClientProtocolException, IOException {
+ doSimpleGetRequest();
+ }
+
+ /**
+ * Test a RH that does a single write
+ *
+ * @throws ClientProtocolException
+ * @throws IOException
+ */
+ @Test
+ public void wTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/w");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("1", payLoad);
+ assertEquals(5, response.getAllHeaders().length);
+ assertEquals("1", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ @Test
+ public void wwTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/ww");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("12", payLoad);
+ assertEquals(5, response.getAllHeaders().length);
+ assertEquals("2", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ @Test
+ public void wwfwTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/wwfw");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("123", payLoad);
+ assertEquals(3, response.getAllHeaders().length);
+ }
+
+ @Test
+ public void wfwfTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/wfwf");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("12", payLoad);
+ assertEquals(3, response.getAllHeaders().length);
+ }
+
+ @Test
+ public void wfffwfffTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/wfffwfff");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("12", payLoad);
+ assertEquals(3, response.getAllHeaders().length);
+ }
+
+ @Test
+ public void deleteTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpDelete httpdelete = new HttpDelete("http://localhost:" + PORT + "/delete");
+ HttpResponse response = httpclient.execute(httpdelete);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("delete", payLoad);
+ }
+
+ @Test
+ public void PostTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/post");
+ HttpResponse response = httpclient.execute(httppost);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("post", payLoad);
+ }
+
+ @Test
+ public void postWithContinueTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+ params.setParameter("http.protocol.expect-continue", Boolean.TRUE);
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/post");
+ HttpResponse response = httpclient.execute(httppost);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("post", payLoad);
+ }
+
+ @Test
+ public void putTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpPut httpput = new HttpPut("http://localhost:" + PORT + "/put");
+ HttpResponse response = httpclient.execute(httpput);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("put", payLoad);
+ }
+
+ @Test
+ public void capturingTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/capturing/1911");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("/capturing/1911", payLoad);
+ }
+
+ @Test
+ public void erroneousCapturingTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/capturing/r1911");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.CLIENT_ERROR_NOT_FOUND.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("Not Found", response.getStatusLine().getReasonPhrase());
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("Requested URL: /capturing/r1911 was not found", payLoad);
+ }
+
+ @Test
+ public void simpleConcurrentGetRequestTest() {
+
+ int nThreads = 8;
+ int nRequests = 2048;
+ final CountDownLatch latch = new CountDownLatch(nRequests);
+ ExecutorService executor = Executors.newFixedThreadPool(nThreads);
+
+ for (int i = 1; i <= nRequests; i++) {
+ executor.submit(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ doSimpleGetRequest();
+ latch.countDown();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ });
+ }
+ try {
+ latch.await(15 * 1000, TimeUnit.MILLISECONDS); // max wait time
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ if (latch.getCount() != 0) {
+ assertTrue("Did not finish " + nRequests + " # of requests", false);
+ }
+ }
+
+ @Test
+ public void keepAliveRequestTest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Keep-Alive"));
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ DefaultHttpClient httpclient = new DefaultHttpClient(params);
+
+ for (int i = 1; i <= 25; i++) {
+ doKeepAliveRequestTest(httpclient);
+ }
+ }
+
+ private void doKeepAliveRequestTest(DefaultHttpClient httpclient) throws IOException, ClientProtocolException {
+
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals(expectedPayload, payLoad);
+ }
+
+ @Test
+ public void HTTP_1_0_noConnectionHeaderTest() throws ClientProtocolException, IOException {
+
+ HttpParams params = new BasicHttpParams();
+ HttpProtocolParams.setVersion(params, new ProtocolVersion("HTTP", 1, 0));
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals(expectedPayload, payLoad);
+ }
+
+ @Test
+ public void httpExceptionTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/throw");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SERVER_ERROR_INTERNAL_SERVER_ERROR.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("Internal Server Error", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("exception message", payLoad);
+ }
+
+ @Test
+ public void asyncHttpExceptionTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/async_throw");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SERVER_ERROR_INTERNAL_SERVER_ERROR.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("Internal Server Error", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("exception message", payLoad);
+ }
+
+ @Test
+ public void staticFileRequestTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/src/test/resources/test.txt");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(7, response.getAllHeaders().length);
+
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("test.txt", payLoad);
+ }
+
+ @Test
+ public void pictureStaticFileRequestTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/src/test/resources/n792205362_2067.jpg");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(7, response.getAllHeaders().length);
+ assertEquals("54963", response.getFirstHeader("Content-Length").getValue());
+ assertEquals("image/jpeg", response.getFirstHeader("Content-Type").getValue());
+ assertNotNull(response.getFirstHeader("Last-Modified"));
+ }
+
+ @Test
+ public void pictureStaticLargeFileRequestTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/src/test/resources/f4_impact_1_original.jpg");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(7, response.getAllHeaders().length);
+ // assertEquals("2145094",
+ // response.getFirstHeader("Content-Length").getValue()); // my mb says
+ // 2145066, imac says 2145094
+ assertEquals("image/jpeg", response.getFirstHeader("Content-Type").getValue());
+ assertNotNull(response.getFirstHeader("Last-Modified"));
+ // TODO RS 101026 Verify that the actual body/entity is 2145094 bytes
+ // big (when we have support for "large" file)
+ }
+
+ @Test
+ public void noBodyRequest() throws ClientProtocolException, IOException {
+
+ HttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/no_body");
+ HttpResponse response = httpclient.execute(httpget);
+ List<String> expectedHeaders = Arrays.asList(new String[] { "Server", "Date", "Content-Length", "Connection" });
+
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ assertEquals(expectedHeaders.size(), response.getAllHeaders().length);
+
+ for (String header : expectedHeaders) {
+ assertTrue(response.getFirstHeader(header) != null);
+ }
+
+ assertEquals("", convertStreamToString(response.getEntity().getContent()).trim());
+ assertEquals("0", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ @Test
+ public void movedPermanentlyRequest() throws ClientProtocolException, IOException {
+
+ HttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/moved_perm");
+ HttpResponse response = httpclient.execute(httpget);
+ List<String> expectedHeaders = Arrays.asList(new String[] { "Server", "Date", "Content-Length", "Connection",
+ "Etag" });
+
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ assertEquals(expectedHeaders.size(), response.getAllHeaders().length);
+
+ for (String header : expectedHeaders) {
+ assertTrue(response.getFirstHeader(header) != null);
+ }
+
+ assertEquals(expectedPayload, convertStreamToString(response.getEntity().getContent()).trim());
+ assertEquals(expectedPayload.length() + "", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ @Test
+ public void sendGarbageTest() throws IOException {
+
+ InetSocketAddress socketAddress = new InetSocketAddress(PORT);
+ SocketChannel channel = SocketChannel.open(socketAddress);
+ channel.write(ByteBuffer.wrap(new byte[] { 1, 1, 1, 1 } // garbage
+ ));
+ channel.close();
+ }
+
+ @Test
+ public void userDefinedStaticContentHandlerTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/static_file_handler");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(4, response.getAllHeaders().length);
+ assertEquals("8", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ @Test
+ public void timeoutTest() throws InterruptedException {
+
+ long now = System.currentTimeMillis();
+ final CountDownLatch latch = new CountDownLatch(5);
+ final AsyncCallback cb = new AsyncCallback() {
+
+ @Override
+ public void onCallback() {
+ latch.countDown();
+ }
+
+ };
+
+ Timeout t1 = new Timeout(now + 1000, cb);
+ Timeout t2 = new Timeout(now + 1200, cb);
+ Timeout t3 = new Timeout(now + 1400, cb);
+ Timeout t4 = new Timeout(now + 1600, cb);
+ Timeout t5 = new Timeout(now + 1800, cb);
+ IOLoop.INSTANCE.addTimeout(t1);
+ IOLoop.INSTANCE.addTimeout(t2);
+ IOLoop.INSTANCE.addTimeout(t3);
+ IOLoop.INSTANCE.addTimeout(t4);
+ IOLoop.INSTANCE.addTimeout(t5);
+
+ latch.await(5 * 1000, TimeUnit.MILLISECONDS);
+ assertTrue(latch.getCount() == 0);
+ }
+
+ @Test
+ public void callbackTest() throws InterruptedException {
+ final CountDownLatch latch = new CountDownLatch(5);
+ final AsyncCallback cb = new AsyncCallback() {
+
+ @Override
+ public void onCallback() {
+ latch.countDown();
+ }
+
+ };
+ IOLoop.INSTANCE.addCallback(cb);
+ IOLoop.INSTANCE.addCallback(cb);
+ IOLoop.INSTANCE.addCallback(cb);
+ IOLoop.INSTANCE.addCallback(cb);
+ IOLoop.INSTANCE.addCallback(cb);
+
+ latch.await(5 * 1000, TimeUnit.MILLISECONDS);
+ assertTrue(latch.getCount() == 0);
+ }
+
+ @Test
+ public void keyValueStoreClientTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/keyvalue");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+ assertEquals("7", response.getFirstHeader("Content-Length").getValue());
+ assertEquals("kickass", convertStreamToString(response.getEntity().getContent()).trim());
+ }
+
+ // ning === http://github.com/ning/async-http-client
+ @Test
+ public void doSimpleAsyncRequestTestWithNing() throws IOException, InterruptedException {
+
+ int iterations = 100;
+ final CountDownLatch latch = new CountDownLatch(iterations);
+ AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
+ for (int i = 1; i <= iterations; i++) {
+
+ asyncHttpClient.prepareGet("http://localhost:" + PORT + "/").execute(
+ new AsyncCompletionHandler<com.ning.http.client.Response>() {
+
+ @Override
+ public com.ning.http.client.Response onCompleted(com.ning.http.client.Response response)
+ throws Exception {
+ String body = response.getResponseBody();
+ assertEquals(expectedPayload, body);
+ {
+ List<String> expectedHeaders = Arrays.asList(new String[] { "Server", "Date",
+ "Content-Length", "Etag", "Connection" });
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusCode());
+ assertEquals(expectedHeaders.size(), response.getHeaders().size());
+ for (String header : expectedHeaders) {
+ assertTrue(response.getHeader(header) != null);
+ }
+ assertEquals(expectedPayload.length() + "", response.getHeader("Content-Length"));
+ }
+ latch.countDown();
+ return response;
+ }
+
+ @Override
+ public void onThrowable(Throwable t) {
+ assertTrue(false);
+ }
+
+ });
+ }
+ latch.await(15 * 1000, TimeUnit.MILLISECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ // TODO 101108 RS enable when /mySql (AsyncDbHandler is properly
+ // implemented)
+ // ning === http://github.com/ning/async-http-client
+ // @Test
+ // public void doAsynchronousRequestTestWithNing() throws IOException,
+ // InterruptedException {
+ // int iterations = 200;
+ // final CountDownLatch latch = new CountDownLatch(iterations);
+ // AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
+ // for (int i = 1; i <= iterations; i++) {
+ //
+ // asyncHttpClient.prepareGet("http://localhost:" + PORT + "/mySql").
+ // execute(new AsyncCompletionHandler<com.ning.http.client.Response>(){
+ //
+ // @Override
+ // public com.ning.http.client.Response
+ // onCompleted(com.ning.http.client.Response response) throws Exception{
+ // String body = response.getResponseBody();
+ // assertEquals("Name: Jim123", body);
+ // List<String> expectedHeaders = Arrays.asList(new String[] {"Server",
+ // "Date", "Content-Length", "Etag", "Connection"});
+ // assertEquals(200, response.getStatusCode());
+ // assertEquals(expectedHeaders.size(),
+ // response.getHeaders().getHeaderNames().size());
+ // for (String header : expectedHeaders) {
+ // assertTrue(response.getHeader(header) != null);
+ // }
+ // assertEquals(""+ "Name: Jim123".length(),
+ // response.getHeader("Content-Length"));
+ // latch.countDown();
+ // return response;
+ // }
+ //
+ // @Override
+ // public void onThrowable(Throwable t){
+ // assertTrue(false);
+ // }
+ //
+ // });
+ // }
+ // latch.await(15 * 1000, TimeUnit.MILLISECONDS);
+ // assertEquals(0, latch.getCount());
+ // }
+
+ @Test
+ public void _450KBEntityTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/450kb_body");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+ // assertEquals(450*1024,
+ // Integer.parseInt(response.getFirstHeader("Content-Length").getValue())/8);
+ // assertEquals(450*1024,
+ // _450KBResponseEntityRequestHandlr.entity.getBytes(Charsets.UTF_8).length);
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals(_450KBResponseEntityRequestHandler.entity, payLoad);
+ }
+
+ @Test
+ public void smallHttpPostBodyWithUnusualCharactersTest() throws ClientProtocolException, IOException {
+
+ final String body = "Räger Schildmäijår";
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/echo");
+ httppost.setEntity(new StringEntity(body)); // HTTP 1.1 says that the
+ // default charset is
+ // ISO-8859-1
+ HttpResponse response = httpclient.execute(httppost);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals(body, payLoad);
+ }
+
+ @Test
+ public void smallHttpPostBodyTest() throws ClientProtocolException, IOException, InterruptedException {
+
+ final String body = "Roger Schildmeijer";
+ final CountDownLatch latch = new CountDownLatch(1);
+ AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
+ asyncHttpClient.preparePost("http://localhost:" + PORT + "/echo").setBody(body).execute(
+ new AsyncCompletionHandler<Response>() {
+
+ @Override
+ public Response onCompleted(Response response) throws Exception {
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusCode());
+ assertEquals("OK", response.getStatusText());
+ assertEquals(5, response.getHeaders().size());
+ String payLoad = response.getResponseBody();
+ assertEquals(body, payLoad);
+ latch.countDown();
+ return response;
+ }
+
+ @Override
+ public void onThrowable(Throwable t) {
+ }
+ });
+
+ latch.await();
+ assertTrue(latch.getCount() == 0);
+ }
+
+ @Test
+ public void largeHttpPostBodyTest() throws ClientProtocolException, IOException, InterruptedException {
+
+ String body = "Roger Schildmeijer: 0\n";
+ for (int i = 1; i <= 1000; i++) {
+ body += "Roger Schildmeijer: " + i + "\n";
+ }
+ final String expectedBody = body;
+ final CountDownLatch latch = new CountDownLatch(1);
+ AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
+ asyncHttpClient.preparePost("http://localhost:" + PORT + "/echo").setBody(body).execute(
+ new AsyncCompletionHandler<Response>() {
+
+ @Override
+ public Response onCompleted(Response response) throws Exception {
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusCode());
+ assertEquals("OK", response.getStatusText());
+ assertEquals(5, response.getHeaders().size());
+ String payLoad = response.getResponseBody();
+ assertEquals(expectedBody, payLoad);
+ latch.countDown();
+ return response;
+ }
+
+ @Override
+ public void onThrowable(Throwable t) {
+ }
+ });
+
+ latch.await();
+ assertTrue(latch.getCount() == 0);
+ }
+
+ @Test
+ public void authenticatedRequestHandlerTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/authenticated");
+ httpget.setHeader("user", "Roger Schildmeijer");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("Roger Schildmeijer", payLoad);
+ }
+
+ @Test
+ public void notAuthenticatedRequestHandlerTest() throws ClientProtocolException, IOException {
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/authenticated");
+ httpget.setHeader("wrong_header", "Roger Schildmeijer");
+ HttpResponse response = httpclient.execute(httpget);
+
+ assertNotNull(response);
+ assertEquals(HttpStatus.CLIENT_ERROR_FORBIDDEN.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("Forbidden", response.getStatusLine().getReasonPhrase());
+ assertEquals(5, response.getAllHeaders().length);
+ String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+ assertEquals("Authentication failed", payLoad);
+ }
+
+ @Test
+ public void queryParamsTest() throws ClientProtocolException, IOException {
+
+ HttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/query_params?key1=value1&key2=value2");
+ HttpResponse response = httpclient.execute(httpget);
+ List<String> expectedHeaders = Arrays.asList(new String[] { "Server", "Date", "Content-Length", "Etag",
+ "Connection" });
+
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ assertEquals(expectedHeaders.size(), response.getAllHeaders().length);
+
+ for (String header : expectedHeaders) {
+ assertTrue(response.getFirstHeader(header) != null);
+ }
+
+ final String expected = "value1 value2";
+ assertEquals(expected, convertStreamToString(response.getEntity().getContent()).trim());
+ assertEquals(expected.length() + "", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ /**
+ * TODO SLM This test does not make sense since stop use a callback and server store only one serverChannel
+ * @Test
+ */
+ public void multipleStartStopCombinations() throws InterruptedException {
+
+ Configuration configuration = new Configuration();
+ configuration.setHandlerPackage(SystemTest.class.getPackage().getName());
+
+ final HttpServer server = new HttpServer(configuration);
+
+ final int n = 10;
+ final CountDownLatch latch = new CountDownLatch(n);
+ for (int i = 0; i < n; i++) {
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ server.listen(PORT + 1);
+ }
+ });
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ server.stop();
+ latch.countDown();
+ }
+ });
+ }
+ latch.await(50, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ @Test
+ public void connectToUnresolvableAddressUsingAsynchronousHttpClient() throws InterruptedException {
+
+ final String unresolvableAddress = "http://ttasfdqwertyuiop.se./start";
+ final CountDownLatch latch = new CountDownLatch(1);
+ final AsynchronousHttpClient client = new AsynchronousHttpClient();
+ final AsyncCallback runByIOLoop = new AsyncCallback() {
+
+ public void onCallback() {
+ client.get(unresolvableAddress, new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response result) {
+ client.close();
+ }
+
+ public void onFailure(Throwable caught) {
+ if (caught instanceof UnresolvedAddressException) {
+ latch.countDown();
+ }
+ client.close();
+ }
+ });
+ }
+ };
+ IOLoop.INSTANCE.addCallback(runByIOLoop);
+
+ latch.await(30, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ @Test
+ public void connectToUnconnectableAddressUsingAsynchronousHttpClient() throws InterruptedException {
+
+ final String unconnectableAddress = "http://localhost:8039/start";
+ final CountDownLatch latch = new CountDownLatch(1);
+ final AsynchronousHttpClient client = new AsynchronousHttpClient();
+ final AsyncCallback runByIOLoop = new AsyncCallback() {
+
+ public void onCallback() {
+ client.get(unconnectableAddress, new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response result) {
+ client.close();
+ }
+
+ public void onFailure(Throwable caught) {
+ if (caught instanceof ConnectException) {
+ latch.countDown();
+ }
+ client.close();
+ }
+ });
+ }
+ };
+ IOLoop.INSTANCE.addCallback(runByIOLoop);
+
+ latch.await(30, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ @Test
+ public void multipleAsynchronousHttpClientTest() throws InterruptedException {
+
+ for (int i = 0; i < 100; i++) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final String url = "http://localhost:" + PORT + "/";
+ final AsynchronousHttpClient http = new AsynchronousHttpClient();
+ final String[] result = { "BODY_PLACEHOLDER", "STATUSCODE_PLACEHOLDER" };
+ final AsyncResult<org.apache.awf.web.http.client.Response> cb = new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response response) {
+ result[0] = response.getBody();
+ result[1] = response.getStatusLine();
+ latch.countDown();
+ }
+
+ public void onFailure(Throwable ignore) {
+ }
+ };
+ // make sure that the http.fetch(..) is invoked from the ioloop
+ // thread
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ http.get(url, cb);
+ }
+ });
+ latch.await(30, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ assertEquals("hello test", result[0]);
+ assertEquals("HTTP/1.1 200 OK", result[1]);
+ }
+ }
+
+ @Test
+ public void AsynchronousHttpClientConnectionFailedTest() throws InterruptedException {
+
+ final CountDownLatch latch = new CountDownLatch(1);
+ final String url = "http://localhost:" + (PORT + 1) + "/";
+ final AsynchronousHttpClient http = new AsynchronousHttpClient();
+ final AsyncResult<org.apache.awf.web.http.client.Response> cb = new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response response) {
+ }
+
+ public void onFailure(Throwable e) {
+ if (e instanceof ConnectException) {
+ latch.countDown();
+ }
+ }
+ };
+ // make sure that the http.fetch(..) is invoked from the ioloop thread
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ http.get(url, cb);
+ }
+ });
+ latch.await(5, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ @Test
+ public void AsynchronousHttpClientRedirectTest() throws InterruptedException {
+
+ final CountDownLatch latch = new CountDownLatch(1);
+ // final String url = "http://localhost:" + (PORT) + "/moved_perm";
+ final String url = "http://localhost:" + PORT + "/moved_perm";
+ final AsynchronousHttpClient http = new AsynchronousHttpClient();
+ final AsyncResult<org.apache.awf.web.http.client.Response> cb = new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response response) {
+ if (response.getBody().equals(expectedPayload)) {
+ latch.countDown();
+ }
+ }
+
+ public void onFailure(Throwable e) {
+ }
+
+ };
+ // make sure that the http.fetch(..) is invoked from the ioloop thread
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ http.get(url, cb);
+ }
+ });
+ latch.await(10, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ @Test
+ public void asynchronousHttpClientTransferEncodingChunkedTest() throws InterruptedException {
+
+ final CountDownLatch latch = new CountDownLatch(1);
+ final String url = "http://localhost:" + PORT + "/chunked";
+ final AsynchronousHttpClient http = new AsynchronousHttpClient();
+ final AsyncResult<org.apache.awf.web.http.client.Response> cb = new AsyncResult<org.apache.awf.web.http.client.Response>() {
+
+ public void onSuccess(org.apache.awf.web.http.client.Response response) {
+ if (response.getBody().equals("arogerab") && response.getHeader("Transfer-Encoding").equals("chunked")) {
+ latch.countDown();
+ }
+ }
+
+ public void onFailure(Throwable e) {
+ }
+
+ };
+ // make sure that the http.fetch(..) is invoked from the ioloop thread
+ IOLoop.INSTANCE.addCallback(new AsyncCallback() {
+ public void onCallback() {
+ http.get(url, cb);
+ }
+ });
+ latch.await(10, TimeUnit.SECONDS);
+ assertEquals(0, latch.getCount());
+ }
+
+ private void doSimpleGetRequest() throws ClientProtocolException, IOException {
+
+ List<Header> headers = new LinkedList<Header>();
+ headers.add(new BasicHeader("Connection", "Close"));
+
+ HttpParams params = new BasicHttpParams();
+ params.setParameter("http.default-headers", headers);
+
+ HttpClient httpclient = new DefaultHttpClient(params);
+ HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/");
+ HttpResponse response = httpclient.execute(httpget);
+ List<String> expectedHeaders = Arrays.asList(new String[] { "Server", "Date", "Content-Length", "Etag",
+ "Connection" });
+
+ assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+ assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+ assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+ assertEquals(expectedHeaders.size(), response.getAllHeaders().length);
+
+ for (String header : expectedHeaders) {
+ assertTrue(response.getFirstHeader(header) != null);
+ }
+
+ assertEquals(expectedPayload, convertStreamToString(response.getEntity().getContent()).trim());
+ assertEquals(expectedPayload.length() + "", response.getFirstHeader("Content-Length").getValue());
+ }
+
+ private String convertStreamToString(InputStream is) throws IOException {
+
+ if (is != null) {
+ StringBuilder sb = new StringBuilder();
+ String line;
+
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+ while ((line = reader.readLine()) != null) {
+ sb.append(line).append("\n");
+ }
+ } finally {
+ is.close();
+ }
+
+ return sb.toString();
+ }
+
+ return "";
+ }
+}
Added: incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTestHandlers.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTestHandlers.java?rev=1243316&view=auto
==============================================================================
--- incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTestHandlers.java (added)
+++ incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/SystemTestHandlers.java Sun Feb 12 20:24:30 2012
@@ -0,0 +1,272 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.awf.web;
+
+import java.io.File;
+
+import org.apache.awf.annotation.Asynchronous;
+import org.apache.awf.annotation.Authenticated;
+import org.apache.awf.example.keyvalue.KeyValueStore;
+import org.apache.awf.example.keyvalue.KeyValueStoreClient;
+import org.apache.awf.web.AsyncResult;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpException;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.protocol.HttpStatus;
+
+/**
+ * A collection of <code>RequestHandler</code>s used by
+ * <code>SystemTest</code>.
+ */
+public class SystemTestHandlers {
+
+ public static final String expectedPayload = "hello test";
+
+ public static class ExampleRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(expectedPayload);
+ }
+ }
+
+ public static class WRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("1");
+ }
+ }
+
+ public static class WWRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("1");
+ response.write("2");
+ }
+ }
+
+ public static class WWFWRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("1");
+ response.write("2");
+ response.flush();
+ response.write("3");
+ }
+ }
+
+ public static class WFWFRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("1");
+ response.flush();
+ response.write("2");
+ response.flush();
+ }
+ }
+
+ public static class WFFFWFFFRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("1");
+ response.flush();
+ response.flush();
+ response.flush();
+ response.write("2");
+ response.flush();
+ response.flush();
+ response.flush();
+ }
+ }
+
+ public static class DeleteRequestHandler extends RequestHandler {
+ @Override
+ public void delete(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("del");
+ response.flush();
+ response.write("ete");
+ response.flush();
+ }
+ }
+
+ public static class PostRequestHandler extends RequestHandler {
+ @Override
+ public void post(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("po");
+ response.flush();
+ response.write("st");
+ response.flush();
+ }
+ }
+
+ public static class PutRequestHandler extends RequestHandler {
+ @Override
+ public void put(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write("p");
+ response.flush();
+ response.write("ut");
+ response.flush();
+ }
+ }
+
+ public static class CapturingRequestRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(request.getRequestedPath());
+ }
+ }
+
+ public static class ThrowingHttpExceptionRequestHandler extends RequestHandler {
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ throw new HttpException(HttpStatus.SERVER_ERROR_INTERNAL_SERVER_ERROR, "exception message");
+ }
+ }
+
+ public static class AsyncThrowingHttpExceptionRequestHandler extends RequestHandler {
+ @Asynchronous
+ @Override
+ public void get(org.apache.awf.web.http.HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ throw new HttpException(HttpStatus.SERVER_ERROR_INTERNAL_SERVER_ERROR, "exception message");
+ }
+ }
+
+ public static class NoBodyRequestHandler extends RequestHandler {
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.setStatus(HttpStatus.SUCCESS_OK);
+ }
+ }
+
+ public static class MovedPermanentlyRequestHandler extends RequestHandler {
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.setStatus(HttpStatus.REDIRECTION_MOVED_PERMANENTLY);
+ response.setHeader("Location", "/");
+ }
+ }
+
+ public static class UserDefinedStaticContentHandler extends RequestHandler {
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(new File("src/test/resources/test.txt"));
+ }
+ }
+
+ public static class KeyValueStoreExampleRequestHandler extends RequestHandler {
+
+ public final KeyValueStoreClient client = new KeyValueStoreClient(KeyValueStore.HOST, KeyValueStore.PORT);
+
+ public KeyValueStoreExampleRequestHandler() {
+ new KeyValueStore().start();
+ client.connect();
+ }
+
+ @Override
+ @Asynchronous
+ public void get(HttpRequest request, final org.apache.awf.web.http.HttpResponse response) {
+ client.get("AWF", new AsyncResult<byte[]>() {
+ @Override
+ public void onFailure(Throwable caught) { /* ignore */
+ }
+
+ @Override
+ public void onSuccess(byte[] result) {
+ response.write(new String(result)).finish();
+ }
+ });
+ }
+
+ }
+
+ public static class _450KBResponseEntityRequestHandler extends RequestHandler {
+ public static String entity;
+
+ static {
+ int iterations = 450 * 1024;
+ StringBuilder sb = new StringBuilder();
+ for (int i = 1; i <= iterations; i++) {
+ sb.append("a");
+ }
+ entity = sb.toString();
+ }
+
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(entity);
+ }
+ }
+
+ public static class EchoingPostBodyRequestHandler extends RequestHandler {
+ @Override
+ public void post(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(request.getBody());
+ }
+ }
+
+ public static class AuthenticatedRequestHandler extends RequestHandler {
+ @Override
+ @Authenticated
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(request.getHeader("user"));
+ }
+
+ @Override
+ public String getCurrentUser(HttpRequest request) {
+ return request.getHeader("user");
+ }
+ }
+
+ public static class QueryParamsRequestHandler extends RequestHandler {
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.write(request.getParameter("key1") + " " + request.getParameter("key2"));
+ }
+ }
+
+ public static class ChunkedRequestHandler extends RequestHandler {
+ @Override
+ public void get(HttpRequest request, org.apache.awf.web.http.HttpResponse response) {
+ response.setHeader("Transfer-Encoding", "chunked");
+ sleep(300);
+
+ response.write("1\r\n");
+ response.write("a\r\n").flush();
+ sleep(300);
+
+ response.write("5\r\n");
+ response.write("roger\r\n").flush();
+ sleep(300);
+
+ response.write("2\r\n");
+ response.write("ab\r\n").flush();
+ sleep(300);
+
+ response.write("0\r\n");
+ response.write("\r\n");
+ }
+
+ public static void sleep(long ms) {
+ try {
+ Thread.sleep(ms);
+ } catch (InterruptedException ignore) { /* nop */
+ }
+ }
+ }
+}
Added: incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerFactoryTest.java?rev=1243316&view=auto
==============================================================================
--- incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerFactoryTest.java (added)
+++ incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerFactoryTest.java Sun Feb 12 20:24:30 2012
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.awf.web.handler;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.handler.RequestHandlerFactory;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link RequestHandlerFactory}.
+ */
+public class RequestHandlerFactoryTest {
+
+ private class TestRequestHandler extends RequestHandler {
+ // Do nothing.
+ }
+
+ @Test
+ public void testCloneHandler() {
+
+ TestRequestHandler handler = new TestRequestHandler();
+
+ RequestHandler created = RequestHandlerFactory.cloneHandler(handler);
+ assertEquals(handler.getClass(), created.getClass());
+ }
+
+ @Test
+ public void testCloneHandlerHandlesNull() {
+
+ RequestHandler created = RequestHandlerFactory.cloneHandler(null);
+ assertNull(created);
+ }
+}
Added: incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerTest.java?rev=1243316&view=auto
==============================================================================
--- incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerTest.java (added)
+++ incubator/deft/sandbox/jmeehan/awf-core/src/test/java/org/apache/awf/web/handler/RequestHandlerTest.java Sun Feb 12 20:24:30 2012
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.awf.web.handler;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.apache.awf.annotation.Asynchronous;
+import org.apache.awf.annotation.Authenticated;
+import org.apache.awf.web.handler.RequestHandler;
+import org.apache.awf.web.http.HttpRequest;
+import org.apache.awf.web.http.HttpResponse;
+import org.apache.awf.web.http.protocol.HttpStatus;
+import org.apache.awf.web.http.protocol.HttpVerb;
+import org.junit.Test;
+
+public class RequestHandlerTest {
+
+ static class RequestHandler1 extends RequestHandler {
+
+ @Override
+ @Asynchronous
+ @Authenticated
+ public void get(HttpRequest request, HttpResponse response) {
+ }
+ }
+
+ static class RequestHandler2 extends RequestHandler {
+
+ @Override
+ public void get(HttpRequest request, HttpResponse response) {
+ }
+
+ @Override
+ @Asynchronous
+ @Authenticated
+ public void post(HttpRequest request, HttpResponse response) {
+ }
+
+ }
+
+ private class TestRequestHandler extends RequestHandler {
+ // Do nothing.
+ }
+
+ @Test
+ public void testAsynchronousAnnotations() {
+
+ RequestHandler rh1 = new RequestHandler1();
+ RequestHandler rh2 = new RequestHandler2();
+
+ assertTrue(rh1.isMethodAsynchronous(HttpVerb.GET));
+
+ assertFalse(rh2.isMethodAsynchronous(HttpVerb.GET));
+ assertTrue(rh2.isMethodAsynchronous(HttpVerb.POST));
+ }
+
+ @Test
+ public void testAuthenticatedAnnotations() {
+ RequestHandler rh1 = new RequestHandler1();
+ RequestHandler rh2 = new RequestHandler2();
+
+ assertTrue(rh1.isMethodAuthenticated(HttpVerb.GET));
+ assertFalse(rh1.isMethodAuthenticated(HttpVerb.POST));
+ assertFalse(rh1.isMethodAuthenticated(HttpVerb.DELETE));
+
+ assertFalse(rh2.isMethodAuthenticated(HttpVerb.GET));
+ assertFalse(rh2.isMethodAuthenticated(HttpVerb.PUT));
+ assertTrue(rh2.isMethodAuthenticated(HttpVerb.POST));
+ }
+
+ @Test
+ public void testGet() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ new TestRequestHandler().get(request, response);
+ verify(response, times(1)).setStatus(HttpStatus.SERVER_ERROR_NOT_IMPLEMENTED);
+ verify(response, times(1)).write("");
+ }
+
+ @Test
+ public void testPost() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ new TestRequestHandler().post(request, response);
+ verify(response, times(1)).setStatus(HttpStatus.SERVER_ERROR_NOT_IMPLEMENTED);
+ verify(response, times(1)).write("");
+ }
+
+ @Test
+ public void testPut() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ new TestRequestHandler().put(request, response);
+ verify(response, times(1)).setStatus(HttpStatus.SERVER_ERROR_NOT_IMPLEMENTED);
+ verify(response, times(1)).write("");
+ }
+
+ @Test
+ public void testDelete() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ new TestRequestHandler().delete(request, response);
+ verify(response, times(1)).setStatus(HttpStatus.SERVER_ERROR_NOT_IMPLEMENTED);
+ verify(response, times(1)).write("");
+ }
+
+ @Test
+ public void testHead() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ new TestRequestHandler().head(request, response);
+ verify(response, times(1)).setStatus(HttpStatus.SERVER_ERROR_NOT_IMPLEMENTED);
+ verify(response, times(1)).write("");
+ }
+
+ @Test
+ public void testGetCurrentUser() {
+
+ HttpRequest request = mock(HttpRequest.class);
+ assertNull(new TestRequestHandler().getCurrentUser(request));
+ }
+}