Skip to content

Commit b9b566c

Browse files
committed
- fixes a bug where the content type for requests with empty bodies would be wrongfully defaulted
1 parent 0c6f373 commit b9b566c

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,7 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
268268
// This ensures that the Content-Length header is properly set
269269
if (request.getHttpMethod() == HttpMethod.POST) {
270270
bytesToWrite = new byte[0];
271-
if(contenttype == null) {
272-
contenttype = BINARY_CONTENT_TYPE;
273-
}
274-
}
275-
else {
271+
} else {
276272
bytesToWrite = null;
277273
}
278274
} else if (serializable instanceof byte[]) {
@@ -329,7 +325,11 @@ public void writeTo(BufferedSink sink) throws IOException {
329325

330326
@Override
331327
public MediaType contentType() {
332-
return MediaType.parse(mediaContentType);
328+
if(mediaContentType == null || mediaContentType.isEmpty()) {
329+
return null;
330+
} else {
331+
return MediaType.parse(mediaContentType);
332+
}
333333
}
334334
};
335335
}

src/test/java/com/microsoft/graph/http/CoreHttpProviderTests.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import com.google.gson.JsonPrimitive;
99

1010
import com.microsoft.graph.core.GraphErrorCodes;
11+
import com.microsoft.graph.core.IBaseClient;
1112
import com.microsoft.graph.logger.ILogger;
1213
import com.microsoft.graph.logger.LoggerLevel;
1314
import com.microsoft.graph.options.HeaderOption;
15+
import com.microsoft.graph.options.Option;
1416
import com.microsoft.graph.serializer.DefaultSerializer;
1517
import com.microsoft.graph.serializer.ISerializer;
1618

@@ -21,7 +23,9 @@
2123
import java.io.InputStream;
2224
import java.net.URL;
2325
import java.nio.charset.StandardCharsets;
26+
import java.util.ArrayList;
2427
import java.util.Arrays;
28+
import java.util.Collections;
2529

2630
import okhttp3.Call;
2731
import okhttp3.MediaType;
@@ -33,6 +37,7 @@
3337

3438
import static org.junit.jupiter.api.Assertions.assertEquals;
3539
import static org.junit.jupiter.api.Assertions.assertFalse;
40+
import static org.junit.jupiter.api.Assertions.assertNull;
3641
import static org.junit.jupiter.api.Assertions.assertTrue;
3742
import static org.junit.jupiter.api.Assertions.fail;
3843
import static org.mockito.ArgumentMatchers.any;
@@ -63,14 +68,14 @@ public void testErrorResponse() throws Exception {
6368
mProvider.send(mRequest, Object.class, null);
6469
fail("Expected exception in previous statement");
6570
} catch (final GraphServiceException e) {
66-
assertTrue(e.getMessage().indexOf("truncated") > 0);
71+
assertTrue(e.getMessage().indexOf("truncated") > 0);
6772
assertEquals(expectedMessage, e.getServiceError().message);
6873
}
6974
}
7075

7176
@Test
7277
public void testVerboseErrorResponse() throws Exception {
73-
final GraphErrorCodes expectedErrorCode = GraphErrorCodes.INVALID_REQUEST;
78+
final GraphErrorCodes expectedErrorCode = GraphErrorCodes.INVALID_REQUEST;
7479
final String expectedMessage = "Test error!";
7580
final GraphErrorResponse toSerialize = new GraphErrorResponse();
7681
toSerialize.error = new GraphError();
@@ -97,8 +102,8 @@ public void testVerboseErrorResponse() throws Exception {
97102
mProvider.send(mRequest, Object.class, null);
98103
fail("Expected exception in previous statement");
99104
} catch (final GraphServiceException e) {
100-
assertFalse(e.getMessage().indexOf("truncated") > 0);
101-
assertTrue(e.getMessage().indexOf("The raw request was invalid") < 0);
105+
assertFalse(e.getMessage().indexOf("truncated") > 0);
106+
assertTrue(e.getMessage().indexOf("The raw request was invalid") < 0);
102107
}
103108
}
104109

@@ -139,6 +144,36 @@ public void testStreamToStringReturnsEmpty() {
139144
String convertedData = CoreHttpProvider.streamToString(inputStream);
140145
assertEquals("", convertedData);
141146
}
147+
@Test
148+
public void emptyPostContentTypeIsNotReset() {
149+
final String contentTypeValue = "application/json";
150+
final HeaderOption ctype = new HeaderOption("Content-Type", contentTypeValue);
151+
final ArrayList<Option> options = new ArrayList<>();
152+
options.add(ctype);
153+
final IHttpRequest absRequest = new BaseRequest<String>("https://localhost", mock(IBaseClient.class), options, String.class) {{
154+
this.setHttpMethod(HttpMethod.POST);
155+
}};
156+
final ISerializer serializer = mock(ISerializer.class);
157+
final ILogger logger = mock(ILogger.class);
158+
mProvider = new CoreHttpProvider(serializer,
159+
logger,
160+
new OkHttpClient.Builder().build());
161+
final Request request = mProvider.getHttpRequest(absRequest, String.class, null);
162+
assertEquals(contentTypeValue, request.body().contentType().toString());
163+
}
164+
@Test
165+
public void emptyPostContentTypeIsNotSet() {
166+
final IHttpRequest absRequest = new BaseRequest<String>("https://localhost", mock(IBaseClient.class), Collections.emptyList(), String.class) {{
167+
this.setHttpMethod(HttpMethod.POST);
168+
}};
169+
final ISerializer serializer = mock(ISerializer.class);
170+
final ILogger logger = mock(ILogger.class);
171+
mProvider = new CoreHttpProvider(serializer,
172+
logger,
173+
new OkHttpClient.Builder().build());
174+
final Request request = mProvider.getHttpRequest(absRequest, String.class, null);
175+
assertNull(request.body().contentType());
176+
}
142177

143178

144179
/**

0 commit comments

Comments
 (0)