|
30 | 30 | import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER_ALTERNATE;
|
31 | 31 | import static software.amazon.awssdk.core.http.HttpResponseHandler.X_AMZ_ID_2_HEADER;
|
32 | 32 |
|
| 33 | +import com.github.tomakehurst.wiremock.http.Fault; |
33 | 34 | import com.github.tomakehurst.wiremock.http.HttpHeader;
|
34 | 35 | import com.github.tomakehurst.wiremock.http.HttpHeaders;
|
35 | 36 | import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
|
|
46 | 47 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
47 | 48 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
48 | 49 | import software.amazon.awssdk.core.ResponseBytes;
|
| 50 | +import software.amazon.awssdk.core.ResponseInputStream; |
49 | 51 | import software.amazon.awssdk.core.async.AsyncResponseTransformer;
|
| 52 | +import software.amazon.awssdk.core.exception.SdkClientException; |
50 | 53 | import software.amazon.awssdk.crt.CrtResource;
|
51 | 54 | import software.amazon.awssdk.crt.Log;
|
52 | 55 | import software.amazon.awssdk.regions.Region;
|
@@ -149,6 +152,77 @@ public void requestFailedMidway_shouldThrowException() {
|
149 | 152 | });
|
150 | 153 | }
|
151 | 154 |
|
| 155 | + @Test |
| 156 | + public void toBlockingInputStream_requestFailedMidwayDueToServerError_shouldThrowException() { |
| 157 | + HttpHeaders httpHeaders = new HttpHeaders(new HttpHeader("content-length", "12345676"), |
| 158 | + new HttpHeader("etag", E_TAG)); |
| 159 | + stubFor(head(anyUrl()).willReturn(aResponse().withStatus(200) |
| 160 | + .withHeaders(httpHeaders))); |
| 161 | + |
| 162 | + stubFor(get(anyUrl()) |
| 163 | + .inScenario("SucceedThenFail") |
| 164 | + .whenScenarioStateIs(Scenario.STARTED) |
| 165 | + .willSetStateTo("first request") |
| 166 | + .willReturn(aResponse() |
| 167 | + .withStatus(200) |
| 168 | + .withBody("helloworld".getBytes(StandardCharsets.UTF_8)))); |
| 169 | + |
| 170 | + stubFor(get(anyUrl()) |
| 171 | + .inScenario("SucceedThenFail") |
| 172 | + .whenScenarioStateIs("first request") |
| 173 | + .willSetStateTo("second request") |
| 174 | + .willReturn(aResponse() |
| 175 | + .withStatus(404) |
| 176 | + .withHeader(X_AMZ_ID_2_HEADER, "foo") |
| 177 | + .withHeader(X_AMZN_REQUEST_ID_HEADER_ALTERNATE, "bar") |
| 178 | + .withBody("".getBytes(StandardCharsets.UTF_8)))); |
| 179 | + ResponseInputStream<GetObjectResponse> stream = s3AsyncClient.getObject( |
| 180 | + r -> r.bucket(BUCKET).key(KEY), AsyncResponseTransformer.toBlockingInputStream()) |
| 181 | + .join(); |
| 182 | + byte[] buffer = new byte[1024 * 8]; |
| 183 | + assertThatThrownBy(() -> stream.read(buffer, 0, buffer.length)) |
| 184 | + .satisfies(throwable -> { |
| 185 | + assertThat(throwable).isInstanceOf(S3Exception.class); |
| 186 | + S3Exception s3Exception = (S3Exception) throwable; |
| 187 | + assertThat(s3Exception.statusCode()).isEqualTo(404); |
| 188 | + assertThat(s3Exception.extendedRequestId()).isEqualTo("foo"); |
| 189 | + assertThat(s3Exception.requestId()).isEqualTo("bar"); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void toBlockingInputStream_requestFailedMidwayDueToIoError_shouldThrowException() { |
| 195 | + HttpHeaders httpHeaders = new HttpHeaders(new HttpHeader("content-length", "12345676"), |
| 196 | + new HttpHeader("etag", E_TAG)); |
| 197 | + stubFor(head(anyUrl()).willReturn(aResponse().withStatus(200) |
| 198 | + .withHeaders(httpHeaders))); |
| 199 | + |
| 200 | + stubFor(get(anyUrl()) |
| 201 | + .inScenario("SucceedThenFail") |
| 202 | + .whenScenarioStateIs(Scenario.STARTED) |
| 203 | + .willSetStateTo("first request") |
| 204 | + .willReturn(aResponse() |
| 205 | + .withStatus(200) |
| 206 | + .withBody("helloworld".getBytes(StandardCharsets.UTF_8)))); |
| 207 | + |
| 208 | + stubFor(get(anyUrl()) |
| 209 | + .inScenario("SucceedThenFail") |
| 210 | + .whenScenarioStateIs("first request") |
| 211 | + .willSetStateTo("second request") |
| 212 | + .willReturn(aResponse() |
| 213 | + .withFault(Fault.RANDOM_DATA_THEN_CLOSE))); |
| 214 | + ResponseInputStream<GetObjectResponse> stream = s3AsyncClient.getObject( |
| 215 | + r -> r.bucket(BUCKET).key(KEY), AsyncResponseTransformer.toBlockingInputStream()) |
| 216 | + .join(); |
| 217 | + byte[] buffer = new byte[1024 * 8]; |
| 218 | + assertThatThrownBy(() -> stream.read(buffer, 0, buffer.length)) |
| 219 | + .satisfies(throwable -> { |
| 220 | + assertThat(throwable).isInstanceOf(SdkClientException.class); |
| 221 | + SdkClientException exception = (SdkClientException) throwable; |
| 222 | + assertThat(exception.getMessage()).contains("Failed to send the request"); |
| 223 | + }); |
| 224 | + } |
| 225 | + |
152 | 226 | @Test
|
153 | 227 | void overrideResponseCompletionExecutor_shouldCompleteWithCustomExecutor(WireMockRuntimeInfo wiremock) {
|
154 | 228 |
|
|
0 commit comments