sslcontext.init(km, tm, null); LocalTestServer localServer = new LocalTestServer(sslcontext); localServer.registerDefaultHandlers(); httpclient.getConnectionManager().getSchemeRegistry().register(https); InetSocketAddress address = localServer.getServiceAddress(); HttpHost target1 = new HttpHost(address.getHostName(), address.getPort(), "https"); HttpGet httpget1 = new HttpGet("/random/100"); EntityUtils.consume(entity2); } finally { localServer.stop();
public static void main(String[] args) throws Exception { LocalTestServer server = new LocalTestServer(null, null); try { server.start(); server.register("/*", new LoggingHandler()); server.awaitTermination(3600 * 1000); } catch (Exception e) { e.printStackTrace(); } finally { server.stop(); } }
public void register(String method, JSONObject response) { server.register( "/api/" + method, TestUtils.makeHttpHandler(HttpStatus.SC_OK, response.toString(2), requestBodies) ); } }
@Before public void setUp() throws Exception { server = new LocalTestServer(null, null); server.start(); }
public void stop() throws Exception { server.stop(); }
@Test public void testSuccess() { StringBuilder expectedStr = new StringBuilder(); Map<String, TLE> expected; try (BufferedReader r = new BufferedReader(new InputStreamReader(CelestrakClientTest.class.getClassLoader().getResourceAsStream("sample-tle.txt")))) { String curLine = null; List<String> lines = new ArrayList<>(); while( (curLine = r.readLine()) != null ) { lines.add(curLine); expectedStr.append(curLine).append("\n"); } expected = convert(lines); } catch (Exception e) { throw new RuntimeException(e); } server.register("/*", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(new StringEntity(expectedStr.toString(), StandardCharsets.UTF_8)); } }); // one slash is important here CelestrakClient client = new CelestrakClient("http:/" + server.getServiceAddress()); Map<String, TLE> actual = client.getWeatherTLE(); assertEquals(expected.size(), actual.size()); assertTrue(expected.equals(actual)); }
private String getTestServerAddress() { return String.format( "http://%s:%s", server.getServiceAddress().getHostName(), server.getServiceAddress().getPort() ); } }
@Before public void start() throws Exception { server.start(); }
@Test public void testGetParentCoverageNull() { server.register("/*", TestUtils.makeHttpHandler(HttpStatus.SC_OK, "null")); assertNull(client.getParentCoverage(TestUtils.TEST_SHA)); }
public FakeConduit(Map<String, JSONObject> responses) throws Exception { server = new LocalTestServer(null, null); this.requestBodies = new ArrayList<String>(); for (Map.Entry<String, JSONObject> entry : responses.entrySet()) { register(entry.getKey(), entry.getValue()); } server.start(); }
@After public void tearDown() throws Exception { server.stop(); }
public static String getTestServerAddress(LocalTestServer server) { return String.format( "http://%s:%s", server.getServiceAddress().getHostName(), server.getServiceAddress().getPort() ); }
@Test public void testGetCoverageInternalError() { server.register("/*", TestUtils.makeHttpHandler(HttpStatus.SC_INTERNAL_SERVER_ERROR, "")); assertNull(client.getCoverage(TestUtils.TEST_SHA)); }
@Before public void setUp() throws Exception { server = new LocalTestServer(null, null); server.start(); client = getDefaultClient(); }
@After public void tearDown() throws Exception { server.stop(); }
@Test public void testGetCoverageMissingKey() { server.register("/*", TestUtils.makeHttpHandler(HttpStatus.SC_OK, "{}")); assertNull(client.getParentCoverage(TestUtils.TEST_SHA)); }
@After public void stop() throws Exception { server.stop(); }
@Test public void testGetCoverageFound() { server.register("/*", TestUtils.makeHttpHandler(HttpStatus.SC_OK, "{}")); String coverage = client.getCoverage(TestUtils.TEST_SHA); assertEquals("{}", coverage); }
@Test public void testSuccessfullFetch() throws Exception { server.register("/api/valid", TestUtils.makeHttpHandler(HttpStatus.SC_OK, "{\"hello\": \"world\"}")); client = new ConduitAPIClient(getTestServerAddress(), TestUtils.TEST_CONDUIT_TOKEN); JSONObject response = client.perform("valid", emptyParams); assertEquals("world", response.getString("hello")); }
@Test(expected = ConduitAPIException.class) public void testBadRequestErrorCode() throws Exception { server.register("/api/foo", TestUtils.makeHttpHandler(HttpStatus.SC_BAD_REQUEST, "nothing")); client = new ConduitAPIClient(getTestServerAddress(), TestUtils.TEST_CONDUIT_TOKEN); client.perform("foo", emptyParams); }