-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29d8857
commit 9d48136
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
azure-client-runtime/src/test/java/com/microsoft/azure/RequestIdHeaderInterceptorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* | ||
*/ | ||
|
||
package com.microsoft.azure; | ||
|
||
import com.microsoft.azure.serializer.AzureJacksonMapperAdapter; | ||
import com.microsoft.rest.RestClient; | ||
import com.microsoft.rest.retry.RetryHandler; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
public class RequestIdHeaderInterceptorTests { | ||
private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; | ||
|
||
@Test | ||
public void newRequestIdForEachCall() throws Exception { | ||
RestClient restClient = new RestClient.Builder("http://localhost") | ||
.withInterceptor(new RequestIdHeaderInterceptor()) | ||
.withInterceptor(new Interceptor() { | ||
private String firstRequestId = null; | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
if (request.header(REQUEST_ID_HEADER) != null) { | ||
if (firstRequestId == null) { | ||
firstRequestId = request.header(REQUEST_ID_HEADER); | ||
return new Response.Builder().code(200).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} else if (!request.header(REQUEST_ID_HEADER).equals(firstRequestId)) { | ||
return new Response.Builder().code(200).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} | ||
} | ||
return new Response.Builder().code(400).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} | ||
}) | ||
.withMapperAdapter(new AzureJacksonMapperAdapter()) | ||
.build(); | ||
AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; | ||
Response response = serviceClient.restClient().httpClient() | ||
.newCall(new Request.Builder().get().url("http://localhost").build()).execute(); | ||
Assert.assertEquals(200, response.code()); | ||
response = serviceClient.restClient().httpClient() | ||
.newCall(new Request.Builder().get().url("http://localhost").build()).execute(); | ||
Assert.assertEquals(200, response.code()); | ||
} | ||
|
||
@Test | ||
public void sameRequestIdForRetry() throws Exception { | ||
RestClient restClient = new RestClient.Builder("http://localhost") | ||
.withInterceptor(new RequestIdHeaderInterceptor()) | ||
.withInterceptor(new RetryHandler()) | ||
.withInterceptor(new Interceptor() { | ||
private String firstRequestId = null; | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
if (request.header(REQUEST_ID_HEADER) != null) { | ||
if (firstRequestId == null) { | ||
firstRequestId = request.header(REQUEST_ID_HEADER); | ||
return new Response.Builder().code(500).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} else if (request.header(REQUEST_ID_HEADER).equals(firstRequestId)) { | ||
return new Response.Builder().code(200).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} | ||
} | ||
return new Response.Builder().code(400).request(request) | ||
.protocol(Protocol.HTTP_1_1).build(); | ||
} | ||
}) | ||
.withMapperAdapter(new AzureJacksonMapperAdapter()) | ||
.build(); | ||
AzureServiceClient serviceClient = new AzureServiceClient(restClient) { }; | ||
Response response = serviceClient.restClient().httpClient() | ||
.newCall(new Request.Builder().get().url("http://localhost").build()).execute(); | ||
Assert.assertEquals(200, response.code()); | ||
} | ||
} |