@BeforeClass public static void setupCorrect() throws HTTPException { HTTPSession sess = HTTPFactory.newSession(server); session = sess; }
static public String getUrlAsString(String url) throws HTTPException { try ( HTTPSession session = HTTPFactory.newSession(url); HTTPMethod m = HTTPFactory.Get(session);) { int status = m.execute(); String content = null; if(status == 200) { content = m.getResponseAsString(); } return content; } }
@Override public double[] readCoordValues(CoverageCoordAxis coordAxis) throws IOException { if (httpClient == null) httpClient = HTTPFactory.newSession(endpoint); // LOOK is this ok? no authentication...
public HTTPMethod(HTTPSession.Methods m, HTTPSession session, String url) throws HTTPException { if(url == null && session != null) url = session.getURL(); try { new URL(url); } catch (MalformedURLException mue) { throw new HTTPException("Malformed URL: " + url, mue); } if(session == null) { session = HTTPFactory.newSession(url); localsession = true; } this.session = session; url = HTTPSession.removeprincipal(url); this.legalurl = url; this.session.addMethod(this); this.methodclass = m; }
public static FeatureType isCdmrfEndpoint(String endpoint) throws IOException { HTTPSession httpClient = HTTPFactory.newSession(endpoint); String url = endpoint + "?req=featureType"; // get the header try (HTTPMethod method = HTTPFactory.Get(httpClient, url)) { method.setFollowRedirects(true); int statusCode = method.execute(); if (statusCode != 200) return null; String content = method.getResponseAsString(); return FeatureType.getType(content); } catch (Throwable t) { t.printStackTrace(); return null; } }
@Test public void testSSH() throws Exception { String version = System.getProperty("java.version"); Assume.assumeTrue("Version must be 1.8 (temporary), not: " + version, version.startsWith("1.8")); System.out.println("*** Testing: Simple Https"); // Reset the ssl stores HTTPSession.clearkeystore(); for(String url : sshurls) { System.out.println("*** URL: " + url); try (HTTPSession session = HTTPFactory.newSession(url)) { this.result = invoke(session, url); Assert.assertTrue("Incorrect return code: " + this.result.status, this.result.status == 200); } } }
@Test public void testThreading1() throws HTTPException { Runner[] runners = new Runner[this.nthreads]; try (HTTPSession session = HTTPFactory.newSession(DFALTSERVER)) { // Set some timeouts //session.setConnectionTimeout(10000); //session.setSOTimeout(10000); for(int i = 0; i < this.nthreads; i++) { if(DEBUG) System.err.printf("[%d]: %s%n", i, testurls[i]); runners[i] = new Runner(session, testurls[i], i); } for(int i = 0; i < this.nthreads; i++) { Runner runner = runners[i]; runner.run(); } } System.err.println("All threads terminated"); HTTPSession.validatestate(); }
@Test public void testString() throws URISyntaxException, IOException { String catalogName = "http://localhost:8081/thredds/catalog.xml"; URI catalogURI = new URI(catalogName); try (HTTPSession client = HTTPFactory.newSession(catalogName)) { HTTPMethod m = HTTPFactory.Get(client,catalogName); int statusCode = m.execute(); logger.debug("status = {}", statusCode); String catAsString = m.getResponseAsString(CDM.UTF8); logger.debug("cat = {}", catAsString); CatalogBuilder builder = new CatalogBuilder(); Catalog cat = builder.buildFromString(catAsString, catalogURI); CatalogXmlWriter writer = new CatalogXmlWriter(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writer.writeXML(cat, baos, false); logger.debug(baos.toString()); } } } }
@Test public void testFailBadUser() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("baadss", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void writeGridAsPointNetcdf() throws JDOMException, IOException { String endpoint = TestOnLocalServer.withHttpPath(ds+"?var="+varName+query+"&accept=netcdf"); File tempFile = tempFolder.newFile(); logger.debug(" write {} to {}", endpoint, tempFile.getAbsolutePath()); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); if (statusCode != 200) { logger.debug("statusCode = {} '{}'", statusCode, method.getResponseAsString()); return; } IO.appendToFile(method.getResponseAsStream(), tempFile.getAbsolutePath()); } logger.debug(" file length {} bytes exists={} {}", tempFile.length(), tempFile.exists(), tempFile.getAbsolutePath()); // Open the result file as Station feature dataset Formatter errlog = new Formatter(); try (FeatureDataset fd = FeatureDatasetFactoryManager.open(FeatureType.STATION, tempFile.getAbsolutePath(), null, errlog)) { assertNotNull(errlog.toString(), fd); VariableSimpleIF v = fd.getDataVariable(varName); assertNotNull(varName, v); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testSuccess() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tds", "secret666")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertEquals(200, statusCode); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath("/dodsC/testRestrictedDataset/testData2.nc.dds"); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue("Expected HttpStatus.SC_UNAUTHORIZED|HttpStatus.SC_FORBIDDEN", statusCode== HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } } }
@Test public void testStream1() throws URISyntaxException, IOException { String catalogName = TestOnLocalServer.withHttpPath("catalog.xml"); URI catalogURI = new URI(catalogName); try (HTTPSession client = HTTPFactory.newSession(catalogName)) { HTTPMethod m = HTTPFactory.Get(client,catalogName); int statusCode = m.execute(); logger.debug("status = {}", statusCode); InputStream stream = m.getResponseBodyAsStream(); CatalogBuilder builder = new CatalogBuilder(); Catalog cat = builder.buildFromStream(stream, catalogURI); CatalogXmlWriter writer = new CatalogXmlWriter(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writer.writeXML(cat, baos, false); logger.debug(baos.toString()); } } }
@Test public void testFailBadPassword() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tiggeUser", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); //if (statusCode != HttpStatus.SC_UNAUTHORIZED && statusCode != HttpStatus.SC_FORBIDDEN) // assert false; Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testIllegalRequest() throws IOException, InvalidRangeException { String url = TestOnLocalServer.withHttpPath("/dodsC/scanLocal/testWrite.nc.dds?"); String esc = url + URLEncoder.encode("<bad>\nworse", CDM.UTF8); try (HTTPSession session = HTTPFactory.newSession(esc)) { HTTPMethod method = HTTPFactory.Get(session); method.execute(); Assert.assertEquals(400, method.getStatusCode()); } } }
public static void saveContentToFile(String endpoint, int expectCode, ContentType expectContentType, File saveTo) { logger.debug("req = '{}'", endpoint); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); if (statusCode != 200) { logger.warn("statusCode = {} '{}'", statusCode, method.getResponseAsString()); Assert.assertEquals(expectCode, statusCode); return; } Assert.assertEquals(expectCode, statusCode); if (expectContentType != null) { Header header = method.getResponseHeader(ContentType.HEADER); Assert.assertEquals(expectContentType.getContentHeader(), header.getValue()); } InputStream content = method.getResponseAsStream(); // closing method may close stream ?? IO.appendToFile(content, saveTo.getAbsolutePath()); } catch (Exception e) { logger.error("Problem with HTTP request", e); assert false; } } }
@Test public void testInline() throws Exception { logger.info("*** Testing: Http Basic Password Authorization inline in URL"); for(AuthDataBasic data : basictests) { Result result = null; logger.info("*** URL: " + data.inline()); try (HTTPSession session = HTTPFactory.newSession(data.url)) { result = invoke(session, data.inline()); report(result); } Assert.assertTrue("Incorrect return code: " + result.status, check(result.status)); // non-existence is ok Assert.assertTrue("no content", result.contents.length > 0); } }
@Test public void readCapabilities() { String endpoint = TestOnLocalServer.withHttpPath(path + "?" + query); System.out.printf("GetCapabilities req = '%s'%n", endpoint); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertEquals(200, statusCode); byte[] content = method.getResponseAsBytes(); assert content.length > 1000; System.out.printf("content='%s'%n", new String(content, "UTF-8")); ByteArrayInputStream bin = new ByteArrayInputStream(content); try { SAXBuilder builder = new SAXBuilder(); org.jdom2.Document tdoc = builder.build(bin); org.jdom2.Element root = tdoc.getRootElement(); } catch (Throwable t) { // if fail, go find where it barfs isValidUTF8(content); assert false; } } catch (Exception e) { e.printStackTrace(); assert false; } }
@Test public void testClosing1() throws HTTPException { try (HTTPSession s = HTTPFactory.newSession(CLOSEFILE)) { for(int i = 0; i < 500; i++) { HTTPMethod m = HTTPFactory.Head(s); int statusCode = m.execute(); Assert.assertTrue("Unexpected return code: " + statusCode, statusCode == 200); m.close(); } } }