@Test public void xhrWrapperConfigurationInjected() throws Exception { checkXhrWrapperConfigurationInjection( "No shindig.xhrwrapper configuration present in rewritten HTML.", null, null, null); checkXhrWrapperConfigurationInjection( "No shindig.xhrwrapper.authorization=signed configuration present in rewritten HTML.", "signed", null, null); checkXhrWrapperConfigurationInjection( "No shindig.xhrwrapper.oauthService configuration present in rewritten HTML.", "oauth", "serviceName", null); checkXhrWrapperConfigurationInjection( "No shindig.xhrwrapper.oauthTokenName configuration present in rewritten HTML.", "oauth", "serviceName", "tokenName"); }
@Test public void inlinedFeaturesWhenNothingForced() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); expectFeatureCalls(gadget, ImmutableList.of(inline("foo_content();", "foo_content_debug();")), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertTrue("Requested scripts not inlined.", rewritten.contains("foo_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 doesNotRewriteWhenSanitizeEquals1() throws Exception { Gadget gadget = makeDefaultGadget(); context.params.put("sanitize", "1"); assertEquals(BODY_CONTENT, rewrite(gadget, BODY_CONTENT)); }
@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))); }
Gadget gadget = makeGadgetWithSpec(gadgetXml).setContext(context); reset(gadgetAdminStore); Feature denied = mock(Feature.class); expect(denied.getName()).andReturn("hello"); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(true); expect(gadgetAdminStore.isAllowedFeature(eq(denied), isA(Gadget.class))).andReturn(false); replay(); FeatureResource fooResource = inline("foo-content", "foo-debug"); expectFeatureCalls(gadget, ImmutableList.of(fooResource), libs, ImmutableList.of(fooResource, inline("bar-c", "bar-d"), inline("baz-c", "baz-d"))); String rewritten = rewrite(gadget, ""); JSONObject json = getConfigJson(rewritten); Set<String> actual = getInjectedScript(rewritten); Set<String> expected = ImmutableSortedSet.of("core.util", "foo", "bar", "baz"); assertFalse(actual.contains("hello")); assertEquals(expected, actual); assertFalse(json.getJSONObject("core.util").has("hello"));
Gadget gadget = makeDefaultGadget() .setContext(context); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.of("foo"), ImmutableList.of(inline("blah", "n/a"))); String rewritten = rewrite(gadget, doc); assertTrue("Output is not valid HTML.", matcher.matches()); assertTrue("DOCTYPE not preserved", matcher.group(BEFORE_HEAD_GROUP).contains(docType)); assertTrue("Missing opening html tag", matcher.group(BEFORE_HEAD_GROUP).contains("<html")); assertTrue("IsGadget beacon not included.", matcher.group(HEAD_GROUP).contains("<script>" + IS_GADGET_BEACON + "</script>")); assertTrue("Forced javascript not included.", matcher.group(HEAD_GROUP).contains("<script src=\"/js/foo?jsload=0\">")); assertFalse("Default styling was injected when a doctype was specified.", matcher.group(HEAD_GROUP).contains(DEFAULT_CSS)); assertTrue("Custom body attributes missing.", matcher.group(BODY_ATTRIBUTES_GROUP).contains(bodyAttr)); assertTrue("Original document not preserved.", matcher.group(BODY_GROUP).contains(body)); assertTrue("gadgets.util.runOnLoadHandlers not invoked.", matcher.group(BODY_GROUP).contains("gadgets.util.runOnLoadHandlers();"));
@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()); }
Gadget gadget = makeDefaultOpenSocial2Gadget(false) .setContext(context); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.of("foo"), ImmutableList.of(inline("blah", "n/a"))); String rewritten = rewrite(gadget, doc); assertTrue("Output is not valid HTML.", matcher.matches()); assertTrue("Doctype should have been rewritten to HTML5", matcher.group(BEFORE_HEAD_GROUP).contains("<!DOCTYPE html>"));
@Test public void featuresInjectedBeforeExistingScript() throws Exception { Gadget gadget = makeDefaultGadget(); String rewritten = rewrite(gadget, "<html><head><script src=\"foo.js\"></script></head><body>hello</body></html>"); Matcher matcher = DOCUMENT_SPLIT_PATTERN.matcher(rewritten); assertTrue("Output is not valid HTML.", matcher.matches()); String headContent = matcher.group(HEAD_GROUP); // Locate user script. int userPosition = headContent.indexOf("<script src=\"foo.js\"></script>"); // Anything else here, we added. int ourPosition = headContent.indexOf("<script>"); // TODO: restore when moved to a non-tag-shifting HTML parser (userPosition == -1 in body) // assertTrue("Injected script must come before user script.", ourPosition < userPosition); }
'}'; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.setCurrentView(gadget.getSpec().getView("default")); String rewritten = rewrite(gadget, BODY_CONTENT); assertXhrConfigContains(message, expected, rewritten);
@Test public void featuresNotInjectedWhenRemoved() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.removeFeature("foo"); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertFalse("Removed script still inlined.", rewritten.contains("foo_content();")); }
Gadget gadget = makeDefaultGadget() .setContext(context); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.of("foo"), ImmutableList.of(inline("blah", "n/a"))); String rewritten = rewrite(gadget, doc);
@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 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 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 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 featuresNotInjectedWhenRemoved() throws Exception { String gadgetXml = "<Module><ModulePrefs title=''>" + " <Require feature='foo'/>" + "</ModulePrefs>" + "<Content type='html'/>" + "</Module>"; Gadget gadget = makeGadgetWithSpec(gadgetXml); gadget.removeFeature("foo"); expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); String rewritten = rewrite(gadget, ""); assertFalse("Removed script still inlined.", rewritten.contains("foo_content();")); }
private Gadget makeGadgetWithSpec(String gadgetXml) throws GadgetException { GadgetSpec spec = new GadgetSpec(SPEC_URL, gadgetXml); Gadget gadget = new Gadget() .setContext(context) .setPreloads(ImmutableList.<PreloadedData>of()) .setSpec(spec) .setCurrentView(spec.getView(GadgetSpec.DEFAULT_VIEW)) .setGadgetFeatureRegistry(featureRegistry); // Convenience: by default expect no features requested, by gadget or extern. // expectFeatureCalls(...) resets featureRegistry if called again. expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); //Convenience: by default expect that the gadget is allowed to render reset(gadgetAdminStore); expect(gadgetAdminStore.checkFeatureAdminInfo(isA(Gadget.class))).andReturn(true); expect(gadgetAdminStore.isAllowedFeature(isA(Feature.class), isA(Gadget.class))) .andReturn(true).anyTimes(); replay(gadgetAdminStore); return gadget; }
private Gadget makeGadgetWithSpec(String gadgetXml) throws GadgetException { GadgetSpec spec = new GadgetSpec(SPEC_URL, gadgetXml); Gadget gadget = new Gadget() .setContext(context) .setPreloads(ImmutableList.<PreloadedData>of()) .setSpec(spec) .setGadgetFeatureRegistry(featureRegistry); // Convenience: by default expect no features requested, by gadget or extern. // expectFeatureCalls(...) resets featureRegistry if called again. expectFeatureCalls(gadget, ImmutableList.<FeatureResource>of(), ImmutableSet.<String>of(), ImmutableList.<FeatureResource>of()); return gadget; }