Skip to content

Commit b6b5f8a

Browse files
committed
- fixes a bug where the batch requests would be missing the content type
- fixes a bug where the batch request method would not be cased properly
1 parent d025c85 commit b6b5f8a

3 files changed

Lines changed: 70 additions & 33 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.microsoft.graph.http.HttpMethod;
4141
import com.microsoft.graph.http.IHttpRequest;
4242
import com.microsoft.graph.options.HeaderOption;
43+
import com.microsoft.graph.serializer.IJsonBackedObject;
4344

4445
/** Respresents the content of a JSON batch request */
4546
public class BatchRequestContent {
@@ -105,19 +106,26 @@ public <T> String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonn
105106
final BatchRequestStep<T> step = new BatchRequestStep<T>() {{
106107
url = protocolAndHostReplacementMatcher.replaceAll("");
107108
body = serializableBody;
108-
method = httpMethod;
109+
method = httpMethod.toString().toUpperCase();
109110
dependsOn = dependsOnRequestsIds != null && dependsOnRequestsIds.length > 0 ? new HashSet<String>(Arrays.asList(dependsOnRequestsIds)) : null;
110111
id = getNextRequestId();
111112
}};
112113

113114
if(!request.getHeaders().isEmpty()) {
114115
step.headers = new HashMap<>();
115116
for(final HeaderOption headerOption : request.getHeaders())
116-
step.headers.putIfAbsent(headerOption.getName(), headerOption.getValue().toString());
117+
step.headers.putIfAbsent(headerOption.getName().toLowerCase(), headerOption.getValue().toString());
118+
}
119+
if(step.body != null && step.body instanceof IJsonBackedObject &&
120+
(step.headers == null || !step.headers.containsKey(contentTypeHeaderKey))) {
121+
if(step.headers == null)
122+
step.headers = new HashMap<>();
123+
step.headers.putIfAbsent(contentTypeHeaderKey, "application/json");
117124
}
118125
requests.add(step);
119126
return step.id;
120127
}
128+
private static final String contentTypeHeaderKey = "content-type";
121129
/**
122130
* Removes requests from the requests to be executed by the batch. Also removes any dependency references that might exist.
123131
* @param stepIds ids of steps to be removed.

‎src/main/java/com/microsoft/graph/content/BatchRequestStep.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import com.google.gson.annotations.Expose;
3030
import com.google.gson.annotations.SerializedName;
31-
import com.microsoft.graph.http.HttpMethod;
3231

3332
/** the http request for the batch step */
3433
public class BatchRequestStep<T> extends BatchStep<T> {
@@ -40,7 +39,7 @@ public class BatchRequestStep<T> extends BatchStep<T> {
4039
/** The HTTP method to use to execute the request */
4140
@Expose
4241
@SerializedName("method")
43-
public HttpMethod method;
42+
public String method;
4443
/** The IDs of the steps this step depends on before being executed */
4544
@Expose
4645
@SerializedName("dependsOn")

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

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.HashSet;
1818
import java.util.concurrent.ThreadLocalRandom;
1919

20+
import com.google.gson.JsonObject;
2021
import com.google.gson.JsonParser;
2122
import com.microsoft.graph.authentication.IAuthenticationProvider;
2223
import com.microsoft.graph.core.BaseClient;
@@ -26,7 +27,10 @@
2627
import com.microsoft.graph.http.IHttpRequest;
2728
import com.microsoft.graph.logger.ILogger;
2829
import com.microsoft.graph.options.HeaderOption;
30+
import com.microsoft.graph.serializer.AdditionalDataManager;
2931
import com.microsoft.graph.serializer.DefaultSerializer;
32+
import com.microsoft.graph.serializer.IJsonBackedObject;
33+
import com.microsoft.graph.serializer.ISerializer;
3034

3135
import org.junit.jupiter.api.Test;
3236
import org.mockito.invocation.InvocationOnMock;
@@ -62,7 +66,8 @@ public void testGetBatchRequestContent() throws MalformedURLException {
6266
BatchRequestContent requestContent = new BatchRequestContent();
6367
String stepId = requestContent.addBatchRequestStep(requestStep);
6468
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
65-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"get\",\"id\":\""+stepId+"\"}]}";
69+
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
70+
+ stepId + "\"}]}";
6671
assertEquals(expectedContent, content);
6772
}
6873

@@ -74,7 +79,8 @@ public void testGetBatchRequestContentWithHeader() throws MalformedURLException
7479
BatchRequestContent requestContent = new BatchRequestContent();
7580
String stepId = requestContent.addBatchRequestStep(requestStep);
7681
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
77-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"get\",\"id\":\""+stepId+"\",\"headers\":{\"testkey\":\"testvalue\"}}]}";
82+
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
83+
+ stepId + "\",\"headers\":{\"testkey\":\"testvalue\"}}]}";
7884
assertEquals(expectedContent, content);
7985
}
8086

@@ -104,7 +110,8 @@ public void testRemoveBatchRequesStepWithIdByAddingMultipleBatchSteps() throws M
104110

105111
requestContent.removeBatchRequestStepWithId(stepId);
106112
String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
107-
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"get\",\"id\":\""+step1Id+"\"}]}";
113+
String expectedContent = "{\"requests\":[{\"url\":\"http://graph.microsoft.com/me\",\"method\":\"GET\",\"id\":\""
114+
+ step1Id + "\"}]}";
108115
assertEquals(expectedContent, content);
109116
}
110117

@@ -122,7 +129,11 @@ public void defensiveProgrammingTests() {
122129
}, "get step by id with null id throws");
123130

