@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()); }
@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()); }
Retrofit createRetrofit(OkHttpClient client, Session session) { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); return new Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .baseUrl(session.getAuthenticator().getSessionConfiguration().getEndpointHost()) .client(client) .build(); } }
Retrofit createRetrofit(OkHttpClient client, Session session) { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); return new Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .baseUrl(session.getAuthenticator().getSessionConfiguration().getEndpointHost()) .client(client) .build(); } }
@Before public void setup() { MockitoAnnotations.initMocks(this); when(session.getAuthenticator()).thenReturn(authenticator); when(authenticator.getSessionConfiguration()).thenReturn(config); when(config.getLocale()).thenReturn(Locale.US); when(config.getEndpointHost()).thenReturn("http://api.uber.com"); }
@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://test").build()); 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)); }