/** * Validates and read the response. * * @param url the URL where the response was received from. * @param statusCode the HTTP status code. * @param response the raw response of fetching the given URL. * @return the response, as {@link String}. * * @throws RobotConnectionException if the response code is not HTTP OK (200). */ public static String validateAndReadResponse(String url, int statusCode, InputStream response) throws RobotConnectionException { try { return validateAndReadResponse(url, statusCode, ByteStreams.toByteArray(response)); } catch (IOException e) { String msg = "Robot fetch http failure: " + url + "."; throw new RobotConnectionException(msg, statusCode); } } }
public void testValidateAndReadResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 200; String response = "{'foo':'bar'}"; assertEquals(response, RobotConnectionUtil.validateAndReadResponse(url, statusCode, response.getBytes(Charsets.UTF_8))); }
public void testValidateAndReadResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 200; String response = "{'foo':'bar'}"; assertEquals(response, RobotConnectionUtil.validateAndReadResponse(url, statusCode, response.getBytes(Charsets.UTF_8))); }
/** * Fetches the given URL, given a method ({@code GET} or {@code POST}). * * @param url the URL to be fetched. * @param method the method to fetch the URL, can be {@code GET} or * {@code POST}. * @return the content of the URL. * * @throws RobotConnectionException if there is a problem fetching the URL, * for example, if the response code is not HTTP OK (200). */ private String fetch(String url, HttpMethod method) throws RobotConnectionException { try { int statusCode = httpClient.executeMethod(method); return RobotConnectionUtil.validateAndReadResponse(url, statusCode, method.getResponseBodyAsStream()); } catch (IOException e) { String msg = "Robot fetch http failure: " + url + "."; throw new RobotConnectionException(msg, e); } finally { method.releaseConnection(); } } }
public void testValidateAndReadResponseOnNonOkResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 404; try { RobotConnectionUtil.validateAndReadResponse(url, statusCode, (byte[]) null); } catch (RobotConnectionException e) { assertEquals(statusCode, e.getStatusCode()); assertTrue(e.getMessage().contains(url)); } } }
public void testValidateAndReadResponseOnNonOkResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 404; try { RobotConnectionUtil.validateAndReadResponse(url, statusCode, (byte[]) null); } catch (RobotConnectionException e) { assertEquals(statusCode, e.getStatusCode()); assertTrue(e.getMessage().contains(url)); } } }