OkRemote implements Remote { @Override public synchronized ListenableFuture<Integer> post(String path, String shardKey, Map jsonObj) { if (closed) { log.warn("Calling post after shutdown. Call will be ignored."); return Futures.immediateCancelledFuture(); } if ((path.length() > 0) && (path.charAt(0) != '/')) { path = "/" + path; } final String jsonStr; try { jsonStr = mapper.writeValueAsString(jsonObj); } catch (JsonProcessingException e) { return Futures.immediateFailedFuture(new RuntimeException("Invalid json input")); } final String url = "http: final RequestBody body = RequestBody.create(JSON, jsonStr); final Request request = new Request.Builder() .url(url) .addHeader(Sharder.SHARD_KEY, shardKey) .addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE) .post(body) .build(); final SettableFuture<Integer> result = SettableFuture.create(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { result.set(503); } @Override public void onResponse(Call call, Response response) throws IOException { result.set(response.code()); response.close(); } }); return result; } OkRemote(String host, int port); @Override synchronized ListenableFuture<Integer> post(String path, String shardKey, Map jsonObj); @Override boolean shutdown(long timeout, TimeUnit timeUnit);  }
@Test public void httpTest() throws InterruptedException, ExecutionException { server.enqueue(new MockResponse().setBody("hia")); ListenableFuture<Integer> result = remote.post("foo", "foo", ImmutableMap.of("aaa", "bbb")); RecordedRequest r = server.takeRequest(); assertEquals("{\"aaa\":\"bbb\"}", r.getBody().readUtf8()); assertEquals(1, server.getRequestCount()); assertEquals("/foo", r.getPath()); assertEquals((Integer) 200, result.get()); }
@Test public void shardTest() throws InterruptedException, ExecutionException { server.enqueue(new MockResponse().setBody("hia")); ListenableFuture<Integer> result = remote.post("foo", "shard-key", ImmutableMap.of()); RecordedRequest r = server.takeRequest(); assertEquals("shard-key", r.getHeader("X-Shard-Key")); }