124131
assertThrows(NullPointerException.class, () -> {
125-
new BatchRequestContent() {{ requests = new ArrayList<>(); }}.removeBatchRequestStepWithId((String)null);
132+
new BatchRequestContent() {
133+
{
134+
requests = new ArrayList<>();
135+
}
136+
}.removeBatchRequestStepWithId((String) null);
126137
}, "remove step by id with null id throws");
127138

128139
assertThrows(IllegalArgumentException.class, () -> {
@@ -141,49 +152,42 @@ public void executeBatchTest() throws Throwable {
141152
final Call mCall = mock(Call.class);
142153
when(mHttpClient.newCall(any(Request.class))).thenReturn(mCall);
143154

144-
final CoreHttpProvider mHttpProvider = new CoreHttpProvider(new DefaultSerializer(mock(ILogger.class)), mock(ILogger.class), mHttpClient);
145-
final IBaseClient mClient = BaseClient.builder()
146-
.authenticationProvider(mock(IAuthenticationProvider.class))
147-
.httpProvider(mHttpProvider)
148-
.buildClient();
149-
final Response mResponse = new Response
150-
.Builder()
151-
.request(new Request
152-
.Builder()
153-
.url("https://graph.microsoft.com/v1.0/$batch")
154-
.build())
155-
.code(200)
156-
.protocol(Protocol.HTTP_1_1)
157-
.message("OK")
158-
.addHeader("Content-type", "application/json")
159-
.body(ResponseBody.create("{\"responses\": [{\"id\": \""+stepId+"\",\"status\": 200,\"body\": null}]}",
160-
MediaType.parse("application/json")))
161-
.build();
155+
final CoreHttpProvider mHttpProvider = new CoreHttpProvider(new DefaultSerializer(mock(ILogger.class)),
156+
mock(ILogger.class), mHttpClient);
157+
final IBaseClient mClient = BaseClient.builder().authenticationProvider(mock(IAuthenticationProvider.class))
158+
.httpProvider(mHttpProvider).buildClient();
159+
final Response mResponse = new Response.Builder()
160+
.request(new Request.Builder().url("https://graph.microsoft.com/v1.0/$batch").build()).code(200)
161+
.protocol(Protocol.HTTP_1_1).message("OK").addHeader("Content-type", "application/json")
162+
.body(ResponseBody.create(
163+
"{\"responses\": [{\"id\": \"" + stepId + "\",\"status\": 200,\"body\": null}]}",
164+
MediaType.parse("application/json")))
165+
.build();
162166
when(mCall.execute()).thenReturn(mResponse);
163167
final BatchResponseContent batchResponse = mClient.batch().buildRequest().post(content);
164168
assertNotNull(mClient.batch().buildRequest().postAsync(content));
165169
final BatchResponseStep<?> response = batchResponse.getResponseById(stepId);
166170
assertNotNull(response);
167171
}
172+
168173
@Test
169-
public void usesHttpMethodFromRequestIfAlreadySet() throws MalformedURLException
170-
{
174+
public void usesHttpMethodFromRequestIfAlreadySet() throws MalformedURLException {
171175
IHttpRequest requestStep = mock(IHttpRequest.class);
172176
when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
173177
when(requestStep.getHttpMethod()).thenReturn(HttpMethod.DELETE);
174178
final BatchRequestContent batchRequest = new BatchRequestContent();
175179
final String stepId = batchRequest.addBatchRequestStep(requestStep);
176-
assertEquals(HttpMethod.DELETE, batchRequest.getStepById(stepId).method);
180+
assertEquals("DELETE", batchRequest.getStepById(stepId).method);
177181
}
182+
178183
@Test
179-
public void doesntThrowWhenTryingToRemoveRequestFromNull()
180-
{
184+
public void doesntThrowWhenTryingToRemoveRequestFromNull() {
181185
final BatchRequestContent batchRequest = new BatchRequestContent();
182186
batchRequest.removeBatchRequestStepWithId("id");
183187
}
188+
184189
@Test
185-
public void doesntRemoveDependsOnWhenNotEmpty() throws MalformedURLException
186-
{
190+
public void doesntRemoveDependsOnWhenNotEmpty() throws MalformedURLException {
187191
IHttpRequest requestStep = mock(IHttpRequest.class);
188192
when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
189193
final BatchRequestContent batchRequest = new BatchRequestContent();
@@ -195,4 +199,30 @@ public void doesntRemoveDependsOnWhenNotEmpty() throws MalformedURLException
195199

196200
assertNotNull(batchRequest.getStepById(stepId3).dependsOn);
197201
}
202+
203+
@Test
204+
public void addsContentTypeForBodies() throws MalformedURLException {
205+
IHttpRequest requestStep = mock(IHttpRequest.class);
206+
when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
207+
final BatchRequestContent batchRequest = new BatchRequestContent();
208+
final IJsonBackedObject body = new IJsonBackedObject() {
209+
210+
@Override
211+
public void setRawObject(ISerializer serializer, JsonObject json) {
212+
// TODO Auto-generated method stub
213+
214+
}
215+
216+
@Override
217+
public AdditionalDataManager additionalDataManager() {
218+
// TODO Auto-generated method stub
219+
return null;
220+
}
221+
222+
};
223+
final String stepId = batchRequest.addBatchRequestStep(requestStep, HttpMethod.POST, body);
224+
final BatchRequestStep<?> step = batchRequest.getStepById(stepId);
225+
assertNotNull(step.headers);
226+
assertEquals("application/json", step.headers.get("content-type"));
227+
}
198228
}

0 commit comments

Comments
 (0)