AuthenticatedFilter extends RoboZonkyFilter { @Override public void filter(final ClientRequestContext clientRequestContext) { final char[] t = token == null ? AuthenticatedFilter.EMPTY_TOKEN : token.get() .getAccessToken(); this.setRequestHeader("Authorization", "Bearer " + String.valueOf(t)); super.filter(clientRequestContext); } AuthenticatedFilter(final Supplier<ZonkyApiToken> token); @Override void filter(final ClientRequestContext clientRequestContext);  }
@Test void hasToken() throws URISyntaxException { final ClientRequestContext crc = mockClientRequestContext(); getTestedFilter().filter(crc); verify(crc.getHeaders()).putSingle(eq("Authorization"), eq("Bearer " + String.valueOf(TOKEN.getAccessToken()))); }
@Test void changes400to401() throws IOException, URISyntaxException { final int expectedCode = 400; final ClientRequestContext ctx = mockClientRequestContext(); final ClientResponseContext ctx2 = mockClientResponseContext(); when(ctx2.hasEntity()).thenReturn(true); when(ctx2.getEntityStream()).thenReturn(c(TOKEN)); when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode)); when(ctx2.getStatus()).thenReturn(expectedCode); final RoboZonkyFilter filter = getTestedFilter(); filter.filter(ctx, ctx2); verify(ctx2, times(1)).setStatus(401); }