Author: beaton
Date: Fri Jun 6 17:27:24 2008
New Revision: 664209
URL: http://svn.apache.org/viewvc?rev=664209&view=rev
Log:
Fix SHINDIG-309. This adds more test cases to the BasicHttpFetcher, though
still not enough.
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpFetcherTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/EchoServer.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/FakeHttpServer.java
Modified:
incubator/shindig/trunk/java/gadgets/pom.xml
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java
Modified: incubator/shindig/trunk/java/gadgets/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/pom.xml?rev=664209&r1=664208&r2=664209&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/pom.xml (original)
+++ incubator/shindig/trunk/java/gadgets/pom.xml Fri Jun 6 17:27:24 2008
@@ -148,6 +148,11 @@
<artifactId>commons-lang</artifactId>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java?rev=664209&r1=664208&r2=664209&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
Fri Jun 6 17:27:24 2008
@@ -22,6 +22,7 @@
import org.apache.commons.io.IOUtils;
+import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -118,16 +119,33 @@
responseCode = HttpResponse.SC_OK;
}
+ // Find the response stream - the error stream may be valid in cases
+ // where the input stream is not.
+ InputStream baseIs = null;
+ try {
+ baseIs = fetcher.getInputStream();
+ } catch (IOException e) {
+ // normal for 401, 403 and 404 responses, for example...
+ }
+ if (baseIs == null && fetcher instanceof HttpURLConnection) {
+ // Try for an error input stream
+ baseIs = ((HttpURLConnection)fetcher).getErrorStream();
+ }
+ if (baseIs == null) {
+ // Fall back to zero length response.
+ baseIs = new ByteArrayInputStream(new byte[0]);
+ }
+
String encoding = fetcher.getContentEncoding();
- InputStream is = null;
// Create the appropriate stream wrapper based on the encoding type.
+ InputStream is = null;
if (encoding == null) {
- is = fetcher.getInputStream();
+ is = baseIs;
} else if (encoding.equalsIgnoreCase("gzip")) {
- is = new GZIPInputStream(fetcher.getInputStream());
+ is = new GZIPInputStream(baseIs);
} else if (encoding.equalsIgnoreCase("deflate")) {
Inflater inflater = new Inflater(true);
- is = new InflaterInputStream(fetcher.getInputStream(), inflater);
+ is = new InflaterInputStream(baseIs, inflater);
}
byte[] body = IOUtils.toByteArray(is);
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpFetcherTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpFetcherTest.java?rev=664209&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpFetcherTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpFetcherTest.java
Fri Jun 6 17:27:24 2008
@@ -0,0 +1,92 @@
+package org.apache.shindig.gadgets.http;
+
+import static org.junit.Assert.assertEquals;
+
+import java.net.URI;
+import java.net.URLEncoder;
+
+import org.apache.shindig.gadgets.http.EchoServer;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Holds test cases that all HttpFetcher implementations should pass. This
+ * starts up an HTTP server and runs tests against it.
+ *
+ * TODO simulate fake POST requests, headers, options, etc.
+ */
+public class AbstractHttpFetcherTest {
+ private static final int ECHO_PORT = 9003;
+ private static final String BASE_URL = "http://localhost:9003/";
+ private static EchoServer server;
+ protected HttpFetcher fetcher = null;
+
+ private String encode(String content) throws Exception {
+ return URLEncoder.encode(content, "UTF-8");
+ }
+
+ @BeforeClass
+ public static void setUpOnce() throws Exception {
+ server = new EchoServer();
+ server.start(ECHO_PORT);
+ }
+
+ @AfterClass
+ public static void tearDownOnce() throws Exception {
+ if (server != null) {
+ server.stop();
+ }
+ }
+
+ @Test public void testHttpFetch() throws Exception {
+ String content = "Hello, world!";
+ HttpRequest request = new HttpRequest(new URI(
+ BASE_URL + "?body=" + encode(content)));
+ HttpResponse response = fetcher.fetch(request);
+ assertEquals(200, response.getHttpStatusCode());
+ assertEquals(content, response.getResponseAsString());
+ }
+
+ @Test public void testHttp404() throws Exception {
+ String content = "Hello, world!";
+ HttpRequest request = new HttpRequest(new URI(
+ BASE_URL + "?body=" + encode(content) + "&status=404"));
+ HttpResponse response = fetcher.fetch(request);
+ assertEquals(404, response.getHttpStatusCode());
+ assertEquals(content, response.getResponseAsString());
+ }
+
+ @Test public void testHttp403() throws Exception {
+ String content = "Hello, world!";
+ HttpRequest request = new HttpRequest(new URI(
+ BASE_URL + "?body=" + encode(content) + "&status=403" +
+ "&header=" + encode("WWW-Authenticate=some auth data")));
+ HttpResponse response = fetcher.fetch(request);
+ assertEquals(403, response.getHttpStatusCode());
+ assertEquals(content, response.getResponseAsString());
+ assertEquals("some auth data", response.getHeader("WWW-Authenticate"));
+ }
+
+ @Test public void testHttp403NoBody() throws Exception {
+ String content = "";
+ HttpRequest request = new HttpRequest(new URI(
+ BASE_URL + "?body=" + encode(content) + "&status=403" +
+ "&header=" + encode("WWW-Authenticate=some auth data")));
+ HttpResponse response = fetcher.fetch(request);
+ assertEquals(403, response.getHttpStatusCode());
+ assertEquals(content, response.getResponseAsString());
+ assertEquals("some auth data", response.getHeader("WWW-Authenticate"));
+ }
+
+ @Test public void testHttp401NoBody() throws Exception {
+ String content = "";
+ HttpRequest request = new HttpRequest(new URI(
+ BASE_URL + "?body=" + encode(content) + "&status=401" +
+ "&header=" + encode("WWW-Authenticate=some auth data")));
+ HttpResponse response = fetcher.fetch(request);
+ assertEquals(401, response.getHttpStatusCode());
+ assertEquals(content, response.getResponseAsString());
+ assertEquals("some auth data", response.getHeader("WWW-Authenticate"));
+ }
+}
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java?rev=664209&r1=664208&r2=664209&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java
Fri Jun 6 17:27:24 2008
@@ -17,37 +17,13 @@
*/
package org.apache.shindig.gadgets.http;
-import junit.framework.TestCase;
+import org.junit.Before;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.net.URI;
+public class BasicHttpFetcherTest extends AbstractHttpFetcherTest {
-public class BasicHttpFetcherTest extends TestCase {
- private HttpCache cache = new BasicHttpCache(10);
- private HttpFetcher fetcher
- = new BasicHttpFetcher(cache, Integer.MAX_VALUE);
-
- public void testFetch() throws Exception {
- String content = "Hello, world!";
- File temp = File.createTempFile(this.getName(), ".txt");
- temp.deleteOnExit();
- BufferedWriter out = new BufferedWriter(new FileWriter(temp));
- out.write(content);
- out.close();
- HttpRequest request = new HttpRequest(temp.toURI());
- HttpResponse response = fetcher.fetch(request);
- assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode());
- assertEquals(content, response.getResponseAsString());
- }
-
- public void testNotExists() throws Exception {
- HttpRequest request
- = new HttpRequest(new URI("file:///does/not/exist"));
- HttpResponse response = fetcher.fetch(request);
- assertEquals(HttpResponse.SC_NOT_FOUND, response.getHttpStatusCode());
+ @Before
+ public void setUp() {
+ HttpCache cache = new BasicHttpCache(10);
+ fetcher = new BasicHttpFetcher(cache, Integer.MAX_VALUE);
}
-
- // TODO simulate fake POST requests, headers, options, etc.
}
\ No newline at end of file
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/EchoServer.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/EchoServer.java?rev=664209&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/EchoServer.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/EchoServer.java
Fri Jun 6 17:27:24 2008
@@ -0,0 +1,73 @@
+/*
+ * 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.shindig.gadgets.http;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.mortbay.jetty.servlet.ServletHolder;
+
+/**
+ * A server that echoes back whatever you send to it.
+ */
+public class EchoServer extends FakeHttpServer {
+
+ public static final String STATUS_PARAM = "status";
+ public static final String BODY_PARAM = "body";
+ public static final String HEADER_PARAM = "header";
+
+ @Override
+ protected void addServlets() throws Exception {
+ ServletHolder servletHolder = new ServletHolder(new EchoServlet());
+ context.addServlet(servletHolder, "/*");
+ }
+
+ private static class EchoServlet extends HttpServlet {
+
+ @Override
+ protected void service(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ int code = HttpServletResponse.SC_OK;
+ if (req.getParameter(STATUS_PARAM) != null) {
+ code = Integer.parseInt(req.getParameter(STATUS_PARAM));
+ }
+ resp.setStatus(code);
+
+ String[] headers = req.getParameterValues(HEADER_PARAM);
+ if (headers != null) {
+ for (String header : headers) {
+ String[] nameAndValue = header.split("=", 2);
+ resp.setHeader(nameAndValue[0], nameAndValue[1]);
+ }
+ }
+
+ String body = req.getParameter(BODY_PARAM);
+ if (body == null) {
+ body = "";
+ }
+ resp.getWriter().print(body);
+ }
+
+ };
+
+}
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/FakeHttpServer.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/FakeHttpServer.java?rev=664209&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/FakeHttpServer.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/FakeHttpServer.java
Fri Jun 6 17:27:24 2008
@@ -0,0 +1,44 @@
+/*
+ * 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.shindig.gadgets.http;
+
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+
+/**
+ * A simple HTTP server to test against.
+ */
+public abstract class FakeHttpServer {
+ protected Server server = null;
+ protected Context context = null;
+
+ public void start(int port) throws Exception {
+ server = new Server(port);
+ context = new Context(server, "/", Context.SESSIONS);
+ addServlets();
+ server.start();
+ }
+
+ /** Override to add your servlets */
+ protected abstract void addServlets() throws Exception;
+
+ public void stop() throws Exception {
+ server.stop();
+ }
+}