19
19
import static com .github .tomakehurst .wiremock .client .WireMock .any ;
20
20
import static com .github .tomakehurst .wiremock .client .WireMock .containing ;
21
21
import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
22
- import static com .github .tomakehurst .wiremock .client .WireMock .postRequestedFor ;
23
22
import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
24
23
import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
25
24
import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
29
28
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
30
29
31
30
import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
31
+ import com .github .tomakehurst .wiremock .http .RequestMethod ;
32
32
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
33
+ import com .github .tomakehurst .wiremock .matching .RequestPatternBuilder ;
33
34
import java .io .ByteArrayInputStream ;
34
35
import java .io .IOException ;
35
36
import java .net .HttpURLConnection ;
39
40
import org .junit .Rule ;
40
41
import org .junit .Test ;
41
42
import org .junit .runner .RunWith ;
42
- import org .mockito .Mock ;
43
43
import org .mockito .runners .MockitoJUnitRunner ;
44
44
import software .amazon .awssdk .utils .IoUtils ;
45
45
@@ -54,14 +54,17 @@ public abstract class SdkHttpClientTestSuite {
54
54
@ Rule
55
55
public WireMockRule mockServer = new WireMockRule (wireMockConfig ().dynamicPort ().dynamicHttpsPort ());
56
56
57
- @ Mock
58
- private SdkRequestContext requestContext ;
59
-
60
57
@ Test
61
58
public void supportsResponseCode200 () throws Exception {
62
59
testForResponseCode (HttpURLConnection .HTTP_OK );
63
60
}
64
61
62
+ @ Test
63
+ public void supportsResponseCode200HEAD () throws Exception {
64
+ // HEAD is special due to closing of the connection immediately and streams are null
65
+ testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD );
66
+ }
67
+
65
68
@ Test
66
69
public void supportsResponseCode202 () throws Exception {
67
70
testForResponseCode (HttpURLConnection .HTTP_ACCEPTED );
@@ -72,6 +75,11 @@ public void supportsResponseCode403() throws Exception {
72
75
testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN );
73
76
}
74
77
78
+ @ Test
79
+ public void supportsResponseCode403HEAD () throws Exception {
80
+ testForResponseCode (HttpURLConnection .HTTP_FORBIDDEN , SdkHttpMethod .HEAD );
81
+ }
82
+
75
83
@ Test
76
84
public void supportsResponseCode301 () throws Exception {
77
85
testForResponseCode (HttpURLConnection .HTTP_MOVED_PERM );
@@ -91,42 +99,45 @@ public void supportsResponseCode500() throws Exception {
91
99
public void validatesHttpsCertificateIssuer () throws Exception {
92
100
SdkHttpClient client = createSdkHttpClient ();
93
101
94
- SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort ());
102
+ SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), SdkHttpMethod . POST );
95
103
96
104
assertThatThrownBy (client .prepareRequest (HttpExecuteRequest .builder ().request (request ).build ())::call )
97
105
.isInstanceOf (SSLHandshakeException .class );
98
106
}
99
107
100
108
private void testForResponseCode (int returnCode ) throws Exception {
109
+ testForResponseCode (returnCode , SdkHttpMethod .POST );
110
+ }
111
+
112
+ private void testForResponseCode (int returnCode , SdkHttpMethod method ) throws Exception {
101
113
SdkHttpClient client = createSdkHttpClient ();
102
114
103
115
stubForMockRequest (returnCode );
104
116
105
- SdkHttpFullRequest request = mockSdkRequest ("http://localhost:" + mockServer .port ());
106
- HttpExecuteResponse response = client .prepareRequest (HttpExecuteRequest .builder ()
107
- .request (request )
108
- .contentStreamProvider (
109
- request .contentStreamProvider ()
110
- .get ())
111
- .build ())
112
- .call ();
117
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), method );
118
+ HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
119
+ .request (req )
120
+ .contentStreamProvider (req .contentStreamProvider ()
121
+ .orElse (null ))
122
+ .build ())
123
+ .call ();
113
124
114
- validateResponse (response , returnCode );
125
+ validateResponse (rsp , returnCode , method );
115
126
}
116
127
117
128
protected void testForResponseCodeUsingHttps (SdkHttpClient client , int returnCode ) throws Exception {
129
+ SdkHttpMethod sdkHttpMethod = SdkHttpMethod .POST ;
118
130
stubForMockRequest (returnCode );
119
131
120
- SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort ());
121
- HttpExecuteResponse response = client .prepareRequest (HttpExecuteRequest .builder ()
122
- .request (request )
123
- .contentStreamProvider (
124
- request .contentStreamProvider ()
125
- .get ())
126
- .build ())
127
- .call ();
132
+ SdkHttpFullRequest req = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), sdkHttpMethod );
133
+ HttpExecuteResponse rsp = client .prepareRequest (HttpExecuteRequest .builder ()
134
+ .request (req )
135
+ .contentStreamProvider (req .contentStreamProvider ()
136
+ .orElse (null ))
137
+ .build ())
138
+ .call ();
128
139
129
- validateResponse (response , returnCode );
140
+ validateResponse (rsp , returnCode , sdkHttpMethod );
130
141
}
131
142
132
143
private void stubForMockRequest (int returnCode ) {
@@ -141,27 +152,44 @@ private void stubForMockRequest(int returnCode) {
141
152
stubFor (any (urlPathEqualTo ("/" )).willReturn (responseBuilder ));
142
153
}
143
154
144
- private void validateResponse (HttpExecuteResponse response , int returnCode ) throws IOException {
145
- verify (1 , postRequestedFor (urlMatching ("/" ))
146
- .withHeader ("Host" , containing ("localhost" ))
147
- .withHeader ("User-Agent" , equalTo ("hello-world!" ))
148
- .withRequestBody (equalTo ("Body" )));
155
+ private void validateResponse (HttpExecuteResponse response , int returnCode , SdkHttpMethod method ) throws IOException {
156
+ RequestMethod requestMethod = RequestMethod .fromString (method .name ());
157
+
158
+ RequestPatternBuilder patternBuilder = RequestPatternBuilder .newRequestPattern (requestMethod , urlMatching ("/" ))
159
+ .withHeader ("Host" , containing ("localhost" ))
160
+ .withHeader ("User-Agent" , equalTo ("hello-world!" ));
161
+
162
+ if (method == SdkHttpMethod .HEAD ) {
163
+ patternBuilder .withRequestBody (equalTo ("" ));
164
+ } else {
165
+ patternBuilder .withRequestBody (equalTo ("Body" ));
166
+ }
167
+
168
+ verify (1 , patternBuilder );
169
+
170
+ if (method == SdkHttpMethod .HEAD ) {
171
+ assertThat (response .responseBody ()).isEmpty ();
172
+ } else {
173
+ assertThat (IoUtils .toUtf8String (response .responseBody ().orElse (null ))).isEqualTo ("hello" );
174
+ }
149
175
150
- assertThat (IoUtils .toUtf8String (response .responseBody ().orElse (null ))).isEqualTo ("hello" );
151
176
assertThat (response .httpResponse ().firstMatchingHeader ("Some-Header" )).contains ("With Value" );
152
177
assertThat (response .httpResponse ().statusCode ()).isEqualTo (returnCode );
153
178
mockServer .resetMappings ();
154
179
}
155
180
156
- private SdkHttpFullRequest mockSdkRequest (String uriString ) {
181
+ private SdkHttpFullRequest mockSdkRequest (String uriString , SdkHttpMethod method ) {
157
182
URI uri = URI .create (uriString );
158
- return SdkHttpFullRequest .builder ()
159
- .uri (uri )
160
- .method (SdkHttpMethod .POST )
161
- .putHeader ("Host" , uri .getHost ())
162
- .putHeader ("User-Agent" , "hello-world!" )
163
- .contentStreamProvider (() -> new ByteArrayInputStream ("Body" .getBytes (StandardCharsets .UTF_8 )))
164
- .build ();
183
+ SdkHttpFullRequest .Builder requestBuilder = SdkHttpFullRequest .builder ()
184
+ .uri (uri )
185
+ .method (method )
186
+ .putHeader ("Host" , uri .getHost ())
187
+ .putHeader ("User-Agent" , "hello-world!" );
188
+ if (method != SdkHttpMethod .HEAD ) {
189
+ requestBuilder .contentStreamProvider (() -> new ByteArrayInputStream ("Body" .getBytes (StandardCharsets .UTF_8 )));
190
+ }
191
+
192
+ return requestBuilder .build ();
165
193
}
166
194
167
195
/**
0 commit comments