Skip to content

Commit

Permalink
Fix runtime tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed May 19, 2016
1 parent c2765e5 commit 63803e0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@

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 {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down

0 comments on commit 63803e0

Please sign in to comment.