GuestAuthInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { final Request request = chain.request(); final GuestSession session = guestSessionProvider.getCurrentSession(); final GuestAuthToken token = session == null ? null : session.getAuthToken(); if (token != null) { final Request.Builder builder = request.newBuilder(); addAuthHeaders(builder, token); return chain.proceed(builder.build()); } return chain.proceed(request); } GuestAuthInterceptor(GuestSessionProvider guestSessionProvider); @Override Response intercept(Chain chain);  }
@Test public void testIntercept() throws Exception { final Request request = new Request.Builder().url(TEST_URL).build(); when(mockChain.request()).thenReturn(request); interceptor.intercept(mockChain); verify(mockChain).proceed(requestCaptor.capture()); final Request signedRequest = requestCaptor.getValue(); assertEquals(TEST_HEADER_AUTHORIZATION, signedRequest.header(OAuthConstants.HEADER_AUTHORIZATION)); assertEquals(TEST_GUEST_TOKEN, signedRequest.header(OAuthConstants.HEADER_GUEST_TOKEN)); }
@Test public void testIntercept_nullSessionFromProvider() throws Exception { final Request request = new Request.Builder().url(TEST_URL).build(); when(mockChain.request()).thenReturn(request); when(mockGuestSessionProvider.getCurrentSession()).thenReturn(null); interceptor.intercept(mockChain); verify(mockChain).proceed(requestCaptor.capture()); final Request unsignedRequest = requestCaptor.getValue(); assertEquals(request, unsignedRequest); }