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());
}
Aggregations