You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2010/03/09 03:28:23 UTC
svn commit: r920625 - in /shindig/trunk/java/gadgets/src:
main/java/org/apache/shindig/gadgets/servlet/
test/java/org/apache/shindig/gadgets/servlet/
Author: johnh
Date: Tue Mar 9 02:28:22 2010
New Revision: 920625
URL: http://svn.apache.org/viewvc?rev=920625&view=rev
Log:
Switches ProxyServlet, by way of ProxyHandler (and ProxyBase, to a lesser
extent) modifications, to use ProxyUriManager.
The implementation follows the now-typical pattern of a UriManager's
make/process methods.
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyBase.java
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyHandler.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyBaseTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletTest.java
Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java Tue Mar 9 02:28:22 2010
@@ -115,7 +115,19 @@ public class MakeRequestHandler extends
* @throws GadgetException
*/
protected HttpRequest buildHttpRequest(HttpServletRequest request) throws GadgetException {
- Uri url = validateUrl(request.getParameter(URL_PARAM));
+ String urlStr = request.getParameter(URL_PARAM);
+ if (urlStr == null) {
+ throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
+ URL_PARAM + " parameter is missing.", HttpResponse.SC_BAD_REQUEST);
+ }
+
+ Uri url = null;
+ try {
+ url = validateUrl(Uri.parse(urlStr));
+ } catch (IllegalArgumentException e) {
+ throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
+ "Invalid " + URL_PARAM + " parameter", HttpResponse.SC_BAD_REQUEST);
+ }
HttpRequest req = new HttpRequest(url)
.setMethod(getParameter(request, METHOD_PARAM, "GET"))
Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyBase.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyBase.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyBase.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyBase.java Tue Mar 9 02:28:22 2010
@@ -67,26 +67,21 @@ public abstract class ProxyBase {
* @return A URI representing a validated form of the url.
* @throws GadgetException If the url is not valid.
*/
- protected Uri validateUrl(String urlToValidate) throws GadgetException {
+ protected Uri validateUrl(Uri urlToValidate) throws GadgetException {
if (urlToValidate == null) {
- throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
- "url parameter is missing.", HttpResponse.SC_BAD_REQUEST);
+ throw new GadgetException(GadgetException.Code.MISSING_PARAMETER, "Missing url param",
+ HttpResponse.SC_BAD_REQUEST);
}
- try {
- UriBuilder url = UriBuilder.parse(urlToValidate);
- if (!"http".equals(url.getScheme()) && !"https".equals(url.getScheme())) {
- throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
- "Invalid request url scheme in url: " + Utf8UrlCoder.encode(urlToValidate) +
- "; only \"http\" and \"https\" supported.", HttpResponse.SC_BAD_REQUEST);
- }
- if (url.getPath() == null || url.getPath().length() == 0) {
- url.setPath("/");
- }
- return url.toUri();
- } catch (IllegalArgumentException e) {
+ UriBuilder url = new UriBuilder(urlToValidate);
+ if (!"http".equals(url.getScheme()) && !"https".equals(url.getScheme())) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
- "url parameter is not a valid url: " + urlToValidate, HttpResponse.SC_BAD_REQUEST);
+ "Invalid request url scheme in url: " + Utf8UrlCoder.encode(urlToValidate.toString()) +
+ "; only \"http\" and \"https\" supported.", HttpResponse.SC_BAD_REQUEST);
+ }
+ if (url.getPath() == null || url.getPath().length() == 0) {
+ url.setPath("/");
}
+ return url.toUri();
}
/**
@@ -127,7 +122,7 @@ public abstract class ProxyBase {
refreshInterval = Integer.valueOf(request.getParameter(REFRESH_PARAM));
} catch (NumberFormatException nfe) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
- "refresh parameter is not a number", HttpResponse.SC_BAD_REQUEST);
+ "refresh parameter is not a number");
}
} else {
refreshInterval = Math.max(60 * 60, (int)(results.getCacheTtl() / 1000L));
@@ -177,11 +172,17 @@ public abstract class ProxyBase {
protected void outputError(HttpServletResponse resp, GadgetException e)
throws IOException {
+ int responseCode;
Level level = Level.FINE;
- int responseCode = HttpResponse.SC_BAD_REQUEST;
- if (e.getCode() == GadgetException.Code.INTERNAL_SERVER_ERROR) {
- level = Level.WARNING;
- responseCode = e.getHttpStatusCode();
+
+ switch (e.getCode()) {
+ case INTERNAL_SERVER_ERROR:
+ responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
+ level = Level.WARNING;
+ break;
+ default:
+ responseCode = HttpServletResponse.SC_BAD_REQUEST;
+ break;
}
logger.log(level, "Request failed", e);
@@ -191,7 +192,7 @@ public abstract class ProxyBase {
abstract protected void doFetch(HttpServletRequest request, HttpServletResponse response)
throws GadgetException, IOException;
- protected boolean getIgnoreCache(HttpServletRequest request) {
+ protected static boolean getIgnoreCache(HttpServletRequest request) {
String ignoreCache = request.getParameter(IGNORE_CACHE_PARAM);
if (ignoreCache == null) {
return false;
Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyHandler.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyHandler.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyHandler.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/ProxyHandler.java Tue Mar 9 02:28:22 2010
@@ -29,7 +29,9 @@ import com.google.inject.Singleton;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.StringUtils;
+import org.apache.shindig.common.servlet.HttpUtil;
import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.common.uri.UriBuilder;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.LockedDomainService;
import org.apache.shindig.gadgets.http.HttpRequest;
@@ -37,6 +39,7 @@ import org.apache.shindig.gadgets.http.H
import org.apache.shindig.gadgets.http.RequestPipeline;
import org.apache.shindig.gadgets.rewrite.RequestRewriterRegistry;
import org.apache.shindig.gadgets.rewrite.RewritingException;
+import org.apache.shindig.gadgets.uri.ProxyUriManager;
import java.io.IOException;
import java.util.Map;
@@ -55,71 +58,55 @@ public class ProxyHandler extends ProxyB
private static final String[] INTEGER_RESIZE_PARAMS = new String[] {
PARAM_RESIZE_HEIGHT, PARAM_RESIZE_WIDTH, PARAM_RESIZE_QUALITY, PARAM_NO_EXPAND
};
+
+ // TODO: parameterize these.
+ static final Integer LONG_LIVED_REFRESH = (365 * 24 * 60 * 60); // 1 year
+ static final Integer DEFAULT_REFRESH = (60 * 60); // 1 hour
static final String FALLBACK_URL_PARAM = "fallback_url";
private final RequestPipeline requestPipeline;
private final LockedDomainService lockedDomainService;
private final RequestRewriterRegistry contentRewriterRegistry;
+ private final ProxyUriManager proxyUriManager;
@Inject
public ProxyHandler(RequestPipeline requestPipeline,
LockedDomainService lockedDomainService,
- RequestRewriterRegistry contentRewriterRegistry) {
+ RequestRewriterRegistry contentRewriterRegistry,
+ ProxyUriManager proxyUriManager) {
this.requestPipeline = requestPipeline;
this.lockedDomainService = lockedDomainService;
this.contentRewriterRegistry = contentRewriterRegistry;
+ this.proxyUriManager = proxyUriManager;
}
/**
* Generate a remote content request based on the parameters sent from the client.
*/
- private HttpRequest buildHttpRequest(HttpServletRequest request, String urlParam)
- throws GadgetException {
- String theUrl = request.getParameter(urlParam);
- if (theUrl == null) {
- return null;
- }
- Uri url = validateUrl(theUrl);
-
- HttpRequest req = new HttpRequest(url)
- .setContainer(getContainer(request));
+ private HttpRequest buildHttpRequest(HttpServletRequest request, Uri uri,
+ ProxyUriManager.ProxyUri uriCtx, Uri tgt) throws GadgetException {
+ validateUrl(tgt);
- copySanitizedIntegerParams(request, req);
-
- if (request.getParameter(GADGET_PARAM) != null) {
- req.setGadget(Uri.parse(request.getParameter(GADGET_PARAM)));
- }
+ HttpRequest req = uriCtx.makeHttpRequest(uriCtx.getResource());
+ copySanitizedIntegerParams(uri, req);
// Allow the rewriter to use an externally forced MIME type. This is needed
// allows proper rewriting of <script src="x"/> where x is returned with
// a content type like text/html which unfortunately happens all too often
- req.setRewriteMimeType(request.getParameter(REWRITE_MIME_TYPE_PARAM));
+ req.setRewriteMimeType(uri.getQueryParameter(REWRITE_MIME_TYPE_PARAM));
+ req.setSanitizationRequested("1".equals(uri.getQueryParameter(SANITIZE_CONTENT_PARAM)));
- req.setIgnoreCache(getIgnoreCache(request));
-
- req.setSanitizationRequested(
- "1".equals(request.getParameter(SANITIZE_CONTENT_PARAM)));
-
- // If the proxy request specifies a refresh param then we want to force the min TTL for
- // the retrieved entry in the cache regardless of the headers on the content when it
- // is fetched from the original source.
- if (request.getParameter(REFRESH_PARAM) != null) {
- try {
- req.setCacheTtl(Integer.parseInt(request.getParameter(REFRESH_PARAM)));
- } catch (NumberFormatException nfe) {
- // Ignore
- }
- }
this.setRequestHeaders(request, req);
+
return req;
}
- private void copySanitizedIntegerParams(HttpServletRequest request, HttpRequest req) {
+ private void copySanitizedIntegerParams(Uri uri, HttpRequest req) {
for (String resizeParamName : INTEGER_RESIZE_PARAMS) {
- if (request.getParameter(resizeParamName) != null) {
- req.setParam(resizeParamName,
- NumberUtils.createInteger(request.getParameter(resizeParamName)));
+ if (uri.getQueryParameter(resizeParamName) != null) {
+ req.setParam(resizeParamName,
+ NumberUtils.createInteger(uri.getQueryParameter(resizeParamName)));
}
}
}
@@ -132,6 +119,7 @@ public class ProxyHandler extends ProxyB
return;
}
+ // TODO: Consider removing due to redundant logic.
String host = request.getHeader("Host");
if (!lockedDomainService.isSafeForOpenProxy(host)) {
// Force embedded images and the like to their own domain to avoid XSS
@@ -142,8 +130,19 @@ public class ProxyHandler extends ProxyB
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, msg,
HttpResponse.SC_BAD_REQUEST);
}
+
+ Uri requestUri = new UriBuilder(request).toUri();
+ ProxyUriManager.ProxyUri proxyUri = proxyUriManager.process(requestUri);
+
+ try {
+ HttpUtil.setCachingHeaders(response,
+ proxyUri.translateStatusRefresh(LONG_LIVED_REFRESH, DEFAULT_REFRESH), false);
+ } catch (GadgetException gex) {
+ response.sendError(HttpServletResponse.SC_BAD_REQUEST, gex.getMessage());
+ return;
+ }
- HttpRequest rcr = buildHttpRequest(request, URL_PARAM);
+ HttpRequest rcr = buildHttpRequest(request, requestUri, proxyUri, proxyUri.getResource());
if (rcr == null) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"No url paramater in request", HttpResponse.SC_BAD_REQUEST);
@@ -152,10 +151,17 @@ public class ProxyHandler extends ProxyB
if (results.isError()) {
// Error: try the fallback. Particularly useful for proxied images.
- HttpRequest fallbackRcr = buildHttpRequest(request, FALLBACK_URL_PARAM);
- if (fallbackRcr != null) {
- results = requestPipeline.execute(fallbackRcr);
- }
+ String fallbackStr = requestUri.getQueryParameter(FALLBACK_URL_PARAM);
+ if (fallbackStr != null) {
+ try {
+ HttpRequest fallbackRcr =
+ buildHttpRequest(request, requestUri, proxyUri, Uri.parse(fallbackStr));
+ results = requestPipeline.execute(fallbackRcr);
+ } catch (IllegalArgumentException e) {
+ throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
+ FALLBACK_URL_PARAM + " param is invalid: " + e, HttpResponse.SC_BAD_REQUEST);
+ }
+ }
}
if (contentRewriterRegistry != null) {
Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyBaseTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyBaseTest.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyBaseTest.java (original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyBaseTest.java Tue Mar 9 02:28:22 2010
@@ -51,7 +51,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlNoPath() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/", url.getPath());
@@ -61,7 +61,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlHttps() throws Exception {
- Uri url = proxy.validateUrl("https://www.example.com");
+ Uri url = proxy.validateUrl(Uri.parse("https://www.example.com"));
assertEquals("https", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/", url.getPath());
@@ -71,7 +71,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlWithPath() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com/foo");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com/foo"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/foo", url.getPath());
@@ -81,7 +81,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlWithPort() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com:8080/foo");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com:8080/foo"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com:8080", url.getAuthority());
assertEquals("/foo", url.getPath());
@@ -91,7 +91,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlWithEncodedPath() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com/foo%20bar");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com/foo%20bar"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/foo%20bar", url.getPath());
@@ -101,7 +101,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlWithEncodedQuery() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com/foo?q=with%20space");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com/foo?q=with%20space"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/foo", url.getPath());
@@ -112,7 +112,7 @@ public class ProxyBaseTest extends Servl
@Test
public void testValidateUrlWithNoPathAndEncodedQuery() throws Exception {
- Uri url = proxy.validateUrl("http://www.example.com?q=with%20space");
+ Uri url = proxy.validateUrl(Uri.parse("http://www.example.com?q=with%20space"));
assertEquals("http", url.getScheme());
assertEquals("www.example.com", url.getAuthority());
assertEquals("/", url.getPath());
@@ -125,16 +125,6 @@ public class ProxyBaseTest extends Servl
proxy.validateUrl(null);
}
- @Test(expected=GadgetException.class)
- public void testValidateUrlBadInput() throws Exception {
- proxy.validateUrl("%$#%#$%#$%");
- }
-
- @Test(expected=GadgetException.class)
- public void testValidateUrlBadProtocol() throws Exception {
- proxy.validateUrl("gopher://foo");
- }
-
@Test
public void testSetResponseHeaders() throws Exception {
HttpResponse results = new HttpResponseBuilder().create();
Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java (original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyHandlerTest.java Tue Mar 9 02:28:22 2010
@@ -25,10 +25,13 @@ import static org.easymock.EasyMock.isA;
import com.google.common.collect.Maps;
import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.common.util.Utf8UrlCoder;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.http.HttpResponseBuilder;
+import org.apache.shindig.gadgets.uri.PassthruManager;
+import org.apache.shindig.gadgets.uri.ProxyUriManager;
import org.easymock.Capture;
import org.junit.Test;
@@ -42,8 +45,9 @@ public class ProxyHandlerTest extends Se
private final static String URL_ONE = "http://www.example.org/test.html";
private final static String DATA_ONE = "hello world";
+ private final ProxyUriManager passthruManager = new PassthruManager();
private final ProxyHandler proxyHandler
- = new ProxyHandler(pipeline, lockedDomainService, rewriterRegistry);
+ = new ProxyHandler(pipeline, lockedDomainService, rewriterRegistry, passthruManager);
private void expectGetAndReturnData(String url, byte[] data) throws Exception {
HttpRequest req = new HttpRequest(Uri.parse(url));
@@ -57,13 +61,31 @@ public class ProxyHandlerTest extends Se
HttpResponse resp = new HttpResponseBuilder().addAllHeaders(headers).create();
expect(pipeline.execute(req)).andReturn(resp);
}
+
+ private void setupProxyRequestBase(String host) {
+ expect(request.getServerName()).andReturn(host).anyTimes();
+ expect(request.getScheme()).andReturn("http").anyTimes();
+ expect(request.getServerPort()).andReturn(80).anyTimes();
+ expect(request.getRequestURI()).andReturn("/").anyTimes();
+ }
- private void setupProxyRequestMock(String host, String url) throws Exception {
+ private void setupProxyRequestMock(String host, String url, String... extraParams)
+ throws Exception {
+ setupProxyRequestBase(host);
expect(request.getHeader("Host")).andReturn(host);
- expect(request.getParameter("url")).andReturn(url).atLeastOnce();
+ String query = (url != null ? "url=" + Utf8UrlCoder.encode(url) + "&" : "")
+ + "container=default";
+ String[] params = extraParams;
+ if (params != null && params.length > 0) {
+ for (int i = 0; i < params.length; i += 2) {
+ query += "&" + params[i] + "=" + Utf8UrlCoder.encode(params[i+1]);
+ }
+ }
+ expect(request.getQueryString()).andReturn(query);
}
private void setupFailedProxyRequestMock(String host, String url) throws Exception {
+ setupProxyRequestBase(host);
expect(request.getHeader("Host")).andReturn(host);
}
@@ -166,9 +188,8 @@ public class ProxyHandlerTest extends Se
String fallback_url = "http://fallback.com/fallback.png";
expect(lockedDomainService.isSafeForOpenProxy(domain)).andReturn(true).atLeastOnce();
- setupProxyRequestMock(domain, url);
- expect(request.getParameter(ProxyBase.IGNORE_CACHE_PARAM)).andReturn("1").atLeastOnce();
- expect(request.getParameter(ProxyHandler.FALLBACK_URL_PARAM)).andReturn(fallback_url).atLeastOnce();
+ setupProxyRequestMock(domain, url, ProxyBase.IGNORE_CACHE_PARAM, "1",
+ ProxyHandler.FALLBACK_URL_PARAM, fallback_url);
HttpRequest req = new HttpRequest(Uri.parse(url)).setIgnoreCache(true);
HttpResponse resp = HttpResponse.error();
@@ -189,8 +210,7 @@ public class ProxyHandlerTest extends Se
String domain = "example.org";
expect(lockedDomainService.isSafeForOpenProxy(domain)).andReturn(true).atLeastOnce();
- setupProxyRequestMock(domain, url);
- expect(request.getParameter(ProxyBase.IGNORE_CACHE_PARAM)).andReturn("1").atLeastOnce();
+ setupProxyRequestMock(domain, url, ProxyBase.IGNORE_CACHE_PARAM, "1");
HttpRequest req = new HttpRequest(Uri.parse(url)).setIgnoreCache(true);
HttpResponse resp = new HttpResponse("Hello");
@@ -229,8 +249,7 @@ public class ProxyHandlerTest extends Se
String domain = "example.org";
expect(lockedDomainService.isSafeForOpenProxy(domain)).andReturn(true).atLeastOnce();
- setupProxyRequestMock(domain, url);
- expect(request.getParameter(ProxyBase.REFRESH_PARAM)).andReturn("120").atLeastOnce();
+ setupProxyRequestMock(domain, url, ProxyBase.REFRESH_PARAM, "120");
HttpRequest req = new HttpRequestCache(Uri.parse(url)).setCacheTtl(120).setIgnoreCache(false);
HttpResponse resp = new HttpResponse("Hello");
@@ -249,8 +268,7 @@ public class ProxyHandlerTest extends Se
String domain = "example.org";
expect(lockedDomainService.isSafeForOpenProxy(domain)).andReturn(true).atLeastOnce();
- setupProxyRequestMock(domain, url);
- expect(request.getParameter(ProxyBase.REFRESH_PARAM)).andReturn("foo").atLeastOnce();
+ setupProxyRequestMock(domain, url, ProxyBase.REFRESH_PARAM, "foo");
HttpRequest req = new HttpRequestCache(Uri.parse(url)).setCacheTtl(-1).setIgnoreCache(false);
HttpResponse resp = new HttpResponse("Hello");
@@ -293,9 +311,7 @@ public class ProxyHandlerTest extends Se
String domain = "example.org";
expect(lockedDomainService.isSafeForOpenProxy(domain)).andReturn(true).atLeastOnce();
- setupProxyRequestMock(domain, url);
- expect(request.getParameter(ProxyHandler.REWRITE_MIME_TYPE_PARAM))
- .andReturn(expectedMime).anyTimes();
+ setupProxyRequestMock(domain, url, ProxyHandler.REWRITE_MIME_TYPE_PARAM, expectedMime);
HttpRequest req = new HttpRequest(Uri.parse(url))
.setRewriteMimeType(expectedMime);
Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletTest.java?rev=920625&r1=920624&r2=920625&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletTest.java (original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/ProxyServletTest.java Tue Mar 9 02:28:22 2010
@@ -25,6 +25,8 @@ import org.apache.shindig.common.uri.Uri
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
+import org.apache.shindig.gadgets.uri.PassthruManager;
+import org.apache.shindig.gadgets.uri.ProxyUriManager;
import org.junit.Before;
import org.junit.Test;
@@ -40,13 +42,12 @@ public class ProxyServletTest extends Se
private static final Uri REQUEST_URL = Uri.parse("http://example.org/file");
private static final String BASIC_SYNTAX_URL
= "http://opensocial.org/proxy?foo=bar&url=" + REQUEST_URL;
- private static final String ALT_SYNTAX_URL
- = "http://opensocial.org/proxy/foo=bar/" + REQUEST_URL;
private static final String RESPONSE_BODY = "Hello, world!";
private static final String ERROR_MESSAGE = "Broken!";
+ private final ProxyUriManager passthruManager = new PassthruManager();
private final ProxyHandler proxyHandler
- = new ProxyHandler(pipeline, lockedDomainService, null);
+ = new ProxyHandler(pipeline, lockedDomainService, null, passthruManager);
private final ProxyServlet servlet = new ProxyServlet();
private final HttpRequest internalRequest = new HttpRequest(REQUEST_URL);
private final HttpResponse internalResponse = new HttpResponse(RESPONSE_BODY);
@@ -60,23 +61,24 @@ public class ProxyServletTest extends Se
expect(lockedDomainService.isSafeForOpenProxy(REQUEST_DOMAIN))
.andReturn(true).anyTimes();
}
-
- private void setupBasic() {
- expect(request.getRequestURI()).andReturn(BASIC_SYNTAX_URL);
- }
-
- private void setupAltSyntax() {
- expect(request.getRequestURI()).andReturn(ALT_SYNTAX_URL);
+
+ private void setupRequest(String str) {
+ Uri uri = Uri.parse(str);
+ expect(request.getScheme()).andReturn(uri.getScheme());
+ expect(request.getServerName()).andReturn(uri.getAuthority());
+ expect(request.getServerPort()).andReturn(80);
+ expect(request.getRequestURI()).andReturn(uri.getPath());
+ expect(request.getQueryString()).andReturn(uri.getQuery());
}
private void assertResponseOk(int expectedStatus, String expectedBody) {
- assertEquals(expectedStatus, recorder.getHttpStatusCode());
- assertEquals(expectedBody, recorder.getResponseAsString());
+ assertEquals(expectedStatus, recorder.getHttpStatusCode());
+ assertEquals(expectedBody, recorder.getResponseAsString());
}
@Test
public void testDoGetNormal() throws Exception {
- setupBasic();
+ setupRequest(BASIC_SYNTAX_URL);
expect(pipeline.execute(internalRequest)).andReturn(internalResponse);
replay();
@@ -87,7 +89,7 @@ public class ProxyServletTest extends Se
@Test
public void testDoGetHttpError() throws Exception {
- setupBasic();
+ setupRequest(BASIC_SYNTAX_URL);
expect(pipeline.execute(internalRequest)).andReturn(HttpResponse.notFound());
replay();
@@ -98,7 +100,7 @@ public class ProxyServletTest extends Se
@Test
public void testDoGetException() throws Exception {
- setupBasic();
+ setupRequest(BASIC_SYNTAX_URL);
expect(pipeline.execute(internalRequest)).andThrow(
new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE));
replay();
@@ -108,16 +110,4 @@ public class ProxyServletTest extends Se
assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode());
assertContains(ERROR_MESSAGE, recorder.getResponseAsString());
}
-
- @Test
- public void testDoGetAlternateSyntax() throws Exception {
- setupAltSyntax();
- expect(request.getRequestURI()).andReturn(ALT_SYNTAX_URL);
- expect(pipeline.execute(internalRequest)).andReturn(internalResponse);
- replay();
-
- servlet.doGet(request, recorder);
-
- assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY);
- }
}