Java Code Examples for com.gargoylesoftware.htmlunit.WebResponse#getContentAsString()
The following examples show how to use
com.gargoylesoftware.htmlunit.WebResponse#getContentAsString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadBehaviorJob.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Performs the download and calls the callback method. */ @Override public void run() { final Scriptable scope = callback_.getParentScope(); final WebRequest request = new WebRequest(url_); try { final WebResponse webResponse = client_.loadWebResponse(request); final String content = webResponse.getContentAsString(); if (LOG.isDebugEnabled()) { LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); } final Object[] args = new Object[] {content}; final ContextFactory cf = ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory(); cf.call(cx -> { callback_.call(cx, scope, scope, args); return null; }); } catch (final IOException e) { if (LOG.isErrorEnabled()) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
Example 2
Source File: DownloadBehaviorJob.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Performs the download and calls the callback method. */ @Override public void run() { final Scriptable scope = callback_.getParentScope(); final WebRequest request = new WebRequest(url_); try { final WebResponse webResponse = client_.loadWebResponse(request); final String content = webResponse.getContentAsString(); if (LOG.isDebugEnabled()) { LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); } final Object[] args = new Object[] {content}; final ContextAction action = new ContextAction() { @Override public Object run(final Context cx) { callback_.call(cx, scope, scope, args); return null; } }; final ContextFactory cf = ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory(); cf.call(action); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } }
Example 3
Source File: RoundTripAbstractTest.java From configuration-as-code-plugin with MIT License | 6 votes |
private void applyConfigViaWebUI(String jenkinsConfig) throws Exception { // The UI requires the path to the config file File f = tempFolder.newFile(); writeToFile(jenkinsConfig, f.getAbsolutePath()); // Call the replace url JenkinsRule.WebClient client = r.j.createWebClient(); WebRequest request = new WebRequest(client.createCrumbedUrl("configuration-as-code/replace"), POST); NameValuePair param = new NameValuePair("_.newSource", f.toURI().toURL().toExternalForm()); request.setRequestParameters(Collections.singletonList(param)); request.setRequestParameters(Collections.singletonList(param)); WebResponse response = client.loadWebResponse(request); assertEquals("Failed to POST to " + request.getUrl().toString(), 200, response.getStatusCode()); String res = response.getContentAsString(); /* The result page has: Configuration loaded from : <ul> <li>path</li> </ul> path is the file used to store the configuration. */ assertThat(res, containsString(f.toURI().toURL().toExternalForm())); }
Example 4
Source File: IdpTest.java From cxf-fediz with Apache License 2.0 | 6 votes |
@org.junit.Test public void testSwagger() throws Exception { String url = "https://localhost:" + getIdpHttpsPort() + "/fediz-idp/services/rs/swagger.json"; String user = "alice"; String password = "ecila"; final WebClient webClient = new WebClient(); webClient.getOptions().setUseInsecureSSL(true); webClient.getCredentialsProvider().setCredentials( new AuthScope("localhost", Integer.parseInt(getIdpHttpsPort())), new UsernamePasswordCredentials(user, password)); final UnexpectedPage swaggerPage = webClient.getPage(url); WebResponse response = swaggerPage.getWebResponse(); Assert.assertEquals("application/json", response.getContentType()); String json = response.getContentAsString(); Assert.assertTrue(json.contains("Claims")); webClient.close(); }
Example 5
Source File: DedicatedWorkerGlobalScope.java From htmlunit with Apache License 2.0 | 5 votes |
void loadAndExecute(final WebClient webClient, final String url, final Context context, final boolean checkMimeType) throws IOException { final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage(); final URL fullUrl = page.getFullyQualifiedUrl(url); final WebRequest webRequest = new WebRequest(fullUrl); final WebResponse response = webClient.loadWebResponse(webRequest); if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) { throw Context.reportRuntimeError( "NetworkError: importScripts response is not a javascript response"); } final String scriptCode = response.getContentAsString(); final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine(); final DedicatedWorkerGlobalScope thisScope = this; final ContextAction<Object> action = new ContextAction<Object>() { @Override public Object run(final Context cx) { final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1); return javaScriptEngine.execute(page, thisScope, script); } }; final ContextFactory cf = javaScriptEngine.getContextFactory(); if (context != null) { action.run(context); } else { final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url); owningWindow_.getWebWindow().getJobManager().addJob(job, page); } }
Example 6
Source File: WebClientUtils.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Attaches a visual (GUI) debugger to the specified client. * @param client the client to which the visual debugger is to be attached * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a> */ public static void attachVisualDebugger(final WebClient client) { final ScopeProvider sp = null; final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory(); final Main main = Main.mainEmbedded(cf, sp, "HtmlUnit JavaScript Debugger"); main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH); final SourceProvider sourceProvider = new SourceProvider() { @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; } }; main.setSourceProvider(sourceProvider); }
Example 7
Source File: DedicatedWorkerGlobalScope.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
void loadAndExecute(final String url, final Context context) throws IOException { final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage(); final URL fullUrl = page.getFullyQualifiedUrl(url); final WebClient webClient = owningWindow_.getWebWindow().getWebClient(); final WebRequest webRequest = new WebRequest(fullUrl); final WebResponse response = webClient.loadWebResponse(webRequest); final String scriptCode = response.getContentAsString(); final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine(); final DedicatedWorkerGlobalScope thisScope = this; final ContextAction action = new ContextAction() { @Override public Object run(final Context cx) { final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1); return javaScriptEngine.execute(page, thisScope, script); } }; final ContextFactory cf = javaScriptEngine.getContextFactory(); if (context != null) { action.run(context); } else { final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url); owningWindow_.getWebWindow().getJobManager().addJob(job, page); } }
Example 8
Source File: RoundTripAbstractTest.java From configuration-as-code-plugin with MIT License | 5 votes |
private void assertConfigViaWebUI(String jenkinsConfig) throws Exception { // The UI requires the path to the config file File f = tempFolder.newFile(); writeToFile(jenkinsConfig, f.getAbsolutePath()); // Call the check url JenkinsRule.WebClient client = r.j.createWebClient(); WebRequest request = new WebRequest(client.createCrumbedUrl("configuration-as-code/checkNewSource"), POST); NameValuePair param = new NameValuePair("newSource", f.toURI().toURL().toExternalForm()); request.setRequestParameters(Collections.singletonList(param)); WebResponse response = client.loadWebResponse(request); assertEquals("Failed to POST to " + request.getUrl().toString(), 200, response.getStatusCode()); String res = response.getContentAsString(); assertThat(res, containsString("The configuration can be applied")); }
Example 9
Source File: AbstractRepositoryServletProxiedMetadataTestCase.java From archiva with Apache License 2.0 | 5 votes |
protected String requestMetadataOK( String path ) throws Exception { // process the response code later, not via an exception. //HttpUnitOptions.setExceptionsThrownOnErrorStatus( false ); WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + path ); WebResponse response = getServletUnitClient().getResponse( request ); assertResponseOK( response ); return response.getContentAsString(); }