Author: ssievers
Date: Tue Mar 20 11:44:18 2012
New Revision: 1302848
URL: http://svn.apache.org/viewvc?rev=1302848&view=rev
Log:
SHINDIG-1728 | gadgets.io.makeRequest supports PUT but the PUT body is not
passed along
Modified:
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/MakeRequestHandler.java
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.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=1302848&r1=1302847&r2=1302848&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 20 11:44:18 2012
@@ -240,7 +240,7 @@ public class MakeRequestHandler implemen
HttpRequest req = new HttpRequest(url).setMethod(getParameter(request,
METHOD_PARAM, "GET"))
.setContainer(container).setGadget(gadgetUri);
- if ("POST".equals(req.getMethod())) {
+ if ("POST".equals(req.getMethod()) || "PUT".equals(req.getMethod())) {
setPostData(container, request, req);
}
@@ -264,7 +264,8 @@ public class MakeRequestHandler implemen
if ("POST".equals(req.getMethod()) && req.getHeader("Content-Type") ==
null) {
req.addHeader("Content-Type", "application/x-www-form-urlencoded");
} else if ("1".equals(getParameter(request, MULTI_PART_FORM_POST, null))) {
- // We need the entire header from the original request because it comes
with a boundry value we need to reuse.
+ // We need the entire header from the original request because it comes
with a boundary value
+ // we need to reuse.
req.addHeader("Content-Type", request.getHeader("Content-Type"));
}
@@ -307,7 +308,8 @@ public class MakeRequestHandler implemen
* Set http request post data according to servlet request. It uses header
encoding if available,
* and defaulted to utf8 Override the function if different behavior is
needed.
*/
- protected void setPostData(String container, HttpServletRequest request,
HttpRequest req) throws GadgetException {
+ protected void setPostData(String container, HttpServletRequest request,
HttpRequest req)
+ throws GadgetException {
if (maxPostSizes.get(container) < request.getContentLength()) {
throw new GadgetException(GadgetException.Code.POST_TOO_LARGE, "Posted
data too large.",
HttpResponse.SC_REQUEST_ENTITY_TOO_LARGE);
@@ -333,7 +335,8 @@ public class MakeRequestHandler implemen
throw new GadgetException(Code.HTML_PARSE_ERROR, e,
HttpResponse.SC_BAD_REQUEST);
} catch (IOException e) {
// Something went wrong reading the request data.
- // TODO: perhaps also support a max post size and enforce it by throwing
and catching exceptions here.
+ // TODO: perhaps also support a max post size and enforce it by throwing
and catching
+ // exceptions here.
throw new GadgetException(Code.INTERNAL_SERVER_ERROR, e,
HttpResponse.SC_BAD_REQUEST);
}
}
Modified:
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java?rev=1302848&r1=1302847&r2=1302848&view=diff
==============================================================================
---
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java
(original)
+++
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/MakeRequestHandlerTest.java
Tue Mar 20 11:44:18 2012
@@ -94,13 +94,58 @@ public class MakeRequestHandlerTest exte
private void expectPostAndReturnBody(AuthType authType, String postData,
String response)
throws Exception {
HttpRequest req = new HttpRequest(REQUEST_URL).setMethod("POST")
- .setPostBody(REQUEST_BODY.getBytes("UTF-8"))
+ .setPostBody(postData.getBytes("UTF-8"))
.setAuthType(authType)
.addHeader("Content-Type", "application/x-www-form-urlencoded");
expect(pipeline.execute(req)).andReturn(new HttpResponse(response));
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("POST");
expect(request.getParameter(MakeRequestHandler.POST_DATA_PARAM))
- .andReturn(REQUEST_BODY);
+ .andReturn(postData);
+ }
+
+ private void expectPutAndReturnBody(String putData, String response) throws
Exception {
+ expectPutAndReturnBody(AuthType.NONE, putData, response);
+ }
+
+ private void expectPutAndReturnBody(AuthType authType, String putData,
String response)
+ throws Exception {
+ HttpRequest req = new HttpRequest(REQUEST_URL).setMethod("PUT")
+ .setPostBody(putData.getBytes("UTF-8"))
+ .setAuthType(authType);
+ expect(pipeline.execute(req)).andReturn(new HttpResponse(response));
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("PUT");
+ expect(request.getParameter(MakeRequestHandler.POST_DATA_PARAM))
+ .andReturn(putData);
+ }
+
+ private void expectDeleteAndReturnBody(String response) throws Exception {
+ expectDeleteAndReturnBody(AuthType.NONE, response);
+ }
+
+ private void expectDeleteAndReturnBody(AuthType authType, String response)
throws Exception {
+ HttpRequest req = new
HttpRequest(REQUEST_URL).setMethod("DELETE").setAuthType(authType);
+ expect(pipeline.execute(req)).andReturn(new HttpResponse(response));
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("DELETE");
+ }
+
+ private void expectHead() throws Exception {
+ expectHead(AuthType.NONE);
+ }
+
+ private void expectHead(AuthType authType) throws Exception {
+ HttpRequest req = new
HttpRequest(REQUEST_URL).setMethod("HEAD").setAuthType(authType);
+ expect(pipeline.execute(req)).andReturn(new HttpResponse(""));
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("HEAD");
+ }
+
+ private void expectPatchAndReturnBody(String response) throws Exception {
+ expectPatchAndReturnBody(AuthType.NONE, response);
+ }
+
+ private void expectPatchAndReturnBody(AuthType authType, String response)
throws Exception {
+ HttpRequest req = new
HttpRequest(REQUEST_URL).setMethod("PATCH").setAuthType(authType);
+ expect(pipeline.execute(req)).andReturn(new HttpResponse(response));
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("PATCH");
}
private JSONObject extractJsonFromResponse() throws JSONException {
@@ -253,6 +298,59 @@ public class MakeRequestHandlerTest exte
}
@Test
+ public void testPutRequest() throws Exception {
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("PUT");
+ expectPutAndReturnBody(REQUEST_BODY, RESPONSE_BODY);
+ replay();
+
+ handler.fetch(request, recorder);
+ JSONObject results = extractJsonFromResponse();
+
+ assertEquals(HttpResponse.SC_OK, results.getInt("rc"));
+ assertEquals(RESPONSE_BODY, results.get("body"));
+ assertTrue(rewriter.responseWasRewritten());
+ }
+
+ @Test
+ public void testDeleteRequest() throws Exception {
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("DELETE");
+ expectDeleteAndReturnBody("");
+ replay();
+
+ handler.fetch(request, recorder);
+ JSONObject results = extractJsonFromResponse();
+
+ assertEquals(HttpResponse.SC_OK, results.getInt("rc"));
+ assertTrue(rewriter.responseWasRewritten());
+ }
+
+ @Test
+ public void testHeadRequest() throws Exception {
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("HEAD");
+ expectHead();
+ replay();
+
+ handler.fetch(request, recorder);
+ JSONObject results = extractJsonFromResponse();
+
+ assertEquals(HttpResponse.SC_OK, results.getInt("rc"));
+ assertTrue(rewriter.responseWasRewritten());
+ }
+
+ @Test
+ public void testPatchRequest() throws Exception {
+
expect(request.getParameter(MakeRequestHandler.METHOD_PARAM)).andReturn("PATCH");
+ expectPatchAndReturnBody("");
+ replay();
+
+ handler.fetch(request, recorder);
+ JSONObject results = extractJsonFromResponse();
+
+ assertEquals(HttpResponse.SC_OK, results.getInt("rc"));
+ assertTrue(rewriter.responseWasRewritten());
+ }
+
+ @Test
public void testFetchAtom1Feed() throws Exception {
String txt = "<?xml version='1.0' encoding='utf-8'?>" +
"<feed xmlns=\"http://www.w3.org/2005/Atom\">" +