com.gargoylesoftware.htmlunit.WebRequest Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.WebRequest. 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: HtmlFileInputTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void contentTypeHeader() throws Exception {
    final String htmlContent
        = "<html>\n"
            + "<body>\n"
            + "<form action='upload2' method='post' enctype='multipart/form-data'>\n"
            + "Name: <input name='myInput' type='file'><br>\n"
            + "Name 2 (should stay empty): <input name='myInput2' type='file'><br>\n"
            + "<input type='submit' value='Upload' id='mySubmit'>\n"
            + "</form>\n"
            + "</body></html>\n";
    getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);

    final WebDriver driver = loadPage2(htmlContent);
    String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm();
    path = path.substring(path.indexOf('/') + 1).replace('/', '\\');

    driver.findElement(By.name("myInput")).sendKeys(path);
    driver.findElement(By.id("mySubmit")).click();

    final WebRequest request = getMockWebConnection().getLastWebRequest();
    final String contentType = request.getAdditionalHeaders().get(HttpHeader.CONTENT_TYPE);
    assertTrue(StringUtils.isNotBlank(contentType));
    assertFalse(StringUtils.containsIgnoreCase(contentType, "charset"));
}
 
Example #2
Source File: MockMvcWebConnection.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public WebResponse getResponse(WebRequest webRequest) throws IOException {
	long startTime = System.currentTimeMillis();
	HtmlUnitRequestBuilder requestBuilder = new HtmlUnitRequestBuilder(this.sessions, this.webClient, webRequest);
	requestBuilder.setContextPath(this.contextPath);

	MockHttpServletResponse httpServletResponse = getResponse(requestBuilder);
	String forwardedUrl = httpServletResponse.getForwardedUrl();
	while (forwardedUrl != null) {
		requestBuilder.setForwardPostProcessor(new ForwardRequestPostProcessor(forwardedUrl));
		httpServletResponse = getResponse(requestBuilder);
		forwardedUrl = httpServletResponse.getForwardedUrl();
	}
	storeCookies(webRequest, httpServletResponse.getCookies());

	return new MockWebResponseBuilder(startTime, webRequest, httpServletResponse).build();
}
 
Example #3
Source File: RepositoryServletNoProxyTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
/**
 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
 */
@Test
public void testGetNoProxyDualExtensionDefaultLayoutManagedLegacy()
    throws Exception
{
    String expectedContents = "the-contents-of-the-dual-extension";
    String dualExtensionPath = "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip";

    Path checksumFile = repoRootLegacy.resolve( "org.project/distributions/example-presentation-3.2.xml.zip" );
    Files.createDirectories(checksumFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFile, Charset.defaultCharset(), expectedContents);

    WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + dualExtensionPath );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseNotFound( response );
}
 
Example #4
Source File: MockMvcWebConnection.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void storeCookies(WebRequest webRequest, javax.servlet.http.Cookie[] cookies) {
	Date now = new Date();
	CookieManager cookieManager = this.webClient.getCookieManager();
	for (javax.servlet.http.Cookie cookie : cookies) {
		if (cookie.getDomain() == null) {
			cookie.setDomain(webRequest.getUrl().getHost());
		}
		Cookie toManage = createCookie(cookie);
		Date expires = toManage.getExpires();
		if (expires == null || expires.after(now)) {
			cookieManager.addCookie(toManage);
		}
		else {
			cookieManager.removeCookie(toManage);
		}
	}
}
 
Example #5
Source File: RepositoryServletNoProxyTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNoProxyVersionedMetadataDefaultLayoutManagedLegacy()
    throws Exception
{
    String commonsLangMetadata = "commons-lang/commons-lang/2.1/maven-metadata.xml";
    String expectedMetadataContents = "dummy-versioned-metadata";

    // TODO: find out what this should be from maven-artifact
    Path metadataFile = repoRootLegacy.resolve(commonsLangMetadata);
    Files.createDirectories(metadataFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile(metadataFile, Charset.defaultCharset(), expectedMetadataContents);

    WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangMetadata );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseNotFound( response );
}
 
Example #6
Source File: DownloadBehaviorJob.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #7
Source File: RepositoryServletNoProxyTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNoProxyDistributionLegacyLayout()
    throws Exception
{
    String expectedContents = "the-contents-of-the-dual-extension";
    String dualExtensionPath = "org/project/example-presentation/3.2/example-presentation-3.2.zip";

    Path checksumFile = repoRootInternal.resolve(dualExtensionPath);
    Files.createDirectories(checksumFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFile, Charset.defaultCharset(), expectedContents);

    WebRequest request = new GetMethodWebRequest(
        "http://machine.com/repository/internal/" + "org.project/distributions/example-presentation-3.2.zip" );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseNotFound( response );

}
 
