AccessTokensRequestInterceptor implements HttpRequestInterceptor { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { try { StringBuilder sb = new StringBuilder(); sb.append(BEARER); sb.append(accessTokens.get(tokenId)); request.setHeader(AUTHORIZATION_HEADER_NAME, sb.toString()); } catch (Exception e) { throw new HttpException("Unable to place header 'access_token' into request.", e); } } AccessTokensRequestInterceptor(final String tokenId, final AccessTokens accessTokens); @Override void process(final HttpRequest request, final HttpContext context);  }
@Test public void testRequestInterceptor() throws HttpException, IOException { AccessTokens accessTokens = Mockito.mock(AccessTokens.class); Mockito.when(accessTokens.get(Mockito.any())).thenReturn("TEST_TOKEN"); AccessTokensRequestInterceptor interceptor = new AccessTokensRequestInterceptor("kio", accessTokens); BasicHttpRequest request = new BasicHttpRequest("GET", "http: interceptor.process(request, httpContext); Assertions.assertThat(request.getHeaders(AUTHORIZATION)).isNotEmpty(); Assertions.assertThat(request.getHeaders(AUTHORIZATION)[0].getValue()).isEqualTo("Bearer TEST_TOKEN"); Mockito.verify(accessTokens, Mockito.atMost(1)).get(Mockito.any()); }
@Test(expected = HttpException.class) public void testRequestInterceptorThrowsHttpException() throws HttpException, IOException { AccessTokens accessTokens = Mockito.mock(AccessTokens.class); Mockito.when(accessTokens.get(Mockito.any())).thenThrow(new RuntimeException("THROWN_BY_MOCK")); AccessTokensRequestInterceptor interceptor = new AccessTokensRequestInterceptor("kio", accessTokens); BasicHttpRequest request = new BasicHttpRequest("GET", "http: interceptor.process(request, httpContext); }