TraceTokenRequestFilter implements HttpRequestFilter { @Override public Request filterRequest(Request request) { requireNonNull(request, "request is null"); TraceToken token = getCurrentTraceToken(); if (token == null) { return request; } String tokenString; if (token.size() == 1) { tokenString = token.toString(); } else { tokenString = TRACE_TOKEN_JSON_CODEC.toJson(token); } return fromRequest(request) .addHeader(TRACETOKEN_HEADER, tokenString) .build(); }  @Override Request filterRequest(Request request); @Override boolean equals(Object obj); @Override int hashCode(); static final String TRACETOKEN_HEADER; }
@Test public void testBasic() { registerRequestToken("testBasic"); TraceTokenRequestFilter filter = new TraceTokenRequestFilter(); Request original = prepareGet().setUri(URI.create("http: Request filtered = filter.filterRequest(original); assertNotSame(filter, original); assertEquals(filtered.getUri(), original.getUri()); assertEquals(original.getHeaders().size(), 0); assertEquals(filtered.getHeaders().size(), 1); assertEquals(filtered.getHeaders().get(TRACETOKEN_HEADER), ImmutableList.of("testBasic")); }
@Test public void testWithProperties() { registerRequestToken("testBasic"); addTraceTokenProperties("key-b", "value-b", "key-a", "value-a", "key-c", "value-c"); TraceTokenRequestFilter filter = new TraceTokenRequestFilter(); Request original = prepareGet().setUri(URI.create("http: Request filtered = filter.filterRequest(original); assertNotSame(filter, original); assertEquals(filtered.getUri(), original.getUri()); assertEquals(original.getHeaders().size(), 0); assertEquals(filtered.getHeaders().size(), 1); assertEquals(filtered.getHeaders().get(TRACETOKEN_HEADER), ImmutableList.of("{\"id\":\"testBasic\",\"key-b\":\"value-b\",\"key-a\":\"value-a\",\"key-c\":\"value-c\"}")); }
@Test public void testIgnoresLocalProperties() { registerRequestToken("testBasic"); addTraceTokenProperties("key-b", "value-b", "_local-1", "value-1", "key-a", "value-a"); TraceTokenRequestFilter filter = new TraceTokenRequestFilter(); Request original = prepareGet().setUri(URI.create("http: Request filtered = filter.filterRequest(original); assertNotSame(filter, original); assertEquals(filtered.getUri(), original.getUri()); assertEquals(original.getHeaders().size(), 0); assertEquals(filtered.getHeaders().size(), 1); assertEquals(filtered.getHeaders().get(TRACETOKEN_HEADER), ImmutableList.of("{\"id\":\"testBasic\",\"key-b\":\"value-b\",\"key-a\":\"value-a\"}")); }
@Test public void testSameRequestReturnedWhenTraceTokenNotSet() { TraceTokenRequestFilter filter = new TraceTokenRequestFilter(); clearRequestToken(); Request original = prepareGet().setUri(URI.create("http: Request request = filter.filterRequest(original); assertSame(request, original); }