ApiInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request.Builder requestBuilder = chain.request().newBuilder(); requestBuilder.addHeader(HEADER_ACCEPT_LANGUAGE, authenticator.getSessionConfiguration().getLocale().getLanguage()); requestBuilder.addHeader(HEADER_USER_AGENT, "Java Rides SDK v" + LIB_VERSION); authenticator.signRequest(requestBuilder); return chain.proceed(requestBuilder.build()); } ApiInterceptor(Authenticator authenticator); @Override Response intercept(Chain chain); static void setAuthorizationHeader(Request.Builder builder, String authorizationHeader); static String getAuthorizationHeader(Request request); final Authenticator authenticator; }
@Test public void testIntercept() throws Exception { ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class); ApiInterceptor interceptor = new ApiInterceptor(authenticator); when(chain.request()).thenReturn(new Request.Builder().url("http: when(chain.proceed(captor.capture())).thenReturn(null); SessionConfiguration config = mock(SessionConfiguration.class); when(config.getLocale()).thenReturn(Locale.US); when(authenticator.getSessionConfiguration()).thenReturn(config); interceptor.intercept(chain); verify(authenticator).signRequest(any(Request.Builder.class)); Request request = captor.getValue(); assertEquals(Locale.US.getLanguage(), request.headers().get(ApiInterceptor.HEADER_ACCEPT_LANGUAGE)); assertEquals("Java Rides SDK v" + LIB_VERSION, request.headers().get(ApiInterceptor.HEADER_USER_AGENT)); }