Example #8
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void testCSRFClientRegistration() throws Exception {
    // Login to the client page successfully
    try (WebClient webClient = setupWebClientIDP("alice", "ecila")) {
        final UriBuilder clientsUrl = oidcEndpointBuilder("/console/clients");
        login(clientsUrl, webClient);

        // Register a new client
        WebRequest request = new WebRequest(clientsUrl.build().toURL(), HttpMethod.POST);
        request.setRequestParameters(Arrays.asList(
            new NameValuePair("client_name", "bad_client"),
            new NameValuePair("client_type", "confidential"),
            new NameValuePair("client_redirectURI", "https://127.0.0.1"),
            new NameValuePair("client_audience", ""),
            new NameValuePair("client_logoutURI", ""),
            new NameValuePair("client_homeRealm", ""),
            new NameValuePair("client_csrfToken", "12345")));

        HtmlPage registeredClientPage = webClient.getPage(request);
        assertTrue(registeredClientPage.asXml().contains("Invalid CSRF Token"));
    }
}
 
Example #9
Source File: HostRequestMatcher.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(WebRequest request) {
	URL url = request.getUrl();
	String host = url.getHost();

	if (this.hosts.contains(host)) {
		return true;
	}

	int port = url.getPort();
	if (port == -1) {
		port = url.getDefaultPort();
	}
	String hostAndPort = host + ":" + port;

	return this.hosts.contains(hostAndPort);
}
 
Example #10
Source File: RepositoryServletNoProxyMetadataTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetProjectMetadataDefaultLayout()
    throws Exception
{
    String commonsLangMetadata = "commons-lang/commons-lang/maven-metadata.xml";
    String expectedMetadataContents = "metadata-for-commons-lang-version-for-project";

    Path checksumFile = repoRootInternal.resolve(commonsLangMetadata);
    Files.createDirectories(checksumFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile( checksumFile, Charset.defaultCharset(), expectedMetadataContents );

    WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangMetadata );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseOK( response );

    assertEquals( "Expected file contents", expectedMetadataContents, response.getContentAsString() );
}
 
Example #11
Source File: HTMLFormElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Submits the form (at the end of the current script execution).
 */
@JsxFunction
public void submit() {
    final HtmlPage page = (HtmlPage) getDomNodeOrDie().getPage();
    final WebClient webClient = page.getWebClient();

    final String action = getHtmlForm().getActionAttribute().trim();
    if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        final String js = action.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length());
        webClient.getJavaScriptEngine().execute(page, js, "Form action", 0);
    }
    else {
        // download should be done ASAP, response will be loaded into a window later
        final WebRequest request = getHtmlForm().getWebRequest(null);
        final String target = page.getResolvedTarget(getTarget());
        final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD);
        final boolean checkHash =
                !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED);
        webClient.download(page.getEnclosingWindow(),
                    target, request, checkHash, forceDownload, "JS form.submit()");
    }
}
 
Example #12
Source File: RoundTripAbstractTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
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 #13
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void init() {
    FrameWindow enclosedWindow = null;
    try {
        final HtmlPage htmlPage = getHtmlPageOrNull();
        if (null != htmlPage) { // if loaded as part of XHR.responseXML, don't load content
            enclosedWindow = new FrameWindow(this);
            // put about:blank in the window to allow JS to run on this frame before the
            // real content is loaded
            final WebClient webClient = htmlPage.getWebClient();
            final HtmlPage temporaryPage = webClient.getPage(enclosedWindow, WebRequest.newAboutBlankRequest());
            temporaryPage.setReadyState(READY_STATE_LOADING);
        }
    }
    catch (final FailingHttpStatusCodeException | IOException e) {
        // should never occur
    }
    enclosedWindow_ = enclosedWindow;
}
 
Example #14
Source File: HtmlImageInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();
        if (!"".equals(src)
                && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();

            final URL url = page.getFullyQualifiedUrl(src);
            final BrowserVersion browser = webClient.getBrowserVersion();
            final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                            browser.getAcceptEncodingHeader());
            request.setCharset(page.getCharset());
            request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
            imageWebResponse_ = webClient.loadWebResponse(request);
        }

        downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));
    }
}
 
