Search in sources :

Example 6 with TokenCredentials

use of com.microsoft.rest.credentials.TokenCredentials in project autorest-clientruntime-for-java by Azure.

the class CredentialsTests method tokenCredentialsTest.

@Test
public void tokenCredentialsTest() throws Exception {
    TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    credentials.applyCredentialsFilter(clientBuilder);
    clientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            String header = chain.request().header("Authorization");
            Assert.assertEquals("Bearer this_is_a_token", header);
            return new Response.Builder().request(chain.request()).code(200).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Aggregations

TokenCredentials (com.microsoft.rest.credentials.TokenCredentials)6 AutoRestAzureSpecialParametersTestClientImpl (fixtures.azurespecials.implementation.AutoRestAzureSpecialParametersTestClientImpl)3 Interceptor (okhttp3.Interceptor)3 BeforeClass (org.junit.BeforeClass)3 Test (org.junit.Test)3 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)2 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)2 RestClient (com.microsoft.rest.RestClient)2 RequestIdHeaderInterceptor (com.microsoft.rest.interceptors.RequestIdHeaderInterceptor)2 UserAgentInterceptor (com.microsoft.rest.interceptors.UserAgentInterceptor)2 JacksonAdapter (com.microsoft.rest.serializer.JacksonAdapter)2 IOException (java.io.IOException)2 Response (okhttp3.Response)2 BasicAuthenticationCredentials (com.microsoft.rest.credentials.BasicAuthenticationCredentials)1 ResponseBuilder (com.microsoft.rest.protocol.ResponseBuilder)1 SerializerAdapter (com.microsoft.rest.protocol.SerializerAdapter)1 Type (java.lang.reflect.Type)1 OkHttpClient (okhttp3.OkHttpClient)1 Retrofit (retrofit2.Retrofit)1