/** * Switches the current <em>lastResponse</em> with the <em>suspendedResponse</em> * so the application test can use WicketTester's assert methods. * * Note: Make sure to call {@linkplain #switchOffTestMode()} later to be able to * assert on non-Atmosphere related responses * * @return this instance, for chaining */ public AtmosphereTester switchOnTestMode() { lastResponse = wicketTester.getLastResponse(); wicketTester.setLastResponse(suspendedResponse); return this; }
/** * Asserts that the Ajax location header is present. */ public void assertAjaxLocation() { if (null != getLastResponse().getHeader("Location")) { fail("Location header should *not* be present when using Ajax"); } String ajaxLocation = getLastResponse().getHeader("Ajax-Location"); if (null == ajaxLocation) { throw new AssertionFailedError("Ajax-Location header should be present when using Ajax"); } int statusCode = getLastResponse().getStatus(); if (statusCode != 200) { throw new AssertionFailedError("Expected HTTP status code to be 200 (OK)"); } }
/** * Assert that the last request redirected to the given Url. * * @param expectedRedirectUrl * expected */ public void assertRedirectUrl(String expectedRedirectUrl) { String actualRedirectUrl = getLastResponse().getRedirectLocation(); assertEquals(expectedRedirectUrl, actualRedirectUrl); }
/** * Assert that the last request redirected to the given Url. * * @param expectedRedirectUrl * expected */ public void assertRedirectUrl(String expectedRedirectUrl) { String actualRedirectUrl = getLastResponse().getRedirectLocation(); assertEquals(expectedRedirectUrl, actualRedirectUrl); } }
/** * Asserts that the Ajax location header is present. */ public void assertAjaxLocation() { if (null != getLastResponse().getHeader("Location")) { fail("Location header should *not* be present when using Ajax"); } String ajaxLocation = getLastResponse().getHeader("Ajax-Location"); if (null == ajaxLocation) { fail("Ajax-Location header should be present when using Ajax"); } int statusCode = getLastResponse().getStatus(); if (statusCode != 200) { fail("Expected HTTP status code to be 200 (OK)"); } }
WicketTester tester = ...; // set up tester tester.setFollowRedirects(false); // important tester.startpage(...); // or tester.executeUrl(...); // then test to see if there was a redirect assert tester.getLastResponse().getRedirectLocation() != null; assert aTester.getLastResponse().getRedirectLocation().endsWith("...."); // then manually redirect to that page and test its the page you expected Url url = Url.parse(aTester.getLastResponse().getRedirectLocation()); aTester.executeUrl(url.getPath().substring(1)); // the substring chops off and leading / aTester.assertRenderedPage(YourRedirectPage.class);
@Test public void testRetrieveContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "name", null, false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_OK); }
@Test public void testRetrieveNotExistsContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "notFound", "text/plain", false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND); }
@Test public void testRetrieveEmptyContent() { tester.executeUrl(OContentShareResource.urlFor(doc, "phone", "text/plain", false).toString()); MockHttpServletResponse response = tester.getLastResponse(); assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND); } }
@Test public void testHeaderContribution() throws Exception { HeaderContribution testHeaderContribution = new HeaderContribution(); testHeaderContribution.setCSSFilename("testHeaderContribution.css"); testHeaderContribution.setScope(getClass()); extensions.singleton( "testHeaderContribution", testHeaderContribution, HeaderContribution.class); tester.startPage(SRSListPage.class); tester.clickLink( "srsListPanel:table:listContainer:items:1:itemProperties:0:component:link"); tester.assertRenderedPage(SRSDescriptionPage.class); assertTrue(tester.getLastResponse().getDocument().contains("testHeaderContribution.css")); } }
@Test public void testDownload() throws Exception { assertFalse(tester.getComponentFromLastRenderedPage("download").isEnabled()); // select resource tester.executeAjaxEvent( "treeview:rootView:/:children:temp:children:temp/dir:children:temp/dir/something:selectableLabel", "click"); assertTrue(tester.getComponentFromLastRenderedPage("download").isEnabled()); // rename resource tester.clickLink("download"); assertTrue(Arrays.equals(DATA.getBytes(), tester.getLastResponse().getBinaryContent())); }