private void ProcessResult(WebTest test, WebResponse response) { long pageLoadTime = response.getLoadTime(); int statusCode = response.getStatusCode(); String content = response.getContentAsString(); boolean responseContentValid = true; if (!test.getRespShouldContain().isEmpty()) { if (!content.contains(test.getRespShouldContain())) { responseContentValid = false; } } else if (!test.getRespShouldNotContain().isEmpty()) { if (content.contains(test.getRespShouldNotContain())) { responseContentValid = false; } } Orville.LOG() .info( String.format( "Page load status code: %d, page load time: %d ms, response valid: %s\n", statusCode, pageLoadTime, (responseContentValid == true) ? "true" : "false")); boolean statusCodeValid = (test.getRespHttpCode() != 0 ? statusCode == test.getRespHttpCode() : true); boolean testPassed = (statusCodeValid && responseContentValid) ? true : false; StringBuffer urlBuffer = new StringBuffer(); urlBuffer.append("http://orville-agent-management.herokuapp.com/test_results/"); // urlBuffer.append("http://localhost:3000/test_results/"); urlBuffer.append(test.getTestId() + "/"); urlBuffer.append(Orville.AgentGuid() + "/"); urlBuffer.append(testPassed + "/"); urlBuffer.append(pageLoadTime); // Need to make sure the URL doesn't have any spaces String url = urlBuffer.toString().replaceAll("\\s+", "%20"); Orville.LOG().info("Test result URL: " + url); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = null; try { httpPost = new HttpPost(url); HttpResponse postResponse = httpclient.execute(httpPost); Orville.LOG() .fine( String.format( "Test result data posted, response code: %d\n", postResponse.getStatusLine().getStatusCode())); } catch (IOException e) { Orville.LOG() .warning( String.format( "Unable to connect with Redstone test results service: %s\n", e.getMessage())); } }
/** * 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()); }