/** * Loads an XML document using the supplied string. * * @param strXML A string containing the XML string to load into this XML document object This * string can contain an entire XML document or a well-formed fragment. * @return true if the load succeeded; false if the load failed */ public boolean jsxFunction_loadXML(final String strXML) { try { final WebWindow webWindow = getWindow().getWebWindow(); // determine the charset of the page String charset = TextUtil.DEFAULT_CHARSET; final SgmlPage sgmlPage = (SgmlPage) webWindow.getEnclosedPage(); if (sgmlPage != null) { final String contentCharset = sgmlPage.getWebResponse().getContentCharset(); if (contentCharset != null) { charset = contentCharset; } } // build a dummy WebResponse List<NameValuePair> headers = Collections.emptyList(); final WebResponseData data = new WebResponseData( TextUtil.stringToByteArray(strXML, charset), HttpStatus.SC_OK, null, headers); final URL hackUrl = new URL("http://-htmlunit-internal/XMLDocument.loadXML"); // hack! better solution? final WebResponse webResponse = new WebResponse(data, hackUrl, (HttpMethod) null, 0); webResponse.getWebRequest().setCharset(charset); final XmlPage page = new XmlPage(webResponse, webWindow); setDomNode(page); return true; } catch (final IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error parsing XML\n" + strXML, e); } return false; } }
/** @throws Exception if the test fails */ @Test public void getWebResponse() throws Exception { final HtmlImage htmlimage = getHtmlElementToTest("image1"); final URL url = htmlimage.getPage().getWebResponse().getWebRequest().getUrl(); Assert.assertNull(htmlimage.getWebResponse(false)); final WebResponse resp = htmlimage.getWebResponse(true); Assert.assertNotNull(resp); assertEquals(url.toExternalForm(), resp.getWebRequest().getAdditionalHeaders().get("Referer")); }
/** * Tries to uncompress the JavaScript code in the provided response. * * @param response the response to uncompress * @return a new response with uncompressed JavaScript code or the original response in case of * failure */ protected WebResponse uncompressJavaScript(final WebResponse response) { final WebRequest request = response.getWebRequest(); final String scriptName = request.getUrl().toString(); final String scriptSource = response.getContentAsString(); // skip if it is already formatted? => TODO final ContextFactory factory = new ContextFactory(); final ContextAction action = new ContextAction() { public Object run(final Context cx) { cx.setOptimizationLevel(-1); final Script script = cx.compileString(scriptSource, scriptName, 0, null); return cx.decompileScript(script, 4); } }; try { final String decompileScript = (String) factory.call(action); final List<NameValuePair> responseHeaders = new ArrayList<NameValuePair>(response.getResponseHeaders()); for (int i = responseHeaders.size() - 1; i >= 0; i--) { if ("content-encoding".equalsIgnoreCase(responseHeaders.get(i).getName())) { responseHeaders.remove(i); } } final WebResponseData wrd = new WebResponseData( decompileScript.getBytes(), response.getStatusCode(), response.getStatusMessage(), responseHeaders); return new WebResponse( wrd, response.getWebRequest().getUrl(), response.getWebRequest().getHttpMethod(), response.getLoadTime()); } catch (final Exception e) { LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e); } return response; }
/** * Saves the response content in the temp dir and adds it to the summary page. * * @param response the response to save * @param request the request used to get the response * @throws IOException if a problem occurs writing the file */ protected void saveResponse(final WebResponse response, final WebRequest request) throws IOException { counter_++; final String extension = chooseExtension(response.getContentType()); final File f = createFile(request.getUrl(), extension); final InputStream input = response.getContentAsStream(); final OutputStream output = new FileOutputStream(f); int length = 0; try { length = IOUtils.copy(input, output); } catch (final EOFException e) { // ignore } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } final URL url = response.getWebRequest().getUrl(); LOG.info("Created file " + f.getAbsolutePath() + " for response " + counter_ + ": " + url); final StringBuilder buffer = new StringBuilder(); buffer.append("tab[tab.length] = {code: " + response.getStatusCode() + ", "); buffer.append("fileName: '" + f.getName() + "', "); buffer.append("contentType: '" + response.getContentType() + "', "); buffer.append("method: '" + request.getHttpMethod().name() + "', "); if (request.getHttpMethod() == HttpMethod.POST && request.getEncodingType() == FormEncodingType.URL_ENCODED) { buffer.append( "postParameters: " + nameValueListToJsMap(request.getRequestParameters()) + ", "); } buffer.append("url: '" + escapeJSString(url.toString()) + "', "); buffer.append("loadTime: " + response.getLoadTime() + ", "); buffer.append("responseSize: " + length + ", "); buffer.append("responseHeaders: " + nameValueListToJsMap(response.getResponseHeaders())); buffer.append("};\n"); appendToJSFile(buffer.toString()); }