From 63803e0b0697b5acee723870f0b46dcd1ec6a7fc Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Wed, 18 May 2016 18:08:21 -0700 Subject: [PATCH] Fix runtime tests --- .../com/microsoft/rest/UserAgentInterceptor.java | 16 ++++++++++++---- .../com/microsoft/rest/CredentialsTests.java | 13 +++++-------- .../com/microsoft/rest/RetryHandlerTests.java | 7 +++++-- .../com/microsoft/rest/ServiceClientTests.java | 13 +++++-------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java index bb15c0f4305d..7705592ba9f7 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java +++ b/client-runtime/src/main/java/com/microsoft/rest/UserAgentInterceptor.java @@ -47,11 +47,19 @@ public void appendUserAgent(String userAgent) { public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String header = request.header("User-Agent"); - if (header == null || !userAgent.equals(DEFAULT_USER_AGENT_HEADER)) { - request = chain.request().newBuilder() - .header("User-Agent", userAgent + " " + header) - .build(); + if (header == null) { + header = DEFAULT_USER_AGENT_HEADER; } + if (!userAgent.equals(DEFAULT_USER_AGENT_HEADER)) { + if (header.equals(DEFAULT_USER_AGENT_HEADER)) { + header = userAgent; + } else { + header = userAgent + " " + header; + } + } + request = chain.request().newBuilder() + .header("User-Agent", header) + .build(); return chain.proceed(request); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 19b7a8ca2a4f..9071c9ae9dc4 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -9,19 +9,14 @@ import com.microsoft.rest.credentials.BasicAuthenticationCredentials; import com.microsoft.rest.credentials.TokenCredentials; - +import com.microsoft.rest.serializer.JacksonMapperAdapter; +import okhttp3.*; import org.junit.Assert; import org.junit.Test; +import retrofit2.Retrofit; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { @@ -29,6 +24,7 @@ public void basicCredentialsTest() throws Exception { Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()) .withCredentials(credentials) .withInterceptor(new Interceptor() { @Override @@ -53,6 +49,7 @@ public void tokenCredentialsTest() throws Exception { Retrofit.Builder retrofitBuilder = new Retrofit.Builder(); TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()) .withCredentials(credentials) .withInterceptor(new Interceptor() { @Override diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index 050e7374aba1..97c46f6fe4fc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -9,6 +9,7 @@ import com.microsoft.rest.retry.RetryHandler; +import com.microsoft.rest.serializer.JacksonMapperAdapter; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; @@ -41,7 +42,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); @@ -67,7 +69,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index 5497d607549d..de57b15dc397 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -7,18 +7,14 @@ package com.microsoft.rest; +import com.microsoft.rest.serializer.JacksonMapperAdapter; +import okhttp3.*; import org.junit.Assert; import org.junit.Test; +import retrofit2.Retrofit; import java.io.IOException; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import retrofit2.Retrofit; - public class ServiceClientTests { @Test public void filterTests() throws Exception { @@ -38,7 +34,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder); + RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder) + .withMapperAdapter(new JacksonMapperAdapter()); ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { }; Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code());