Search in sources :

Example 1 with NanoHTTPD

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

the class AetherRepsitoryTests method setUp.

@Override
protected void setUp() throws Exception {
    httpd = new NanoHTTPD(0, IO.getFile("testdata/repo"));
    httpdPort = httpd.getPort();
}
Also used : NanoHTTPD(test.lib.NanoHTTPD)

Example 2 with NanoHTTPD

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

the class TestLocalIndexedRepo method setUp.

protected void setUp() throws Exception {
    // Ensure output directory exists and is empty
    outputDir = IO.getFile("generated/tmp/test/" + getName());
    IO.deleteWithException(outputDir);
    if (!outputDir.exists() && !outputDir.mkdirs()) {
        throw new IOException("Could not create directory " + outputDir);
    }
    httpd = new NanoHTTPD(0, new File("testdata"));
    httpdPort = httpd.getPort();
}
Also used : NanoHTTPD(test.lib.NanoHTTPD) IOException(java.io.IOException) File(java.io.File)

Example 3 with NanoHTTPD

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

the class HttpRedirectionTest method testFollowRedirect.

public void testFollowRedirect() throws Exception {
    Reporter reporter = mock(Reporter.class);
    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(NanoHTTPD.HTTP_OK, "text/plain", "got it");
            } else {
                r = new Response(NanoHTTPD.HTTP_BADREQUEST, "text/plain", "sod off");
            }
            return r;
        }
    };
    String baseUrl = "http://localhost:" + httpd.getPort() + "/";
    String originalUrl = baseUrl + "foo";
    String redirectUrl = baseUrl + "bar";
    DefaultURLConnector connector = new DefaultURLConnector();
    connector.setReporter(reporter);
    InputStream stream = connector.connect(new URL(originalUrl));
    String result = IO.collect(stream);
    assertEquals("got it", result);
    verify(reporter).warning("HTTP address redirected from %s to %s", originalUrl, redirectUrl);
}
Also used : InputStream(java.io.InputStream) Reporter(aQute.service.reporter.Reporter) NanoHTTPD(test.lib.NanoHTTPD) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL)

Example 4 with NanoHTTPD

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

the class CachingUriResourceHandlerTest method testLoadFromRemote.

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

Example 5 with NanoHTTPD

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

the class CachingUriResourceHandlerTest method testReplaceCache.

public static void testReplaceCache() throws Exception {
    File cached = IO.getFile("testdata/httpcache/5/http%3A%2F%2Flocalhost%3A18083%2Fbundles/dummybundle.jar");
    long cacheTimestamp = cached.lastModified();
    // Clear the SHA so the file appears modified
    File shaFile = new File(cached.getAbsolutePath() + AbstractIndexedRepo.REPO_INDEX_SHA_EXTENSION);
    IO.store("00000000", shaFile);
    CachingUriResourceHandle handle = new CachingUriResourceHandle(new URI("http://localhost:18083/bundles/dummybundle.jar"), IO.getFile("testdata/httpcache/5"), new DefaultURLConnector(), EXPECTED_SHA);
    NanoHTTPD httpd = new NanoHTTPD(18083, IO.getFile("testdata/http"));
    try {
        File result = handle.request();
        assertEquals(cached, result);
        assertNotSame("File timestamp SHOULD change", cacheTimestamp, result.lastModified());
        assertEquals(EXPECTED_SHA, IO.collect(shaFile));
    } 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