Reviewers: shindig.remailer_gmail.com, johnfargo,

Description:

Handle the case of bad spec that is smaller then the optional BOM

Please review this at http://codereview.appspot.com/634043/show

Affected files:
java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactory.java java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactoryTest.java


### Eclipse Workspace Patch 1.0
#P shindig-project
Index: java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactory.java
===================================================================
--- java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactory.java (revision 925050) +++ java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactory.java (working copy)
@@ -90,7 +90,8 @@
protected GadgetSpec parse(String content, Query query) throws XmlException, GadgetException {
     // Allow BOM entity as first item on stream and ignore it:
     final String BOM_ENTITY = "";
- if (content.substring(0, BOM_ENTITY.length()).equalsIgnoreCase(BOM_ENTITY)) {
+    if (content.length() >= BOM_ENTITY.length() &&
+ content.substring(0, BOM_ENTITY.length()).equalsIgnoreCase(BOM_ENTITY)) {
       content = content.substring(BOM_ENTITY.length());
     }
     Element element = XmlUtil.parse(content);
Index: java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactoryTest.java
===================================================================
--- java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactoryTest.java (revision 925050) +++ java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultGadgetSpecFactoryTest.java (working copy)
@@ -145,6 +145,25 @@
assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
   }

+  @Test(expected = GadgetException.class)
+  public void specFetchedEmptyContent() throws Exception {
+    HttpRequest request = createIgnoreCacheRequest();
+    HttpResponse response = new HttpResponse("");
+    expect(pipeline.execute(request)).andReturn(response);
+    replay(pipeline);
+
+ GadgetSpec spec = specFactory.getGadgetSpec(createContext(SPEC_URL, true));
+  }
+
+  @Test(expected = GadgetException.class)
+  public void malformedGadgetSpecIsCachedAndThrows2() throws Exception {
+    HttpRequest request = createIgnoreCacheRequest();
+ expect(pipeline.execute(request)).andReturn(new HttpResponse("")).once();
+    replay(pipeline);
+
+    specFactory.getGadgetSpec(createContext(SPEC_URL, true));
+  }
+
   @Test
   public void specFetchedWithBomChar() throws Exception {
     HttpRequest request = createIgnoreCacheRequest();


Reply via email to