/** * Validate request fails (for bad certificates when server only supports clientAuthMandatory e.g. * GlassFish * * @param httpClientToTest * @param url */ private void validateRequestFails(HttpClient httpClientToTest, String url) throws Exception { HttpGet request = new HttpGet(url); logger.info("request:" + request); try { HttpResponse response = httpClientToTest.execute(request); StatusLine status = response.getStatusLine(); logger.error(status); BasicResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = responseHandler.handleResponse(response).trim(); final int xmlstart = responseBody.indexOf("<?xml"); if (xmlstart > 0) { responseBody = responseBody.substring(xmlstart); } logger.error("responseBody*>>"); logger.error(responseBody); logger.error("responseBody*<<"); Assert.fail("expected exception for bad certifiate:but got response"); } catch (SSLPeerUnverifiedException pue) { logger.debug("expected exception", pue); } }
private CloudServersException parseCloudServersException(HttpResponse response) { CloudServersException cse = new CloudServersException(); try { BasicResponseHandler responseHandler = new BasicResponseHandler(); String body = responseHandler.handleResponse(response); CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(parser); xmlReader.parse(new InputSource(new StringReader(body))); cse = parser.getException(); } catch (ClientProtocolException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (IOException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (ParserConfigurationException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (SAXException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (FactoryConfigurationError e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } return cse; }
/** * Do request and validate response * * @param httpClientToTest * @param url * @param expectedTitle */ private void validateResponseToRequest( HttpClient httpClientToTest, String url, String expectedTitle) throws Exception { HttpGet request = new HttpGet(url); HttpResponse response = httpClientToTest.execute(request); logger.info("request:" + request); StatusLine status = response.getStatusLine(); Assert.assertEquals("Status code:" + status, HttpStatus.SC_OK, status.getStatusCode()); logger.info(status); BasicResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = responseHandler.handleResponse(response).trim(); final int xmlstart = responseBody.indexOf("<?xml"); if (xmlstart > 0) { responseBody = responseBody.substring(xmlstart); } logger.debug("responseBody*>>"); logger.debug(responseBody); logger.debug("responseBody*<<"); Pattern titlePattern = Pattern.compile("(<title>)([^<]+)(</title>)"); Matcher matcher = titlePattern.matcher(responseBody); Assert.assertTrue("title element found", matcher.find()); String title = matcher.group(2); Assert.assertEquals("title", expectedTitle, title); }