Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clients/aws-sdk-api-gateway/tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

from aws_sdk_api_gateway.client import APIGatewayClient
from aws_sdk_api_gateway.config import APIGatewayConfig
from aws_sdk_api_gateway.models import GetApiKeysInput, GetExportInput
from smithy_test import mock


def test_should_add_default_accept_header() -> None:
"""The client sends `Accept: application/json` when the caller sets no Accept header."""
client = APIGatewayClient(APIGatewayConfig.resolve(region="us-west-2"))
mock(client).expect_request(headers={"accept": "application/json"})

client.get_api_keys(GetApiKeysInput())


def test_should_not_override_accept_header() -> None:
"""An Accept header from the caller reaches the request unchanged."""
client = APIGatewayClient(APIGatewayConfig.resolve(region="us-west-2"))
mock(client).expect_request(headers={"accept": "application/yaml"})

client.get_export(
GetExportInput(
rest_api_id="rest-api-id",
stage_name="stage-name",
export_type="oas30",
accepts="application/yaml",
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import pytest
from aws_sdk_bedrock_runtime.client import BedrockRuntimeClient
from aws_sdk_bedrock_runtime.config import BedrockRuntimeConfig
from pytest import MonkeyPatch
from smithy_test import mock

MOCK_BEARER_TOKEN = "mock-bearer-token"


# TODO: resolve a bearer token from AWS_BEARER_TOKEN_BEDROCK
@pytest.mark.skip
def test_when_environment_bearer_token_is_set(monkeypatch: MonkeyPatch) -> None:
"""With AWS_BEARER_TOKEN_BEDROCK set, the client authenticates with that token as a bearer token."""
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN)
client = BedrockRuntimeClient(
BedrockRuntimeConfig.resolve(
region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG"
)
)
mock(client).expect_request(
headers={"authorization": f"Bearer {MOCK_BEARER_TOKEN}"}
)

client.list_async_invokes()


def test_when_environment_bearer_token_is_not_set(monkeypatch: MonkeyPatch) -> None:
"""Without AWS_BEARER_TOKEN_BEDROCK, the client signs the request with SigV4."""
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
client = BedrockRuntimeClient(
BedrockRuntimeConfig.resolve(
region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG"
)
)
mock(client).expect_request(
hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"}
)

client.list_async_invokes()
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import datetime
import json
from base64 import b64encode
from io import BytesIO

from aws_sdk_bedrock_runtime.aio.client import AsyncBedrockRuntimeClient
from aws_sdk_bedrock_runtime.aio.config import AsyncBedrockRuntimeConfig
from aws_sdk_bedrock_runtime.models import (
BidirectionalInputPayloadPart,
BidirectionalOutputPayloadPart,
InvokeModelWithBidirectionalStreamInputChunk,
InvokeModelWithBidirectionalStreamOperationInput,
InvokeModelWithBidirectionalStreamOutputChunk,
)
from smithy_aws_event_stream.events import Event, EventMessage
from smithy_test.aio import mock_async

SERVER_CHUNK = bytes([48, 49, 50, 51, 52, 56, 57, 57, 57])


async def test_should_send_input_stream_chunks_as_signed_event_frames() -> None:
"""Chunks sent on the input stream go out as signed event frames, and response frames come back as output chunks."""
server_event = EventMessage(
headers={
":event-type": "chunk",
":content-type": "application/json",
":message-type": "event",
},
payload=json.dumps({"bytes": b64encode(SERVER_CHUNK).decode()}).encode(),
).encode()

async with AsyncBedrockRuntimeClient(
await AsyncBedrockRuntimeConfig.resolve(
region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG"
)
) as client:
async with await mock_async(client) as controller:
controller.expect_request().respond_with(
headers={"content-type": "application/vnd.amazon.eventstream"},
body=server_event * 10,
)

async with await client.invoke_model_with_bidirectional_stream(
InvokeModelWithBidirectionalStreamOperationInput(model_id="bedrock")
) as stream:
for chunk in ([0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]):
await stream.input_stream.send(
InvokeModelWithBidirectionalStreamInputChunk(
value=BidirectionalInputPayloadPart(bytes_=bytes(chunk))
)
)
await stream.input_stream.close()
_, output_stream = await stream.await_output()
response_chunks = [event async for event in output_stream]

assert (
response_chunks
== [
InvokeModelWithBidirectionalStreamOutputChunk(
value=BidirectionalOutputPayloadPart(bytes_=SERVER_CHUNK)
)
]
* 10
)

request_body = controller.requests[0].body
assert isinstance(request_body, bytes)
source = BytesIO(request_body)
signed_events: list[Event] = []
while (event := Event.decode(source)) is not None:
signed_events.append(event)
sent_bodies: list[object] = []
for signed in signed_events:
assert isinstance(signed.message.headers[":date"], datetime.datetime)
assert isinstance(signed.message.headers[":chunk-signature"], bytes)
inner = Event.decode(BytesIO(signed.message.payload))
if inner is not None:
sent_bodies.append(json.loads(inner.message.payload))
assert sent_bodies == [
{"bytes": b64encode(bytes([0, 1, 2, 3])).decode()},
{"bytes": b64encode(bytes([4, 5, 6, 7])).decode()},
{"bytes": b64encode(bytes([8, 9, 10, 11])).decode()},
]
2 changes: 2 additions & 0 deletions clients/aws-sdk-bedrock/tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import pytest
from aws_sdk_bedrock.client import BedrockClient
from aws_sdk_bedrock.config import BedrockConfig
from pytest import MonkeyPatch
from smithy_test import mock

MOCK_BEARER_TOKEN = "mock-bearer-token"


# TODO: resolve a bearer token from AWS_BEARER_TOKEN_BEDROCK
@pytest.mark.skip
def test_when_environment_bearer_token_is_set(monkeypatch: MonkeyPatch) -> None:
"""With AWS_BEARER_TOKEN_BEDROCK set, the client authenticates with that token as a bearer token."""
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN)
client = BedrockClient(
BedrockConfig.resolve(
region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG"
)
)
mock(client).expect_request(
headers={"authorization": f"Bearer {MOCK_BEARER_TOKEN}"}
)

