private ResponseStream handleResponse(HttpResponse response) throws HttpException, IOException { if (response == null) { throw new HttpException("response is null"); } StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode < 300) { ResponseStream responseStream = new ResponseStream(response, charset, requestUrl, expiry); responseStream.setRequestMethod(requestMethod); return responseStream; } else if (statusCode == 301 || statusCode == 302) { if (httpRedirectHandler == null) { httpRedirectHandler = new DefaultHttpRedirectHandler(); } HttpRequestBase request = httpRedirectHandler.getDirectRequest(response); if (request != null) { return this.sendRequest(request); } } else if (statusCode == 416) { throw new HttpException(statusCode, "maybe the file has downloaded completely"); } else { throw new HttpException(statusCode, status.getReasonPhrase()); } return null; }
public void run(JstdTestCase testCase) { stopWatch.start("run %s", testCase.getId()); logger.info("run {}", testCase.getId()); String browserId = params.get("id"); try { stopWatch.start("checkBrowser %s", browserId); checkBrowser(); stopWatch.stop("checkBrowser %s", browserId); logger.debug("Starting upload for {}", browserId); // TODO(corysmith): Move the loading of files to a browser into the server. if (upload) { fileUploader.uploadToTheBrowser( browserId, stream, fileUploader.determineBrowserFileSet(browserId, testCase, stream), getBrowser(browserId).getUploadSize()); } logger.debug("Finished upload for {}", browserId); stopWatch.start("post %s", params); server.post(baseUrl + "/cmd", params); stopWatch.stop("post %s", params); } finally { } try { logger.debug("Starting {} for {}", params.get("data"), browserId); stopWatch.start("execution %s", params.get("data")); StreamMessage streamMessage = null; do { String response = server.fetch(baseUrl + "/cmd?id=" + browserId); try { streamMessage = gson.fromJson(response, StreamMessage.class); } catch (Exception e) { throw new RuntimeException("Error deserializing: " + response, e); } Response resObj = streamMessage.getResponse(); if (ResponseType.BROWSER_PANIC.equals(resObj.getResponseType())) { throw new BrowserPanicException(resObj.getBrowser(), resObj.getResponse()); } stream.stream(resObj); } while (!streamMessage.isLast()); logger.debug( "finished {} for {} with {}", new Object[] { params.get("data"), browserId, streamMessage.getResponse().getResponseType() }); } finally { stopWatch.stop("execution %s", params.get("data")); logger.debug("finished {} for {}", params.get("data"), browserId); stopWatch.stop("run %s", testCase.getId()); } }