@@ -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,87 @@ 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 supportsRequestBodyOnGetRequest () throws Exception {
109
+ testForResponseCode (200 , SdkHttpMethod .GET , true );
110
+ }
111
+
112
+ @ Test
113
+ public void supportsRequestBodyOnPostRequest () throws Exception {
114
+ testForResponseCode (200 , SdkHttpMethod .POST , true );
115
+ }
116
+
117
+ @ Test
118
+ public void supportsRequestBodyOnPutRequest () throws Exception {
119
+ testForResponseCode (200 , SdkHttpMethod .PUT , true );
120
+ }
121
+
122
+ @ Test
123
+ public void supportsRequestBodyOnDeleteRequest () throws Exception {
124
+ testForResponseCode (200 , SdkHttpMethod .DELETE , true );
125
+ }
126
+
127
+ @ Test
128
+ public void supportsRequestBodyOnHeadRequest () throws Exception {
129
+ testForResponseCode (200 , SdkHttpMethod .HEAD , true );
130
+ }
131
+
132
+ @ Test
133
+ public void supportsRequestBodyOnPatchRequest () throws Exception {
134
+ testForResponseCode (200 , SdkHttpMethod .PATCH , true );
135
+ }
136
+
137
+ @ Test
138
+ public void supportsRequestBodyOnOptionsRequest () throws Exception {
139
+ testForResponseCode (200 , SdkHttpMethod .OPTIONS , true );
140
+ }
141
+
107
142
private void testForResponseCode (int returnCode ) throws Exception {
108
- testForResponseCode (returnCode , SdkHttpMethod .POST );
143
+ testForResponseCode (returnCode , SdkHttpMethod .POST , true );
109
144
}
110
145
111
- private void testForResponseCode (int returnCode , SdkHttpMethod method ) throws Exception {
146
+ protected void testForResponseCode (int returnCode , SdkHttpMethod method , boolean includeBody ) throws Exception {
147
+ testForResponseCode (returnCode , method , method , includeBody );
148
+ }
149
+
150
+ protected void testForResponseCode (int returnCode ,
151
+ SdkHttpMethod method ,
152
+ SdkHttpMethod expectedMethod ,
153
+ boolean includeBody ) throws Exception {
112
154
SdkHttpClient client = createSdkHttpClient ();
113
155
114
156
stubForMockRequest (returnCode );
115
157
116
- SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), method );
158
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), method , includeBody );
117
159
HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
118
160
.request (req )
119
161
.contentStreamProvider (req .contentStreamProvider ()
120
162
.orElse (null ))
121
163
.build ())
122
164
.call ();
123
165
124
- validateResponse (rsp , returnCode , method );
166
+ validateResponse (rsp , returnCode , expectedMethod , includeBody );
125
167
}
126
168
127
169
protected void testForResponseCodeUsingHttps (SdkHttpClient client , int returnCode ) throws Exception {
128
170
SdkHttpMethod sdkHttpMethod = SdkHttpMethod .POST ;
129
171
stubForMockRequest (returnCode );
130
172
131
- SdkHttpFullRequest req = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), sdkHttpMethod );
173
+ SdkHttpFullRequest req = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), sdkHttpMethod , true );
132
174
HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
133
175
.request (req )
134
176
.contentStreamProvider (req .contentStreamProvider ()
135
177
.orElse (null ))
136
178
.build ())
137
179
.call ();
138
180
139
- validateResponse (rsp , returnCode , sdkHttpMethod );
181
+ validateResponse (rsp , returnCode , sdkHttpMethod , true );
140
182
}
141
183
142
184
private void stubForMockRequest (int returnCode ) {
@@ -151,17 +193,20 @@ private void stubForMockRequest(int returnCode) {
151
193
mockServer .stubFor (any (urlPathEqualTo ("/" )).willReturn (responseBuilder ));
152
194
}
153
195
154
- private void validateResponse (HttpExecuteResponse response , int returnCode , SdkHttpMethod method ) throws IOException {
196
+ private void validateResponse (HttpExecuteResponse response ,
197
+ int returnCode ,
198
+ SdkHttpMethod method ,
199
+ boolean expectBody ) throws IOException {
155
200
RequestMethod requestMethod = RequestMethod .fromString (method .name ());
156
201
157
202
RequestPatternBuilder patternBuilder = RequestPatternBuilder .newRequestPattern (requestMethod , urlMatching ("/" ))
158
203
.withHeader ("Host" , containing ("localhost" ))
159
204
.withHeader ("User-Agent" , equalTo ("hello-world!" ));
160
205
161
- if (method == SdkHttpMethod .HEAD ) {
162
- patternBuilder .withRequestBody (absent ());
163
- } else {
206
+ if (expectBody ) {
164
207
patternBuilder .withRequestBody (equalTo ("Body" ));
208
+ } else {
209
+ patternBuilder .withRequestBody (absent ());
165
210
}
166
211
167
212
mockServer .verify (1 , patternBuilder );
@@ -177,14 +222,14 @@ private void validateResponse(HttpExecuteResponse response, int returnCode, SdkH
177
222
mockServer .resetMappings ();
178
223
}
179
224
180
- private SdkHttpFullRequest mockSdkRequest (String uriString , SdkHttpMethod method ) {
225
+ private SdkHttpFullRequest mockSdkRequest (String uriString , SdkHttpMethod method , boolean includeBody ) {
181
226
URI uri = URI .create (uriString );
182
227
SdkHttpFullRequest .Builder requestBuilder = SdkHttpFullRequest .builder ()
183
228
.uri (uri )
184
229
.method (method )
185
230
.putHeader ("Host" , uri .getHost ())
186
231
.putHeader ("User-Agent" , "hello-world!" );
187
- if (method != SdkHttpMethod . HEAD ) {
232
+ if (includeBody ) {
188
233
byte [] content = "Body" .getBytes (StandardCharsets .UTF_8 );
189
234
requestBuilder .putHeader ("Content-Length" , Integer .toString (content .length ));
190
235
requestBuilder .contentStreamProvider (() -> new ByteArrayInputStream (content ));
0 commit comments