@Test public void verifyExampleInClassLevelJavadoc() throws Exception { Assume.group(TestGroup.PERFORMANCE); WebClient webClient = new WebClient(); MockMvc mockMvc = MockMvcBuilders.standaloneSetup().build(); MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient); WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); WebConnection httpConnection = new HttpWebConnection(webClient); webClient.setWebConnection( new DelegatingWebConnection(mockConnection, new DelegateWebConnection(cdnMatcher, httpConnection))); Page page = webClient.getPage("http://code.jquery.com/jquery-1.11.0.min.js"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); assertThat(page.getWebResponse().getContentAsString(), not(isEmptyString())); }
private void cleanUp() { Page p = page.get(); if (p != null) { p.cleanUp(); } for (WebWindow window : webClient.get().getWebWindows()) { window.getJobManager().removeAllJobs(); } page.remove(); webClient.get().close(); webClient.remove(); }
private JSONObject callStepsToJson(String jenkinsFileContent) throws IOException { JenkinsRule.WebClient wc = j.createWebClient(); WebRequest req = new WebRequest(new URL(wc.getContextPath() + ModelConverterAction.PIPELINE_CONVERTER_URL + "/stepsToJson"), HttpMethod.POST); assertNotNull(jenkinsFileContent); NameValuePair pair = new NameValuePair("jenkinsfile", jenkinsFileContent); req.setRequestParameters(Collections.singletonList(pair)); String rawResult = wc.getPage(req).getWebResponse().getContentAsString(); assertNotNull(rawResult); return JSONObject.fromObject(rawResult); } }
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); }
private void copyViaHttp(Folder f, JenkinsRule.WebClient wc, String fromName, String toName) throws Exception { // Taken from https://github.com/jenkinsci/jenkins/blob/80aa2c8e4093df270193402c3933f3f1f16271da/test/src/test/java/hudson/jobs/CreateItemTest.java#L68 r.jenkins.setCrumbIssuer(null); URL apiURL = new URL( r.jenkins.getRootUrl().toString() + "/" + f.getUrl().toString() + "createItem?mode=copy&from=" + URLEncoder.encode(fromName, "UTF-8") + "&name=" + URLEncoder.encode(toName, "UTF-8")); WebRequest request = new WebRequest(apiURL, HttpMethod.POST); request.setEncodingType(null); assertEquals("Copy Job request has failed", 200, r.createWebClient() .getPage(request).getWebResponse().getStatusCode()); }
@Override public String getSource(final DebuggableScript script) { String sourceName = script.getSourceName(); if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) { return null; // script is result of eval call. Rhino already knows the source and we don't } if (sourceName.startsWith("script in ")) { sourceName = StringUtils.substringBetween(sourceName, "script in ", " from"); for (final WebWindow ww : client.getWebWindows()) { final WebResponse wr = ww.getEnclosedPage().getWebResponse(); if (sourceName.equals(wr.getWebRequest().getUrl().toString())) { return wr.getContentAsString(); } } } return null; } };
private static String getKey(final Type type, final Page page) { switch (type) { case GLOBAL_STORAGE: return page.getUrl().getHost(); case LOCAL_STORAGE: final URL url = page.getUrl(); return url.getProtocol() + "://" + url.getHost() + ':' + url.getProtocol(); case SESSION_STORAGE: final WebWindow topWindow = page.getEnclosingWindow() .getTopWindow(); return Integer.toHexString(topWindow.hashCode()); default: return null; } }
/** * Constructor. * @param browserVersion the simulated browser version * @param worker the started worker * @throws Exception in case of problem */ DedicatedWorkerGlobalScope(final Window owningWindow, final Context context, final BrowserVersion browserVersion, final Worker worker) throws Exception { context.initStandardObjects(this); final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration( DedicatedWorkerGlobalScope.class, browserVersion); final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, null, browserVersion); setPrototype(prototype); owningWindow_ = owningWindow; final URL currentURL = owningWindow.getWebWindow().getEnclosedPage().getUrl(); origin_ = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort(); worker_ = worker; }
private static String getFileExtension(final Page enclosedPage) { if (enclosedPage != null) { if (enclosedPage.isHtmlPage()) { return "html"; } final URL url = enclosedPage.getUrl(); if (url.getPath().contains(".")) { return StringUtils.substringAfterLast(url.getPath(), "."); } } return ".unknown"; }
public static JsonObject getPageAsJSONObject(String path, URL url, WebClient client) throws IOException { if (client == null) { client = new WebClient(); } JsonReader jsonReader = Json.createReader(client.getPage(url.toString().concat(path)).getWebResponse().getContentAsStream()); return jsonReader.readObject(); }
@Override public String getCurrentUrl() { getWebClient(); // check that session is active Page page = getCurrentWindow().getTopWindow().getEnclosedPage(); if (page == null) { return null; } URL url = page.getUrl(); if (url == null) { return null; } return url.toString(); }
/** * Immediately refreshes the specified page using the specified URL. * @param page the page that is going to be refreshed * @param url the URL where the new page will be loaded * @param seconds the number of seconds to wait before reloading the page (ignored!) * @throws IOException if the refresh fails */ public void handleRefresh(final Page page, final URL url, final int seconds) throws IOException { final WebWindow window = page.getEnclosingWindow(); if (window == null) { return; } final WebClient client = window.getWebClient(); if (page.getWebResponse().getRequestSettings().getUrl().toExternalForm().equals(url.toExternalForm()) && HttpMethod.GET == page.getWebResponse().getRequestSettings().getHttpMethod()) { final String msg = "Refresh Aborted by HtmlUnit: " + "Attempted to refresh a page using an ImmediateRefreshHandler " + "which could have caused an OutOfMemoryError " + "Please use WaitingRefreshHandler or ThreadedRefreshHandler instead."; throw new RuntimeException(msg); } client.getPage(window, new WebRequestSettings(url)); }
/** * Immediately refreshes the specified page using the specified URL. * @param page the page that is going to be refreshed * @param url the URL where the new page will be loaded * @param seconds the number of seconds to wait before reloading the page (ignored!) * @throws IOException if the refresh fails */ @Override public void handleRefresh(final Page page, final URL url, final int seconds) throws IOException { final WebWindow window = page.getEnclosingWindow(); if (window == null) { return; } final WebClient client = window.getWebClient(); if (page.getUrl().toExternalForm().equals(url.toExternalForm()) && HttpMethod.GET == page.getWebResponse().getWebRequest().getHttpMethod()) { final String msg = "Refresh to " + url + " (" + seconds + "s) aborted by HtmlUnit: " + "Attempted to refresh a page using an ImmediateRefreshHandler " + "which could have caused an OutOfMemoryError " + "Please use WaitingRefreshHandler or ThreadedRefreshHandler instead."; throw new RuntimeException(msg); } client.getPage(window, new WebRequest(url)); }
private String getPath() { return lastPage().getWebResponse().getRequestUrl().getPath(); }
private String getDomainForCookie(Cookie cookie) { URL current = lastPage().getWebResponse().getRequestUrl(); if (current.getPort() == 80) { return current.getHost(); } return String.format("%s:%s", current.getHost(), current.getPort()); } }
@Override public String call() throws Exception { WebClient client = new WebClient(); client.setThrowExceptionOnFailingStatusCode(false); client.getCookieManager().addCookie(new Cookie(contextPath.getHost(), JSESSIONID, jsessionid)); Page page = client.getPage(contextPath + "introspect?mode=" + mode + "&cid=" + cid); if (!(page instanceof TextPage)) { return "" + page.getWebResponse().getStatusCode(); } TextPage textPage = (TextPage) page; return textPage.getContent(); }
/** * Initializes the object. * * @param window the window that this location belongs to */ public void initialize(final Window window) { window_ = window; if (window_ != null && window_.getWebWindow().getEnclosedPage() != null) { hash_ = window_.getWebWindow().getEnclosedPage().getWebResponse().getRequestSettings().getUrl().getRef(); } }
/** * Initializes the object. * * @param window the window that this location belongs to */ public void initialize(final Window window) { window_ = window; if (window_ != null && window_.getWebWindow().getEnclosedPage() != null) { setHash(window_.getWebWindow().getEnclosedPage().getUrl().getRef()); } }
@Test public void contextPathNull() throws IOException { this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient)); Page page = this.webClient.getPage("http://localhost/context/a"); assertThat(page.getWebResponse().getStatusCode(), equalTo(200)); }
@Override public String getServerResponse() { StringBuffer result = new StringBuffer(); WebResponse wr = wc.getCurrentWindow().getEnclosedPage() .getWebResponse(); result.append(wr.getStatusCode()).append(" ").append( wr.getStatusMessage()).append("\n"); result.append("Location: ").append(wr.getWebRequest().getUrl()).append("\n"); for (NameValuePair h : wr.getResponseHeaders()) { result.append(h.getName()).append(": ").append(h.getValue()) .append("\n"); } result.append("\n"); result.append(wr.getContentAsString()); return result.toString(); }