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();
}
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();
}
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);
}
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();
}
}
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();
}
}
Aggregations