@Test public void unsupportedOptionalFeatureDoesNotThrow() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Optional feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); rewrite(gadget, ""); // rewrite will throw if the optional unsupported feature doesn't work. }
@Test public void doesRewriteWhenSanitizeEquals0() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "0"); assertFalse("Didn't rewrite when sanitize was '0'.", BODY_CONTENT.equals(rewrite(gadget, BODY_CONTENT))); }
@Test public void unsupportedOptionalFeatureDoesNotThrow() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Optional feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); rewrite(gadget, ""); // rewrite will throw if the optional unsupported feature doesn't work. }
@Test public void baseElementNotInsertedWhenConfigDoesNotAllowIt() throws Exception { Gadget gadget = makeDefaultGadget(); config.data.put(INSERT_BASE_ELEMENT_KEY, false); String rewritten = rewrite(gadget, BODY_CONTENT); assertFalse("Base element injected incorrectly.", rewritten.contains("<base")); }
@Test public void unsupportedOptionalFeatureDoesNotThrow() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Optional feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); rewrite(gadget, ""); // rewrite will throw if the optional unsupported feature doesn't work. }
@Test public void doesRewriteWhenSanitizeEquals0() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "0"); assertFalse("Didn't rewrite when sanitize was '0'.", BODY_CONTENT.equals(rewrite(gadget, BODY_CONTENT))); }
@Test public void baseElementNotInsertedWhenConfigDoesNotAllowIt() throws Exception { Gadget gadget = makeDefaultGadget(); config.data.put(INSERT_BASE_ELEMENT_KEY, false); String rewritten = rewrite(gadget, BODY_CONTENT); assertFalse("Base element injected incorrectly.", rewritten.contains("<base")); }
@Test public void doesRewriteWhenSanitizeEquals0() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "0"); assertFalse("Didn't rewrite when sanitize was '0'.", BODY_CONTENT.equals(rewrite(gadget, BODY_CONTENT))); }
@Test public void doesNotRewriteWhenSanitizeEquals1() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "1"); assertEquals(BODY_CONTENT, rewrite(gadget, BODY_CONTENT)); }
@Test public void bidiSettings() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Locale language_direction='rtl'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); String rewritten = rewrite(gadget, ""); assertTrue("Bi-directional locale settings not preserved.", rewritten.contains("<body dir=\"rtl\">")); }
@Test public void doesNotRewriteWhenSanitizeEquals1() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "1"); assertEquals(BODY_CONTENT, rewrite(gadget, BODY_CONTENT)); }
@Test public void failedPreloadHandledGracefully() throws Exception { PreloadedData preloadedData = new PreloadedData() { public Collection<Object> toJson() throws PreloadException { throw new PreloadException("test"); } }; Gadget gadget = makeDefaultGadget().setPreloads(ImmutableList.of(preloadedData)); String rewritten = rewrite(gadget, ""); JSONArray json = getPreloadedJson(rewritten); assertEquals(0, json.length()); }
@Test public void failedPreloadHandledGracefully() throws Exception { PreloadedData preloadedData = new PreloadedData() { public Collection<Object> toJson() throws PreloadException { throw new PreloadException("test"); } }; Gadget gadget = makeDefaultGadget().setPreloads(ImmutableList.of(preloadedData)); String rewritten = rewrite(gadget, ""); JSONArray json = getPreloadedJson(rewritten); assertEquals(0, json.length()); }
@Test public void failedPreloadHandledGracefully() throws Exception { PreloadedData preloadedData = new PreloadedData() { public Collection<Object> toJson() throws PreloadException { throw new PreloadException("test"); } }; Gadget gadget = makeDefaultGadget().setPreloads(ImmutableList.of(preloadedData)); String rewritten = rewrite(gadget, ""); JSONArray json = getPreloadedJson(rewritten); assertEquals(0, json.length()); }
@Test(expected = RewritingException.class) public void exceptionWhenFeatureNotAllowed() throws Exception { Gadget gadget = makeDefaultGadget(); reset(gadgetAdminStore); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(false); replay(gadgetAdminStore); rewrite(gadget, BODY_CONTENT); }
@Test(expected = RewritingException.class) public void exceptionWhenFeatureNotAllowed() throws Exception { Gadget gadget = makeDefaultGadget(); reset(gadgetAdminStore); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(false); replay(gadgetAdminStore); rewrite(gadget, BODY_CONTENT); }
@Test public void baseElementInsertedWhenContentIsInline() throws Exception { Gadget gadget = makeDefaultGadget(); config.data.put(INSERT_BASE_ELEMENT_KEY, true); String rewritten = rewrite(gadget, BODY_CONTENT); String base = getBaseElement(rewritten); assertEquals(SPEC_URL.toString(), base); }
@Test public void baseElementInsertedWhenContentIsProxied() throws Exception { Gadget gadget = makeDefaultGadget(); String viewUrl = "http://example.org/view.html"; String xml = "<Content href='" + viewUrl + "'/>"; View fakeView = new View("foo", Arrays.asList(XmlUtil.parse(xml)), SPEC_URL); gadget.setCurrentView(fakeView); config.data.put(INSERT_BASE_ELEMENT_KEY, true); String rewritten = rewrite(gadget, BODY_CONTENT); String base = getBaseElement(rewritten); assertEquals(viewUrl, base); }
@Test public void baseElementInsertedWhenContentIsProxied() throws Exception { Gadget gadget = makeDefaultGadget(); String viewUrl = "http://example.org/view.html"; String xml = "<Content href='" + viewUrl + "'/>"; View fakeView = new View("foo", Arrays.asList(XmlUtil.parse(xml)), SPEC_URL); gadget.setCurrentView(fakeView); config.data.put(INSERT_BASE_ELEMENT_KEY, true); String rewritten = rewrite(gadget, BODY_CONTENT); String base = getBaseElement(rewritten); assertEquals(viewUrl, base); }
@Test public void xhrWrapperConfigurationNotInjectedIfUnnecessary() throws Exception { String gadgetXml = "<Module><ModulePrefs title='' />" + "<Content type='html' href='http://foo.com/bar/baz.html' />" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.setCurrentView(gadget.getSpec().getView("default")); String rewritten = rewrite(gadget, BODY_CONTENT); boolean containsConfig = rewritten.contains("\"shindig.xhrwrapper\""); assertFalse("shindig.xhrwrapper configuration present in rewritten HTML.", containsConfig); }