Skip to content

Commit 11d2a77

Browse files
authored
Merge pull request #365 from microsoftgraph/bugfix/batches-national-clouds
fixes a bug where batching would fail for national clouds
2 parents 73afac6 + dc63fb2 commit 11d2a77

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- Fixed a bug where batching would fail for national clouds
1617
- Bumps Azure Core from 1.20.0 to 1.22.0 #359, #360, #341, #342
1718
- Bumps gson from 2.8.8 to 2.8.9 #356, #355
1819
- Bumps actions/checkout from 2.3.5 to 2.4.0 #358, #349

gradle/dependencies.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dependencies {
22
// Use JUnit test framework
33
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
4+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
45
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
56
testImplementation 'org.mockito:mockito-inline:4.0.0'
67

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<version>5.8.1</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-params</artifactId>
49+
<version>5.8.1</version>
50+
<scope>test</scope>
51+
</dependency>
4652
<dependency>
4753
<groupId>org.mockito</groupId>
4854
<artifactId>mockito-inline</artifactId>

src/main/java/com/microsoft/graph/content/BatchRequestContent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ public <T> BatchRequestStep<T> getStepById(@Nonnull final String stepId) {
168168
}
169169
/** pattern to replace the protocol and host part of the request if specified */
170170
@Nonnull
171-
protected static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive
171+
protected static final Pattern protocolAndHostReplacementPattern =
172+
Pattern.compile("(?i)^http[s]?:\\/\\/(?:graph|dod-graph|microsoftgraph)\\.(?:microsoft|chinacloudapi)\\.(?:com|cn|us|de)\\/(?:v1\\.0|beta)"); // (?i) case insensitive
173+
//https://docs.microsoft.com/en-us/graph/deployments#microsoft-graph-and-graph-explorer-service-root-endpoints
172174
/**
173175
* Generates a randomly available request id
174176
* @return a random request id

src/test/java/com/microsoft/graph/content/BatchRequestContentTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.microsoft.graph.serializer.ISerializer;
3131

3232
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.ValueSource;
3335

3436
import okhttp3.Call;
3537
import okhttp3.MediaType;
@@ -41,7 +43,7 @@
4143

4244
class BatchRequestContentTest {
4345

44-
String testurl = "http://graph.microsoft.com/me";
46+
String testurl = "http://graph.microsoft.com/v1.0/me";
4547

4648
@Test
4749
void testBatchRequestContentCreation() throws MalformedURLException {
@@ -61,11 +63,21 @@ void testGetBatchRequestContent() throws MalformedURLException {
6163
BatchRequestContent requestContent = new BatchRequestContent();
6264
String stepId = requestContent.addBatchRequestStep(requestStep);
6365
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
64-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
66+
String expectedContent = "{\"requests\":[{\"url\":\"/me\",\"method\":\"GET\",\"id\":\""
6567
+ stepId + "\"}]}";
6668
assertEquals(expectedContent, content);
6769
}
6870

71+
@ParameterizedTest
72+
@ValueSource(strings = { "https://microsoftgraph.chinacloudapi.cn/v1.0/me", "https://graph.microsoft.com/v1.0/me", "https://graph.microsoft.us/v1.0/me", "https://dod-graph.microsoft.us/v1.0/me", "https://graph.microsoft.de/v1.0/me"})
73+
void testItReplacesNationalHost(final String url) throws MalformedURLException {
74+
IHttpRequest requestStep = mock(IHttpRequest.class);
75+
when(requestStep.getRequestUrl()).thenReturn(new URL(url));
76+
BatchRequestContent requestContent = new BatchRequestContent();
77+
requestContent.addBatchRequestStep(requestStep);
78+
var step = requestContent.requests.get(0);
79+
assertEquals("/me", step.url);
80+
}
6981
@Test
7082
void testGetBatchRequestContentWithHeader() throws MalformedURLException {
7183
IHttpRequest requestStep = mock(IHttpRequest.class);
@@ -74,7 +86,7 @@ void testGetBatchRequestContentWithHeader() throws MalformedURLException {
7486
BatchRequestContent requestContent = new BatchRequestContent();
7587
String stepId = requestContent.addBatchRequestStep(requestStep);
7688
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
77-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
89+
String expectedContent = "{\"requests\":[{\"url\":\"/me\",\"method\":\"GET\",\"id\":\""
7890
+ stepId + "\",\"headers\":{\"testkey\":\"testvalue\"}}]}";
7991
assertEquals(expectedContent, content);
8092
}
@@ -105,7 +117,7 @@ void testRemoveBatchRequesStepWithIdByAddingMultipleBatchSteps() throws Malforme
105117

106118
requestContent.removeBatchRequestStepWithId(stepId);
107119
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
108-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
120+
String expectedContent = "{\"requests\":[{\"url\":\"/me\",\"method\":\"GET\",\"id\":\""
109121
+ step1Id + "\"}]}";
110122
assertEquals(expectedContent, content);
111123
}

0 commit comments

Comments
 (0)