Files
plexamp-lite/test/NetTest.java
T
2026-10-14 19:38:37 +02:00

58 lines
2.3 KiB
Java

import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.minamp.net.CertVerifier;
import com.minamp.net.Http;
/** Desktop-JVM test of the bundled TLS + HTTP client against real servers. */
public class NetTest {
public static void main(String[] args) throws Exception {
final File dir = new File(args[0]);
CertVerifier.setRootStore(new CertVerifier.RootStore() {
public List<byte[]> find(byte[] subjectDer) {
String h = CertVerifier.sha1Hex(subjectDer);
List<byte[]> out = new ArrayList<byte[]>();
for (int i = 0; ; i++) {
File f = new File(dir, h + "-" + i + ".der");
if (!f.exists()) break;
try { out.add(read(f)); } catch (Exception e) { break; }
}
return out.isEmpty() ? null : out;
}
});
for (int i = 1; i < args.length; i++) {
String url = args[i];
long t0 = System.currentTimeMillis();
try {
Map<String, String> h = new HashMap<String, String>();
h.put("Accept", "application/json");
Http.Response r = Http.get(url, h, 15000);
byte[] body = r.readAll();
long t1 = System.currentTimeMillis();
// second request should reuse the pooled connection
Http.Response r2 = Http.get(url, h, 15000);
byte[] body2 = r2.readAll();
long t2 = System.currentTimeMillis();
System.out.println("OK " + url + " -> " + r.code + " " + body.length + "B chunked=" + r.chunked
+ " first=" + (t1 - t0) + "ms reuse=" + (t2 - t1) + "ms (" + body2.length + "B)");
} catch (Exception e) {
System.out.println("ERR " + url + " -> " + e);
}
}
}
static byte[] read(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream b = new ByteArrayOutputStream();
byte[] buf = new byte[4096]; int n;
while ((n = in.read(buf)) > 0) b.write(buf, 0, n);
in.close();
return b.toByteArray();
}
}