/** * Go to a particular page. * * @throws TestingEngineResponseException if an error response code is encountered * and ignoreFailingStatusCodes is not enabled. */ @Override public void gotoPage(URL initialURL) throws TestingEngineResponseException { try { wc.getPage(initialURL); win = wc.getCurrentWindow(); form = null; } catch (FailingHttpStatusCodeException ex) { throw new TestingEngineResponseException(ex.getStatusCode(), "unexpected status code [" + ex.getStatusCode() + "] at URL: [" + initialURL + "]", ex); } catch (IOException ex) { throw new RuntimeException(ex); } }
/** * Verify that the server rejects an attempt to load the given page. * @param url a URL path (relative to Jenkins root) * @param statusCode the expected failure code (such as {@link HttpURLConnection#HTTP_FORBIDDEN}) * @since 1.502 */ public void assertFails(String url, int statusCode) throws Exception { try { fail(url + " should have been rejected but produced: " + super.getPage(getContextPath() + url).getWebResponse().getContentAsString()); } catch (FailingHttpStatusCodeException x) { assertEquals(statusCode, x.getStatusCode()); } }
/** * Submit the current form with the default submit button. See {@link #getForm}for an explanation of how the * current form is established. */ @Override public void submit() { HtmlElement btn = getSubmitButton(); if (btn == null) { throw new RuntimeException("No submit button found in current form."); } try { btn.click(); } catch (FailingHttpStatusCodeException e) { throw new TestingEngineResponseException( e.getStatusCode(), e); } catch (IOException e) { throw new RuntimeException( "HtmlUnit Error submitting form using default submit button, " + "check that form has single submit button, otherwise use submit(name): \n", e); } }
/** * {@inheritDoc} */ @Override public String getMessage() { final int code = getStatusCode(); final String msg = getStatusMessage(); final URL url = getResponse().getRequestSettings().getUrl(); return code + " " + msg + " for " + url; }
/** * {@inheritDoc} */ @Override public String getMessage() { final int code = getStatusCode(); final String msg = getStatusMessage(); final URL url = getResponse().getRequestSettings().getUrl(); return code + " " + msg + " for " + url; }
/** * {@inheritDoc} */ @Override public String getMessage() { final int code = getStatusCode(); final String msg = getStatusMessage(); final URL url = getResponse().getRequestSettings().getUrl(); return code + " " + msg + " for " + url; }
@Override public InputStream getInputStream(URL resourceUrl) throws TestingEngineResponseException { WebWindow imageWindow = null; try { // as far as I can tell, there is no such thing as an iframe/object kind of "window" in htmlunit, so I'm // opening a fake new window here imageWindow = wc.openWindow(resourceUrl, "for_stream"); Page page = imageWindow.getEnclosedPage(); return page.getWebResponse().getContentAsStream(); } catch (FailingHttpStatusCodeException aException) { throw new TestingEngineResponseException( aException.getStatusCode(), aException); } catch (IOException e) { throw new RuntimeException(e); } finally { if (imageWindow != null) { wc.deregisterWebWindow(imageWindow); } } }
public void testPublicFieldDispatch() throws Exception { WebClient wc = new WebClient(); URL url = new URL(this.url, "testWithPublicField/testClass/value/"); try { wc.getPage(url); } catch (FailingHttpStatusCodeException e) { assertEquals(HttpServletResponse.SC_OK, e.getStatusCode()); } }
public void testProtectedMethodDispatch() throws Exception { WebClient wc = new WebClient(); wc.getPage(new URL(url, "public/value")); try { wc.getPage(new URL(url, "protected/value")); fail("should not have allowed protected access"); } catch (FailingHttpStatusCodeException x) { assertEquals(HttpServletResponse.SC_NOT_FOUND, x.getStatusCode()); } try { wc.getPage(new URL(url, "private/value")); fail("should not have allowed private access"); } catch (FailingHttpStatusCodeException x) { assertEquals(HttpServletResponse.SC_NOT_FOUND, x.getStatusCode()); } } public TestClass getPublic() {return new TestClass();}
/** * Verify that the server rejects an attempt to load the given page. * @param url a URL path (relative to Jenkins root) * @param statusCode the expected failure code (such as {@link HttpURLConnection#HTTP_FORBIDDEN}) * @since 1.504 */ public void assertFails(String url, int statusCode) throws Exception { assert !url.startsWith("/"); boolean currentConfiguration = isThrowExceptionOnFailingStatusCode(); // enforce the throwing of exception for the catch scope only setThrowExceptionOnFailingStatusCode(true); try { fail(url + " should have been rejected but produced: " + super.getPage(getContextPath() + url).getWebResponse().getContentAsString()); } catch (FailingHttpStatusCodeException x) { assertEquals(statusCode, x.getStatusCode()); } finally { setThrowExceptionOnFailingStatusCode(currentConfiguration); } }
public void assertInternalError() { WebClient client = new WebClient(); try { Page page = client.getPage(deploymentURL + "/juzu"); throw failure("Was expecting an internal error instead of page " + page.toString()); } catch (FailingHttpStatusCodeException e) { assertEquals(500, e.getStatusCode()); } catch (IOException e) { throw failure("Was not expecting io exception", e); } }
public void assertInternalError() { WebClient client = new WebClient(); try { Page page = client.getPage(deploymentURL + "/juzu"); throw failure("Was expecting an internal error instead of page " + page.toString()); } catch (FailingHttpStatusCodeException e) { assertEquals(500, e.getStatusCode()); } catch (IOException e) { throw failure("Was not expecting io exception", e); } }
public void testInheritance() throws Exception { WebClient wc = new WebClient(); // the request should get to the overriding method and it should still see all the annotations in the base type TextPage p = wc.getPage(new URL(url, "inheritance/foo?q=abc")); assertEquals("abc", p.getContent()); // doBar is a web method for 'foo', so bar endpoint shouldn't respond try { wc.getPage(new URL(url, "inheritance/bar")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(404,e.getStatusCode()); } }
public void testInterfaceMethods() throws Exception { WebClient wc = new WebClient(); try { wc.getPage(new URL(url, "usesInterfaceMethods/foo")); fail(); } catch (FailingHttpStatusCodeException x) { assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, x.getStatusCode()); } assertEquals("default", wc.getPage(new WebRequestSettings(new URL(url, "usesInterfaceMethods/foo"), HttpMethod.POST)).getWebResponse().getContentAsString().trim()); try { wc.getPage(new URL(url, "overridesInterfaceMethods/foo")); fail(); } catch (FailingHttpStatusCodeException x) { assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, x.getStatusCode()); } assertEquals("due to UnionAnnotatedElement it is even inherited", "overridden", wc.getPage(new WebRequestSettings(new URL(url, "overridesInterfaceMethods/foo"), HttpMethod.POST)).getWebResponse().getContentAsString().trim()); } public interface InterfaceWithWebMethods {
public void testInterceptorStageContentTypeWithCharset() throws Exception { WebClient wc = new WebClient(); try { wc.getPage(new URL(url, "interceptorStage/foo")); fail("Expected 404"); } catch (FailingHttpStatusCodeException e) { assertEquals(404, e.getStatusCode()); } WebRequestSettings req = new WebRequestSettings(new URL(url, "interceptorStage/foo"), HttpMethod.POST); req.setAdditionalHeader("Content-Type","application/json; charset=utf-8"); req.setRequestBody("{x:3,y:5}"); TextPage p = wc.getPage(req); assertEquals("3,5",p.getContent()); }
public void testRequirePostOnBase() throws Exception { WebClient wc = new WebClient(); URL url = new URL(this.url, "requirePostOnBase/something"); try { wc.getPage(url); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getStatusCode()); } // POST should succeed wc.getPage(new WebRequestSettings(url, HttpMethod.POST)); assertEquals(1, requirePostOnBase.hit); }
/** * POST annotation selection needs to happen before databinding of the parameter happens. */ public void testInterceptorStage() throws Exception { WebClient wc = new WebClient(); try { wc.getPage(new URL(url, "interceptorStage/foo")); fail("Expected 404"); } catch (FailingHttpStatusCodeException e) { assertEquals(404, e.getStatusCode()); } WebRequestSettings req = new WebRequestSettings(new URL(url, "interceptorStage/foo"), HttpMethod.POST); req.setAdditionalHeader("Content-Type","application/json"); req.setRequestBody("{x:3,y:5}"); TextPage p = wc.getPage(req); assertEquals("3,5",p.getContent()); }
public void testStaplerProxy() throws Exception { WebClient wc = new WebClient(); Page p = wc.getPage(new URL(url, "staplerProxyOK")); assertEquals(200, p.getWebResponse().getStatusCode()); try { p = wc.getPage(new URL(url, "staplerProxyFail")); fail("expected failure"); } catch (FailingHttpStatusCodeException ex) { assertEquals(404, ex.getStatusCode()); } assertTrue(staplerProxyOK.counter > 0); assertTrue(staplerProxyFail.counter > 0); }
/** * Tests the dispatching of WebMethod based on verb */ public void testVerbMatch() throws Exception { WebClient wc = new WebClient(); check(wc, HttpMethod.GET); check(wc, HttpMethod.POST); try { check(wc, HttpMethod.DELETE); fail("There's no route for DELETE"); } catch (FailingHttpStatusCodeException e) { assertEquals(404, e.getStatusCode()); } }
@Test public void canStillProvideTheCommitNotifyAction() throws Exception { j.jenkins.setCrumbIssuer(null); String uuid = "12345678-1234-1234-1234-123456789012"; JenkinsRule.WebClient wc = j.createWebClient(); String relativeUrl = "subversion/" + uuid + "/notifyCommit/"; try { // protected against GET request wc.goTo(relativeUrl); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(405, e.getStatusCode()); } WebRequest request = new WebRequest(new URL(j.getURL() + relativeUrl), HttpMethod.POST); HtmlPage page = wc.getPage(request); j.assertGoodStatus(page); }