Example #15
Source File: HtmlLink.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * If the linked content is not already downloaded it triggers a download. Then it stores the response
 * for later use.<br>
 *
 * @param downloadIfNeeded indicates if a request should be performed this hasn't been done previously
 * @param request the request; if null getWebRequest() is called to create one
 * @return {@code null} if no download should be performed and when this wasn't already done; the response
 * received when performing a request for the content referenced by this tag otherwise
 * @throws IOException if an error occurs while downloading the content
 */
public WebResponse getWebResponse(final boolean downloadIfNeeded, WebRequest request) throws IOException {
    if (downloadIfNeeded && cachedWebResponse_ == null) {
        final WebClient webclient = getPage().getWebClient();
        if (null == request) {
            request = getWebRequest();
        }
        try {
            cachedWebResponse_ = webclient.loadWebResponse(request);
            final int statusCode = cachedWebResponse_.getStatusCode();
            final boolean successful = statusCode >= HttpStatus.SC_OK
                                            && statusCode < HttpStatus.SC_MULTIPLE_CHOICES;
            if (successful) {
                executeEvent(Event.TYPE_LOAD);
            }
            else {
                executeEvent(Event.TYPE_ERROR);
            }
        }
        catch (final IOException e) {
            executeEvent(Event.TYPE_ERROR);
            throw e;
        }
    }
    return cachedWebResponse_;
}
 
Example #16
Source File: RepositoryServletNoProxyMetadataTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetVersionMetadataDefaultLayout()
    throws Exception
{
    String commonsLangMetadata = "commons-lang/commons-lang/2.1/maven-metadata.xml";
    String expectedMetadataContents = "metadata-for-commons-lang-version-2.1";

    Path checksumFile = repoRootInternal.resolve(commonsLangMetadata);
    Files.createDirectories(checksumFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile( checksumFile, Charset.defaultCharset(), expectedMetadataContents );

    WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangMetadata );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseOK( response );

    assertEquals( "Expected file contents", expectedMetadataContents, response.getContentAsString() );
}
 
Example #17
Source File: XMLHttpRequest3Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
private void testMethod(final HttpMethod method) throws Exception {
    final String content = "<html><head><script>\n"
        + "function test() {\n"
        + "  var req = new XMLHttpRequest();\n"
        + "  req.open('" + method.name().toLowerCase(Locale.ROOT) + "', 'foo.xml', false);\n"
        + "  req.send('');\n"
        + "}\n"
        + "</script></head>\n"
        + "<body onload='test()'></body></html>";

    final WebClient client = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, content);
    final URL urlPage2 = new URL(URL_FIRST, "foo.xml");
    conn.setResponse(urlPage2, "<foo/>\n", MimeType.TEXT_XML);
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);

    final WebRequest request = conn.getLastWebRequest();
    assertEquals(urlPage2, request.getUrl());
    assertSame(method, request.getHttpMethod());
}
 
Example #18
Source File: XMLHttpRequest2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void setRequestHeader() throws Exception {
    final String html = "<html><head><script>\n"
        + "  function test() {\n"
        + "    var xhr = new XMLHttpRequest();\n"
        + "    xhr.open('GET', 'second.html', false);\n"
        + "    xhr.setRequestHeader('Accept', 'text/javascript, application/javascript, */*');\n"
        + "    xhr.setRequestHeader('Accept-Language', 'ar-eg');\n"
        + "    xhr.send('');\n"
        + "  }\n"
        + "</script></head><body onload='test()'></body></html>";

    getMockWebConnection().setDefaultResponse("");
    loadPage2(html);

    final WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
    final Map<String, String> headers = lastRequest.getAdditionalHeaders();
    assertEquals("text/javascript, application/javascript, */*", headers.get(HttpHeader.ACCEPT));
    assertEquals("ar-eg", headers.get(HttpHeader.ACCEPT_LANGUAGE));
}
 
Example #19
Source File: XMLHttpRequestTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the <tt>Referer</tt> header is set correctly.
 * @throws Exception if the test fails
 */
