Search in sources :

Example 6 with NanoHTTPD

use of test.lib.NanoHTTPD in project bnd by bndtools.

the class CachingUriResourceHandlerTest method testEmptyCache.

public static void testEmptyCache() throws Exception {
    String testDirName = "testdata/httpcache/6";
    File cacheDir = new File(testDirName);
    URI baseUri = new URI("http://localhost:18083/bundles");
    String jarName = "dummybundle.jar";
    URI uri = new URI(baseUri + "/" + jarName);
    String uriCacheDir = URLEncoder.encode(baseUri.toURL().toExternalForm(), "UTF-8");
    File cached = IO.getFile(testDirName + "/" + uriCacheDir + "/" + jarName);
    cached.delete();
    File shaFile = new File(cached.getAbsolutePath() + AbstractIndexedRepo.REPO_INDEX_SHA_EXTENSION);
    shaFile.delete();
    CachingUriResourceHandle handle = new CachingUriResourceHandle(uri, cacheDir, new DefaultURLConnector(), EXPECTED_SHA);
    NanoHTTPD httpd = new NanoHTTPD(18083, IO.getFile("testdata/http"));
    try {
        File result = handle.request();
        assertEquals(cached, result.getAbsoluteFile());
        assertEquals(EXPECTED_SHA, IO.collect(shaFile));
        /* cleanup */
        List<String> cacheFiles = Arrays.asList(cacheDir.list());
        assert (cacheFiles.size() == 1 || cacheFiles.size() == 2);
        assert (cacheFiles.contains(uriCacheDir));
        if (cacheFiles.size() == 2) {
            assert (cacheFiles.contains(".gitignore"));
        }
        IO.delete(IO.getFile(testDirName + "/" + uriCacheDir));
    } finally {
        httpd.stop();
    }
}
Also used : NanoHTTPD(test.lib.NanoHTTPD) File(java.io.File) URI(java.net.URI) DefaultURLConnector(aQute.bnd.deployer.http.DefaultURLConnector)

Example 7 with NanoHTTPD

use of test.lib.NanoHTTPD in project bnd by bndtools.

the class TestCompressedObrRepo method setUp.

@Override
protected void setUp() throws Exception {
    httpd = new NanoHTTPD(0, IO.getFile("testdata/http"));
    httpdPort = httpd.getPort();
    Sed.file2GzFile(obrSrc, "__httpdPort__", Integer.toString(httpdPort), obrDst);
    reporter = new Processor();
    obr = new FixedIndexedRepo();
    Map<String, String> config = new HashMap<String, String>();
    config.put("name", "obr");
    config.put("locations", new File(obrDst).toURI().toString());
    config.put("type", "OBR");
    obr.setProperties(config);
    obr.setReporter(reporter);
    File tmpFile = File.createTempFile("cache", ".tmp");
    tmpFile.deleteOnExit();
    obr.setCacheDirectory(new File(tmpFile.getAbsolutePath() + ".dir"));
}
Also used : Processor(aQute.bnd.osgi.Processor) HashMap(java.util.HashMap) NanoHTTPD(test.lib.NanoHTTPD) File(java.io.File)

Example 8 with NanoHTTPD

use of test.lib.NanoHTTPD in project bnd by bndtools.

the class HttpRedirectionTest method testDetectRedirectLoop.

public void testDetectRedirectLoop() throws Exception {
    final NanoHTTPD httpd = new NanoHTTPD(0, new File(".")) {

        @Override
        public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
            Response r;
            if ("/foo".equals(uri)) {
                r = new Response("302 Found", "text/plain", "over there");
                r.header.put("Location", "/bar");
            } else if ("/bar".equals(uri)) {
                r = new Response("302 Found", "text/plain", "go back");
                r.header.put("Location", "/foo");
            } else {
                r = new Response(NanoHTTPD.HTTP_BADREQUEST, "text/plain", "sod off");
            }
            return r;
        }
    };
    // Use a future to ensure we timeout after 1s if the redirect does
    // actually loop forever
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<?> future = executor.submit(new Runnable() {

        public void run() {
            DefaultURLConnector connector = new DefaultURLConnector();
            try {
                InputStream stream = connector.connect(new URL("http://localhost:" + httpd.getPort() + "/foo"));
                IO.collect(stream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    try {
        future.get(1, TimeUnit.SECONDS);
    } finally {
        future.cancel(true);
        executor.shutdownNow();
    }
}
Also used : InputStream(java.io.InputStream) ExecutorService(java.util.concurrent.ExecutorService) NanoHTTPD(test.lib.NanoHTTPD) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL)

Example 9 with NanoHTTPD

use of test.lib.NanoHTTPD in project bnd by bndtools.

the class OBRTest method setUp.

@Override
protected void setUp() throws Exception {
    httpd = new NanoHTTPD(0, IO.getFile("testdata/http"));
    httpdPort = httpd.getPort();
    Sed.file2File(obrSrc, "__httpdPort__", Integer.toString(httpdPort), obrDst);
    obr = new OBR();
    Map<String, String> config = new HashMap<String, String>();
    config.put("location", new File(obrDst).getAbsoluteFile().toURI().toString());
    File tmpFile = File.createTempFile("cache", ".tmp");
    tmpFile.deleteOnExit();
    File cacheDir = new File(tmpFile.getAbsolutePath() + ".dir");
    obr.setProperties(config);
    obr.setCacheDirectory(cacheDir);
    reporter = new Processor();
    obr.setReporter(reporter);
}
Also used : Processor(aQute.bnd.osgi.Processor) HashMap(java.util.HashMap) NanoHTTPD(test.lib.NanoHTTPD) File(java.io.File)

Example 10 with NanoHTTPD

use of test.lib.NanoHTTPD in project bnd by bndtools.

the class CachingUriResourceHandlerTest method testUseCached.

public static void testUseCached() throws Exception {
    File cached = IO.getFile("testdata/httpcache/4/http%3A%2F%2Flocalhost%3A18083%2Fbundles/dummybundle.jar");
    long cacheTimestamp = cached.lastModified();
    CachingUriResourceHandle handle = new CachingUriResourceHandle(new URI("http://localhost:18083/bundles/dummybundle.jar"), IO.getFile("testdata/httpcache/4"), new DefaultURLConnector(), EXPECTED_SHA);
    NanoHTTPD httpd = new NanoHTTPD(18083, IO.getFile("testdata/http"));
    try {
        File result = handle.request();
        assertEquals(cached, result);
        assertEquals("File timestamp should NOT change", cacheTimestamp, result.lastModified());
    } finally {
        httpd.stop();
    }
}
Also used : NanoHTTPD(test.lib.NanoHTTPD) File(java.io.File) URI(java.net.URI) DefaultURLConnector(aQute.bnd.deployer.http.DefaultURLConnector)

Aggregations

NanoHTTPD (test.lib.NanoHTTPD)11 File (java.io.File)10 DefaultURLConnector (aQute.bnd.deployer.http.DefaultURLConnector)4 URI (java.net.URI)4 Processor (aQute.bnd.osgi.Processor)3 HashMap (java.util.HashMap)3 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Properties (java.util.Properties)2 Reporter (aQute.service.reporter.Reporter)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1