use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.
the class Aws4EventStreamSignerTest method testEventStreamSigning.
@Test
public void testEventStreamSigning() {
TestVector testVector = generateTestVector();
SdkHttpFullRequest.Builder request = testVector.httpFullRequest();
AwsBasicCredentials credentials = AwsBasicCredentials.create("access", "secret");
SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingClock(), "us-east-1");
AsyncRequestBody transformedPublisher = SignerTestUtils.signAsyncRequest(signer, signedRequest, testVector.requestBodyPublisher(), credentials, "demo", signingClock(), "us-east-1");
TestSubscriber testSubscriber = TestSubscriber.create();
Flowable.fromPublisher(transformedPublisher).flatMap(new Function<ByteBuffer, Publisher<?>>() {
Queue<Message> messages = new LinkedList<>();
MessageDecoder decoder = new MessageDecoder(message -> messages.offer(message));
@Override
public Publisher<?> apply(ByteBuffer byteBuffer) throws Exception {
decoder.feed(byteBuffer.array());
List<Message> messageList = new ArrayList<>();
while (!messages.isEmpty()) {
messageList.add(messages.poll());
}
return Flowable.fromIterable(messageList);
}
}).subscribe(testSubscriber);
testSubscriber.assertNoErrors();
testSubscriber.assertComplete();
testSubscriber.assertValueSequence(testVector.expectedMessagePublisher().blockingIterable());
}
use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignatureWithChecksum.
@Test
void test_sign_computesCorrectSignatureWithChecksum() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "abc".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).checksumParams(SignerChecksumParams.builder().algorithm(Algorithm.SHA256).checksumHeaderName("x-amzn-header").isStreamingRequest(true).build()).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
final SdkHttpFullRequest sdkHttpFullRequest = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join();
List<String> asyncSignature = sdkHttpFullRequest.headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
final List<String> stringList = sdkHttpFullRequest.headers().get("x-amzn-header");
assertThat(stringList.stream().findFirst()).hasValue("ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=");
assertThat(sdkHttpFullRequest.firstMatchingHeader(("x-amzn-trailer"))).isNotPresent();
}
use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_publisherThrows_exceptionPropagated.
@Test
void test_sign_publisherThrows_exceptionPropagated() {
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
RuntimeException error = new RuntimeException("error");
Flowable<ByteBuffer> errorPublisher = Flowable.error(error);
AsyncRequestBody asyncBody = new AsyncRequestBody() {
@Override
public Optional<Long> contentLength() {
return Optional.of(42L);
}
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
errorPublisher.subscribe(subscriber);
}
};
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
assertThatThrownBy(asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams)::join).hasCause(error);
}
use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignature.
@Test
void test_sign_computesCorrectSignature() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "Hello AWS!".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
List<String> asyncSignature = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join().headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
}
use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.
the class EventStreamInitialRequestInterceptorTest method testInitialRequestEvent.
@Test
public void testInitialRequestEvent() {
ModifyHttpRequest context = buildContext(bytePublisher, payload);
Optional<AsyncRequestBody> modifiedBody = interceptor.modifyAsyncHttpContent(context, attr);
List<Message> messages = Flowable.fromPublisher(modifiedBody.get()).map(Message::decode).toList().blockingGet();
Message initialRequestEvent = messages.get(0);
assertArrayEquals(payload, initialRequestEvent.getPayload());
assertEquals(RPC_CONTENT_TYPE, initialRequestEvent.getHeaders().get(":content-type").getString());
}
Aggregations