@Test
public void refererHeader() throws Exception {
    final String html = "<html><head><script>\n"
        + "function test() {\n"
        + "  req = new XMLHttpRequest();\n"
        + "  req.open('post', 'foo.xml', false);\n"
        + "  req.send('');\n"
        + "}\n"
        + "</script></head>\n"
        + "<body onload='test()'></body></html>";

    final URL urlPage2 = new URL(URL_FIRST, "foo.xml");
    getMockWebConnection().setResponse(urlPage2, "<foo/>\n", MimeType.TEXT_XML);
    loadPage2(html);

    final WebRequest request = getMockWebConnection().getLastWebRequest();
    assertEquals(urlPage2, request.getUrl());
    assertEquals(URL_FIRST.toExternalForm(), request.getAdditionalHeaders().get(HttpHeader.REFERER));
}
 
Example #20
Source File: RepositoryServletNoProxyTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNoProxySnapshotArtifactDefaultLayoutManagedLegacy()
    throws Exception
{
    String commonsLangJar = "commons-lang/commons-lang/2.1-SNAPSHOT/commons-lang-2.1-SNAPSHOT.jar";
    String expectedArtifactContents = "dummy-commons-lang-snapshot-artifact";

    Path artifactFile = repoRootLegacy.resolve( "commons-lang/jars/commons-lang-2.1-SNAPSHOT.jar" );
    Files.createDirectories(artifactFile.getParent());

    org.apache.archiva.common.utils.FileUtils.writeStringToFile(artifactFile, Charset.defaultCharset(), expectedArtifactContents);

    WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/legacy/" + commonsLangJar );
    WebResponse response = getServletUnitClient().getResponse( request );
    assertResponseNotFound( response );
}
 
Example #21
Source File: DebuggingWebConnectionTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that Content-Encoding headers are removed when JavaScript is uncompressed.
 * (was causing java.io.IOException: Not in GZIP format).
 * @throws Exception if the test fails
 */
@Test
public void gzip() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
        IOUtils.write("alert(1)", gzipOutputStream, UTF_8);
    }

    final MockWebConnection mockConnection = new MockWebConnection();
    final List<NameValuePair> responseHeaders = Arrays.asList(
        new NameValuePair("Content-Encoding", "gzip"));
    mockConnection.setResponse(URL_FIRST, baos.toByteArray(), 200, "OK", MimeType.APPLICATION_JAVASCRIPT,
        responseHeaders);

    final String dirName = "test-" + getClass().getSimpleName();
    try (DebuggingWebConnection dwc = new DebuggingWebConnection(mockConnection, dirName)) {

        final WebRequest request = new WebRequest(URL_FIRST);
        final WebResponse response = dwc.getResponse(request); // was throwing here
        assertNull(response.getResponseHeaderValue("Content-Encoding"));

        FileUtils.deleteDirectory(dwc.getReportFolder());
    }
}
 
Example #22
Source File: HostRequestMatcher.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public boolean matches(WebRequest request) {
	URL url = request.getUrl();
	String host = url.getHost();

	if (this.hosts.contains(host)) {
		return true;
	}

	int port = url.getPort();
	if (port == -1) {
		port = url.getDefaultPort();
	}
	String hostAndPort = host + ":" + port;

	return this.hosts.contains(hostAndPort);
}
 
Example #23
Source File: Location.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Reloads the current page, possibly forcing retrieval from the server even if
 * the browser cache contains the latest version of the document.
 * @param force if {@code true}, force reload from server; otherwise, may reload from cache
 * @throws IOException if there is a problem reloading the page
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536342.aspx">MSDN Documentation</a>
 */
@JsxFunction
public void reload(final boolean force) throws IOException {
    final HtmlPage htmlPage = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
    final WebRequest request = htmlPage.getWebResponse().getWebRequest();

    if (getBrowserVersion().hasFeature(JS_LOCATION_RELOAD_REFERRER)) {
        final String referer = htmlPage.getUrl().toExternalForm();
        request.setAdditionalHeader(HttpHeader.REFERER, referer);
    }

    final WebWindow webWindow = window_.getWebWindow();
    webWindow.getWebClient().download(webWindow, "", request, true, false, "JS location.reload");
}
 
Example #24
Source File: DedicatedWorkerGlobalScope.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
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 #25
Source File: XMLHTTPRequestTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "no ActiveX",
        IE = {"*/*",
               "gzip, deflate",
               "4",
               "localhost:12345",
               "§§URL§§"})
