use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.
the class Crc32ValidationTest method adapt_InputStreamWithCrc32Header_WrappedWithValidatingStream.
@Test
public void adapt_InputStreamWithCrc32Header_WrappedWithValidatingStream() throws UnsupportedEncodingException {
InputStream content = new StringInputStream("content");
SdkHttpFullResponse httpResponse = SdkHttpFullResponse.builder().statusCode(200).putHeader("x-amz-crc32", "1234").content(AbortableInputStream.create(content)).build();
SdkHttpFullResponse adapted = adapt(httpResponse);
InputStream in = adapted.content().get().delegate();
assertThat(in).isInstanceOf((Crc32ChecksumValidatingInputStream.class));
}
use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.
the class SyncChecksumValidationInterceptorTest method afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid.
@Test
public void afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid() {
SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
PutObjectResponse response = PutObjectResponse.builder().eTag(INVALID_CHECKSUM).build();
PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();
SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder().uri(URI.create("http://localhost:8080")).method(SdkHttpMethod.PUT).contentStreamProvider(() -> new StringInputStream("Test")).build();
Context.AfterUnmarshalling afterUnmarshallingContext = InterceptorContext.builder().request(putObjectRequest).httpRequest(sdkHttpRequest).response(response).httpResponse(sdkHttpResponse).requestBody(RequestBody.fromString("Test")).build();
ExecutionAttributes attributes = getExecutionAttributesWithChecksum();
interceptor.modifyHttpContent(afterUnmarshallingContext, attributes);
assertThatThrownBy(() -> interceptor.afterUnmarshalling(afterUnmarshallingContext, attributes)).hasMessageContaining("Data read has a different checksum than expected.");
}
use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.
the class SyncChecksumValidationInterceptorTest method checksumCalculatingStreamProvider_shouldReturnNewStreamResetChecksum.
@Test
public void checksumCalculatingStreamProvider_shouldReturnNewStreamResetChecksum() throws IOException {
List<CloseAwareStream> closeAwareStreams = new ArrayList<>();
ContentStreamProvider underlyingStreamProvider = () -> {
CloseAwareStream stream = new CloseAwareStream(new StringInputStream("helloWorld"));
closeAwareStreams.add(stream);
return stream;
};
SdkChecksum checksum = new Md5Checksum();
ChecksumCalculatingStreamProvider checksumCalculatingStreamProvider = new ChecksumCalculatingStreamProvider(underlyingStreamProvider, checksum);
ChecksumCalculatingInputStream currentStream = (ChecksumCalculatingInputStream) checksumCalculatingStreamProvider.newStream();
IoUtils.drainInputStream(currentStream);
byte[] checksumBytes = currentStream.getChecksumBytes();
ChecksumCalculatingInputStream newStream = (ChecksumCalculatingInputStream) checksumCalculatingStreamProvider.newStream();
assertThat(closeAwareStreams.get(0).isClosed).isTrue();
IoUtils.drainInputStream(newStream);
byte[] newStreamChecksumBytes = newStream.getChecksumBytes();
assertThat(getChecksum(checksumBytes)).isEqualTo(getChecksum(newStreamChecksumBytes));
newStream.close();
}
use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.
the class ProfileUseArnRegionProviderTest method specifiedInOverrideConfig_shouldUse.
@Test
public void specifiedInOverrideConfig_shouldUse() {
ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
String profileFileContent = "[default]\n" + "s3_use_arn_region = true\n";
ProfileFile profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).content(new StringInputStream(profileFileContent)).build();
S3Client s3 = S3Client.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).overrideConfiguration(c -> c.defaultProfileFile(profileFile).defaultProfileName("default").addExecutionInterceptor(interceptor).retryPolicy(r -> r.numRetries(0))).build();
String arn = "arn:aws:s3:us-banana-46:12345567890:accesspoint:foo";
assertThatThrownBy(() -> s3.getObject(r -> r.bucket(arn).key("bar"))).isInstanceOf(SdkException.class);
ArgumentCaptor<Context.BeforeTransmission> context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
Mockito.verify(interceptor).beforeTransmission(context.capture(), any());
String host = context.getValue().httpRequest().host();
assertThat(host).contains("us-banana-46");
}
use of software.amazon.awssdk.utils.StringInputStream in project aws-sdk-java-v2 by aws.
the class InterceptorTestUtils method modifyHttpResponse.
public static Context.ModifyHttpResponse modifyHttpResponse(SdkRequest request, SdkHttpResponse sdkHttpResponse) {
Publisher<ByteBuffer> publisher = new EmptyPublisher<>();
InputStream responseBody = new StringInputStream("helloworld");
return new Context.ModifyResponse() {
@Override
public SdkResponse response() {
return null;
}
@Override
public SdkHttpResponse httpResponse() {
return sdkHttpResponse;
}
@Override
public Optional<Publisher<ByteBuffer>> responsePublisher() {
return Optional.of(publisher);
}
@Override
public Optional<InputStream> responseBody() {
return Optional.of(responseBody);
}
@Override
public SdkHttpRequest httpRequest() {
return SdkHttpRequest.builder().build();
}
@Override
public Optional<RequestBody> requestBody() {
return Optional.empty();
}
@Override
public Optional<AsyncRequestBody> asyncRequestBody() {
return Optional.empty();
}
@Override
public SdkRequest request() {
return request;
}
};
}
Aggregations