HttpDaemon { public static void registerServlet(String urlPattern, HttpServlet servlet) { sServletMap.put(urlPattern, servlet); } HttpDaemon(int port);  HttpDaemon(); static void registerServlet(String urlPattern, HttpServlet servlet); static int getPort(); static HttpDaemon getSingleton(); void start(); void stop(); boolean isAlive(); boolean isStarted(); int aliveRequests(); static int sPort; }
@Test public void testGet() throws Exception { final String greet = "Hello World"; HttpDaemon.registerServlet("/", new HttpServlet() { @Override public HttpResponse doGet(HttpRequest request) { return HttpResponse.newResponse(greet); } @Override public HttpResponse doPost(HttpRequest request) { return null; } }); Request request = new Request.Builder().url(url).build(); Response response = new OkHttpClient().newCall(request).execute(); assertThat(response.body().string(), is(greet)); }
@Test public void testFormFileProgress() throws Exception { final List<Integer> progressList = new ArrayList<>(); HttpDaemon.registerServlet("/", new HttpServlet() { @Override public HttpResponse doGet(HttpRequest request) { return null; } @Override public HttpResponse doPost(HttpRequest request) { ProgressListener listener = new ProgressListener() { private int mProgress; @Override public void update(String fileUri, long pBytesRead, long pContentLength) { int progress = (int) (pBytesRead * 100.0 / pContentLength); if (progress > mProgress) { mProgress = progress; progressList.add(mProgress); } } }; request.parseMultipartBody(repository, listener); return HttpResponse.newResponse(request.form().get(FILE_URI)); } }); Request request = formWithFileAndUriRequest(); Response response = new OkHttpClient().newCall(request).execute(); System.out.println(progressList); assertThat(progressList, hasItem(100)); }
@Test public void testFormWithFileAndUri() throws Exception { HttpDaemon.registerServlet("/", new HttpServlet() { @Override public HttpResponse doGet(HttpRequest request) { return null; } @Override public HttpResponse doPost(HttpRequest request) { request.parseMultipartBody(repository, null); String title = request.form().get("fileUri"); return HttpResponse.newResponse(title); } }); Request request = formWithFileAndUriRequest(); Response response = new OkHttpClient().newCall(request).execute(); String uri = response.body().string(); boolean same = Files.equal(fileCopy, file); assertThat(same, is(true)); assertThat(uri, is(file.getAbsolutePath())); }
@Test public void testRawData() throws Exception { final String jsonPayload = "{\"name\":\"yieldnull\"}"; HttpDaemon.registerServlet("/", new HttpServlet() { @Override public HttpResponse doGet(HttpRequest request) { return null; } @Override public HttpResponse doPost(HttpRequest request) { return HttpResponse.newResponse(request.text()); } }); RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonPayload); Request request = new Request.Builder() .url(url) .post(body) .addHeader("Connection", "close") .build(); System.setProperty("http.keepAlive", "false"); Response response = new OkHttpClient().newCall(request).execute(); String json = response.body().string(); assertThat(json, is(jsonPayload)); }