public void send_headersDefaultBody() throws Exception {
    expandExpectedAlertsVariables(URL_FIRST);
    final String[] expectedHeaders = getExpectedAlerts();
    if (getExpectedAlerts().length > 1) {
        setExpectedAlerts();
    }

    final String test = ""
        + "try {\n"
        + "  xhr.open('GET', '" + URL_SECOND + "', false);\n"
        + "  xhr.send('1234');\n"
        + "} catch(e) { alert('exception-send'); }\n";

    tester(test, "");

    if (getExpectedAlerts().length > 1) {
        final WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
        final Map<String, String> headers = lastRequest.getAdditionalHeaders();
        assertEquals(expectedHeaders[0], "" + headers.get(HttpHeader.ACCEPT));
        assertEquals(expectedHeaders[1], "" + headers.get(HttpHeader.ACCEPT_ENCODING));
        assertEquals(expectedHeaders[2], "" + headers.get(HttpHeader.CONTENT_LENGTH));
        assertEquals(expectedHeaders[3], "" + headers.get(HttpHeader.HOST));
        assertEquals(expectedHeaders[4], "" + headers.get(HttpHeader.REFERER));
    }
}
 
Example #26
Source File: Location.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location URL to an entirely new value.
 * @param newLocation the new location URL
 * @throws IOException if loading the specified location fails
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
 */
@JsxSetter
public void setHref(final String newLocation) throws IOException {
    final HtmlPage page = (HtmlPage) getWindow(getStartingScope()).getWebWindow().getEnclosedPage();
    if (newLocation.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        final String script = newLocation.substring(11);
        page.executeJavaScript(script, "new location value", 1);
        return;
    }
    try {
        URL url = page.getFullyQualifiedUrl(newLocation);
        // fix for empty url
        if (StringUtils.isEmpty(newLocation)) {
            final boolean dropFilename = page.getWebClient().getBrowserVersion().
                    hasFeature(ANCHOR_EMPTY_HREF_NO_FILENAME);
            if (dropFilename) {
                String path = url.getPath();
                path = path.substring(0, path.lastIndexOf('/') + 1);
                url = UrlUtils.getUrlWithNewPath(url, path);
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
            else {
                url = UrlUtils.getUrlWithNewRef(url, null);
            }
        }

        final WebRequest request = new WebRequest(url);
        request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());

        final WebWindow webWindow = window_.getWebWindow();
        webWindow.getWebClient().download(webWindow, "", request, true, false, "JS set location");
    }
    catch (final MalformedURLException e) {
        LOG.error("setHref('" + newLocation + "') got MalformedURLException", e);
        throw e;
    }
}
 
Example #27
Source File: HtmlImage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();

        if (!"".equals(src)) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();
            final BrowserVersion browser = webClient.getBrowserVersion();

            if (!(browser.hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY)
                    && StringUtils.isBlank(src))) {
                final URL url = page.getFullyQualifiedUrl(src);
                final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                                browser.getAcceptEncodingHeader());
                request.setCharset(page.getCharset());
                request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
                imageWebResponse_ = webClient.loadWebResponse(request);
            }
        }

        if (imageData_ != null) {
            imageData_.close();
            imageData_ = null;
        }
        downloaded_ = true;
        isComplete_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));

        width_ = -1;
        height_ = -1;
    }
}
 
Example #28
Source File: ProducesIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoProducesEmptyAccept2() throws Exception {
    final WebResponse wr = webClient.loadWebResponse(
            new WebRequest(new URL(webUrl + "resources/no_produces2"), ""));
    assertEquals(Response.Status.OK.getStatusCode(), wr.getStatusCode());
    assertEquals(MediaType.TEXT_HTML, wr.getContentType());     // default
}
 
Example #29
Source File: RegexHttpWebConnection.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
    final URL url = request.getUrl();
    if (StringUtils.isBlank(filter(url.toString())) || url.toString().indexOf("robots.txt") > -1) {
        LOG.info("Thread: {}, - Http Excluding URL: {}", Thread.currentThread().getId(), url);
        return new StringWebResponse("", url);
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Thread: {}, + Http Fetching URL: {}", Thread.currentThread().getId(), url);
    }
    return super.getResponse(request);
}
 
Example #30
Source File: ProducesIT.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void locale1() throws Exception {
    final WebRequest wrq = new WebRequest(new URL(webUrl + "resources/locale1"), ACCEPT_HEADER);
    wrq.setAdditionalHeader("Accept-Language", ACCEPT_LANGUAGE);
    final WebResponse wr = webClient.loadWebResponse(wrq);
    assertEquals(Response.Status.OK.getStatusCode(), wr.getStatusCode());
    assertEquals(MediaType.APPLICATION_XHTML_XML, wr.getContentType());
    assertEquals("en-GB", wr.getResponseHeaderValue("Content-Language"));
}