client.list_custom_models()


# TODO: add the auth_scheme_preference config option
@pytest.mark.skip
def test_can_be_overridden_by_auth_scheme_preference(monkeypatch: MonkeyPatch) -> None:
"""An auth scheme preference for SigV4 takes precedence over a bearer token from the environment."""
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", MOCK_BEARER_TOKEN)
client = BedrockClient(
BedrockConfig.resolve( # pyright: ignore[reportUnknownArgumentType]
region="us-west-2",
aws_access_key_id="INTEG",
aws_secret_access_key="INTEG",
auth_scheme_preference=["sigv4"], # pyright: ignore[reportCallIssue]
)
)
mock(client).expect_request(
hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"}
)

client.list_custom_models()


def test_when_environment_bearer_token_is_not_set(monkeypatch: MonkeyPatch) -> None:
"""Without AWS_BEARER_TOKEN_BEDROCK, the client signs the request with SigV4."""
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
client = BedrockClient(
BedrockConfig.resolve(
region="us-west-2", aws_access_key_id="INTEG", aws_secret_access_key="INTEG"
)
)
mock(client).expect_request(
hostname="/bedrock/", headers={"authorization": "/AWS4-HMAC-SHA256 Credential/"}
)

client.list_custom_models()
2 changes: 2 additions & 0 deletions clients/aws-sdk-cloudfront/tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
26 changes: 26 additions & 0 deletions clients/aws-sdk-cloudfront/tests/functional/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import logging

from aws_sdk_cloudfront.client import CloudFrontClient
from aws_sdk_cloudfront.config import CloudFrontConfig
from aws_sdk_cloudfront.models import ListDistributionsInput
from pytest import LogCaptureFixture
from smithy_test import mock


def test_debug_logging_should_have_no_effect_on_request_creation(
caplog: LogCaptureFixture,
) -> None:
"""Debug logging leaves the serialized request unchanged."""
caplog.set_level(logging.DEBUG)
client = CloudFrontClient(CloudFrontConfig.resolve(region="us-west-2"))
mock(client).expect_request(
method="GET",
hostname="cloudfront.amazonaws.com",
scheme="https",
path="/2020-05-31/distribution",
)

client.list_distributions(ListDistributionsInput())
2 changes: 2 additions & 0 deletions clients/aws-sdk-dynamodb/tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
Loading
Loading