@@ -61,7 +61,7 @@ public void supportsResponseCode200() throws Exception {
61
61
@ Test
62
62
public void supportsResponseCode200Head () throws Exception {
63
63
// HEAD is special due to closing of the connection immediately and streams are null
64
- testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD );
64
+ testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD , false );
65
65
}
66
66
67
67
@ Test
@@ -76,7 +76,7 @@ public void supportsResponseCode403() throws Exception {
76
76
77
77
@ Test
78
78
public void supportsResponseCode403Head () throws Exception {
79
- testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD );
79
+ testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD , false );
80
80
}
81
81
82
82
@ Test
@@ -98,45 +98,122 @@ public void supportsResponseCode500() throws Exception {
98
98
public void validatesHttpsCertificateIssuer () {
99
99
SdkHttpClient client = createSdkHttpClient ();
100
100
101
- SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), SdkHttpMethod .POST );
101
+ SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), SdkHttpMethod .POST , true );
102
102
103
103
assertThatThrownBy (client .prepareRequest (HttpExecuteRequest .builder ().request (request ).build ())::call )
104
104
.isInstanceOf (SSLHandshakeException .class );
105
105
}
106
106
107
+ @ Test
108
+ public void supportsDeleteRequestWithoutRequestBody () throws Exception {
109
+ testForResponseCode (200 , SdkHttpMethod .DELETE , false );
110
+ }
111
+
112
+ @ Test
113
+ public void supportsDeleteRequestWithRequestBody () throws Exception {
114
+ testForResponseCode (200 , SdkHttpMethod .DELETE , true );
115
+ }
116
+
117
+ @ Test
118
+ public void supportsGetRequestWithoutRequestBody () throws Exception {
119
+ testForResponseCode (200 , SdkHttpMethod .GET , false );
120
+ }
121
+
122
+ @ Test
123
+ public void supportsGetRequestWithRequestBody () throws Exception {
124
+ testForResponseCode (200 , SdkHttpMethod .GET , true );
125
+ }
126
+
127
+ @ Test
128
+ public void supportsHeadRequestWithoutRequestBody () throws Exception {
129
+ testForResponseCode (200 , SdkHttpMethod .HEAD , false );
130
+ }
131
+
132
+ @ Test
133
+ public void supportsHeadRequestWithRequestBody () throws Exception {
134
+ testForResponseCode (200 , SdkHttpMethod .HEAD , true );
135
+ }
136
+
137
+ @ Test
138
+ public void supportsOptionsRequestWithoutRequestBody () throws Exception {
139
+ testForResponseCode (200 , SdkHttpMethod .OPTIONS , false );
140
+ }
141
+
142
+ @ Test
143
+ public void supportsOptionsRequestWithRequestBody () throws Exception {
144
+ testForResponseCode (200 , SdkHttpMethod .OPTIONS , true );
145
+ }
146
+
147
+ @ Test
148
+ public void supportsPatchRequestWithoutRequestBody () throws Exception {
149
+ testForResponseCode (200 , SdkHttpMethod .PATCH , false );
150
+ }
151
+
152
+ @ Test
153
+ public void supportsPatchRequestWithRequestBody () throws Exception {
154
+ testForResponseCode (200 , SdkHttpMethod .PATCH , true );
155
+ }
156
+
157
+ @ Test
158
+ public void supportsPostRequestWithoutRequestBody () throws Exception {
159
+ testForResponseCode (200 , SdkHttpMethod .POST , false );
160
+ }
161
+
162
+ @ Test
163
+ public void supportsPostRequestWithRequestBody () throws Exception {
164
+ testForResponseCode (200 , SdkHttpMethod .POST , true );
165
+ }
166
+
167
+ @ Test
168
+ public void supportsPutRequestWithoutRequestBody () throws Exception {
169
+ testForResponseCode (200 , SdkHttpMethod .PUT , false );
170
+ }
171
+
172
+ @ Test
173
+ public void supportsPutRequestWithRequestBody () throws Exception {
174
+ testForResponseCode (200 , SdkHttpMethod .PUT , true );
175
+ }
176
+
107
177
private void testForResponseCode (int returnCode ) throws Exception {
108
- testForResponseCode (returnCode , SdkHttpMethod .POST );
178
+ testForResponseCode (returnCode , SdkHttpMethod .POST , true );
109
179
}
110
180
111
- private void testForResponseCode (int returnCode , SdkHttpMethod method ) throws Exception {
181
+ protected void testForResponseCode (int returnCode , SdkHttpMethod method , boolean includeBody ) throws Exception {
182
+ testForResponseCode (returnCode , method , method , includeBody );
183
+ }
184
+
185
+ protected void testForResponseCode (int returnCode ,
186
+ SdkHttpMethod method ,
187
+ SdkHttpMethod expectedMethod ,
188
+ boolean includeBody ) throws Exception {
112
189
SdkHttpClient client = createSdkHttpClient ();
113
190
114
191
stubForMockRequest (returnCode );
115
192
116
- SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), method );
193
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), method , includeBody );
117
194
HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
118
195
.request (req )
119
196
.contentStreamProvider (req .contentStreamProvider ()
120
197
.orElse (null ))
121
198
.build ())
122
199
.call ();
123
200
124
- validateResponse (rsp , returnCode , method );
201
+ validateResponse (rsp , returnCode , expectedMethod , includeBody );
125
202
}
126
203
127
204
protected void testForResponseCodeUsingHttps (SdkHttpClient client , int returnCode ) throws Exception {
128
205
SdkHttpMethod sdkHttpMethod = SdkHttpMethod .POST ;
129
206
stubForMockRequest (returnCode );
130
207
131
- SdkHttpFullRequest req = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), sdkHttpMethod );
208
+ SdkHttpFullRequest req = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), sdkHttpMethod , true );
132
209
HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
133
210
.request (req )
134
211
.contentStreamProvider (req .contentStreamProvider ()
135
212
.orElse (null ))
136
213
.build ())
137
214
.call ();
138
215
139
- validateResponse (rsp , returnCode , sdkHttpMethod );
216
+ validateResponse (rsp , returnCode , sdkHttpMethod , true );
140
217
}
141
218
142
219
private void stubForMockRequest (int returnCode ) {
@@ -151,17 +228,24 @@ private void stubForMockRequest(int returnCode) {
151
228
mockServer .stubFor (any (urlPathEqualTo ("/" )).willReturn (responseBuilder ));
152
229
}
153
230
154
- private void validateResponse (HttpExecuteResponse response , int returnCode , SdkHttpMethod method ) throws IOException {
231
+ private void validateResponse (HttpExecuteResponse response ,
232
+ int returnCode ,
233
+ SdkHttpMethod method ,
234
+ boolean expectBody ) throws IOException {
155
235
RequestMethod requestMethod = RequestMethod .fromString (method .name ());
156
236
157
237
RequestPatternBuilder patternBuilder = RequestPatternBuilder .newRequestPattern (requestMethod , urlMatching ("/" ))
158
238
.withHeader ("Host" , containing ("localhost" ))
159
239
.withHeader ("User-Agent" , equalTo ("hello-world!" ));
160
240
161
- if (method == SdkHttpMethod .HEAD ) {
162
- patternBuilder .withRequestBody (absent ());
241
+ if (expectBody ) {
242
+ patternBuilder .withRequestBody (equalTo ("Body" ))
243
+ .withHeader ("Content-Length" , equalTo ("4" ));
163
244
} else {
164
- patternBuilder .withRequestBody (equalTo ("Body" ));
245
+ patternBuilder .withRequestBody (absent ());
246
+ if (method != SdkHttpMethod .PATCH && method != SdkHttpMethod .POST && method != SdkHttpMethod .PUT ) {
247
+ patternBuilder .withoutHeader ("Content-Length" );
248
+ }
165
249
}
166
250
167
251
mockServer .verify (1 , patternBuilder );
@@ -177,14 +261,14 @@ private void validateResponse(HttpExecuteResponse response, int returnCode, SdkH
177
261
mockServer .resetMappings ();
178
262
}
179
263
180
- private SdkHttpFullRequest mockSdkRequest (String uriString , SdkHttpMethod method ) {
264
+ private SdkHttpFullRequest mockSdkRequest (String uriString , SdkHttpMethod method , boolean includeBody ) {
181
265
URI uri = URI .create (uriString );
182
266
SdkHttpFullRequest .Builder requestBuilder = SdkHttpFullRequest .builder ()
183
267
.uri (uri )
184
268
.method (method )
185
269
.putHeader ("Host" , uri .getHost ())
186
270
.putHeader ("User-Agent" , "hello-world!" );
187
- if (method != SdkHttpMethod . HEAD ) {
271
+ if (includeBody ) {
188
272
byte [] content = "Body" .getBytes (StandardCharsets .UTF_8 );
189
273
requestBuilder .putHeader ("Content-Length" , Integer .toString (content .length ));
190
274
requestBuilder .contentStreamProvider (() -> new ByteArrayInputStream (content ));
0 commit comments