88import com .google .gson .JsonPrimitive ;
99
1010import com .microsoft .graph .core .GraphErrorCodes ;
11+ import com .microsoft .graph .core .IBaseClient ;
1112import com .microsoft .graph .logger .ILogger ;
1213import com .microsoft .graph .logger .LoggerLevel ;
1314import com .microsoft .graph .options .HeaderOption ;
15+ import com .microsoft .graph .options .Option ;
1416import com .microsoft .graph .serializer .DefaultSerializer ;
1517import com .microsoft .graph .serializer .ISerializer ;
1618
2123import java .io .InputStream ;
2224import java .net .URL ;
2325import java .nio .charset .StandardCharsets ;
26+ import java .util .ArrayList ;
2427import java .util .Arrays ;
28+ import java .util .Collections ;
2529
2630import okhttp3 .Call ;
2731import okhttp3 .MediaType ;
3337
3438import static org .junit .jupiter .api .Assertions .assertEquals ;
3539import static org .junit .jupiter .api .Assertions .assertFalse ;
40+ import static org .junit .jupiter .api .Assertions .assertNull ;
3641import static org .junit .jupiter .api .Assertions .assertTrue ;
3742import static org .junit .jupiter .api .Assertions .fail ;
3843import 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