Skip to content

Commit

Permalink
Test runtime with httpmock. (#780)
Browse files Browse the repository at this point in the history
* Test runtime with httpmock.

- Remove generic runtime over the client.
- Make tests use httpmock for more concise assertions.

Signed-off-by: David Calavera <[email protected]>

* Bump MSRV to 1.65.

Fixes compilation issues with the regex crate.

Signed-off-by: David Calavera <[email protected]>

---------

Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera authored Jan 16, 2024
1 parent 6e1c154 commit 99eb031
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 396 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
toolchain:
- "1.64.0" # Current MSRV
- "1.65.0" # Current MSRV
- stable
env:
RUST_BACKTRACE: 1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
toolchain:
- "1.64.0" # Current MSRV
- "1.65.0" # Current MSRV
- stable
env:
RUST_BACKTRACE: 1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
toolchain:
- "1.64.0" # Current MSRV
- "1.65.0" # Current MSRV
- stable
env:
RUST_BACKTRACE: 1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ This will make your function compile much faster.

## Supported Rust Versions (MSRV)

The AWS Lambda Rust Runtime requires a minimum of Rust 1.64, and is not guaranteed to build on compiler versions earlier than that.
The AWS Lambda Rust Runtime requires a minimum of Rust 1.65, and is not guaranteed to build on compiler versions earlier than that.

## Security

Expand Down
41 changes: 11 additions & 30 deletions lambda-runtime-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
//! the AWS Lambda Runtime API.
use http::{uri::PathAndQuery, uri::Scheme, Request, Response, Uri};
use hyper::body::Incoming;
use hyper_util::client::legacy::connect::{Connect, Connection, HttpConnector};
use hyper_util::client::legacy::connect::HttpConnector;
use std::{convert::TryInto, fmt::Debug};
use tower_service::Service;

const USER_AGENT_HEADER: &str = "User-Agent";
const DEFAULT_USER_AGENT: &str = concat!("aws-lambda-rust/", env!("CARGO_PKG_VERSION"));
Expand All @@ -20,27 +19,24 @@ pub mod body;

/// API client to interact with the AWS Lambda Runtime API.
#[derive(Debug)]
pub struct Client<C = HttpConnector> {
pub struct Client {
/// The runtime API URI
pub base: Uri,
/// The client that manages the API connections
pub client: hyper_util::client::legacy::Client<C, body::Body>,
pub client: hyper_util::client::legacy::Client<HttpConnector, body::Body>,
}

impl Client {
/// Create a builder struct to configure the client.
pub fn builder() -> ClientBuilder<HttpConnector> {
pub fn builder() -> ClientBuilder {
ClientBuilder {
connector: HttpConnector::new(),
uri: None,
}
}
}

impl<C> Client<C>
where
C: Connect + Sync + Send + Clone + 'static,
{
impl Client {
/// Send a given request to the Runtime API.
/// Use the client's base URI to ensure the API endpoint is correct.
pub async fn call(&self, req: Request<body::Body>) -> Result<Response<Incoming>, BoxError> {
Expand All @@ -49,7 +45,7 @@ where
}

/// Create a new client with a given base URI and HTTP connector.
pub fn with(base: Uri, connector: C) -> Self {
fn with(base: Uri, connector: HttpConnector) -> Self {
let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
.http1_max_buf_size(1024 * 1024)
.build(connector);
Expand Down Expand Up @@ -80,26 +76,14 @@ where
}

/// Builder implementation to construct any Runtime API clients.
pub struct ClientBuilder<C: Service<http::Uri> = HttpConnector> {
connector: C,
pub struct ClientBuilder {
connector: HttpConnector,
uri: Option<http::Uri>,
}

impl<C> ClientBuilder<C>
where
C: Service<http::Uri> + Clone + Send + Sync + Unpin + 'static,
<C as Service<http::Uri>>::Future: Unpin + Send,
<C as Service<http::Uri>>::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
<C as Service<http::Uri>>::Response: Connection + Unpin + Send + 'static,
{
impl ClientBuilder {
/// Create a new builder with a given HTTP connector.
pub fn with_connector<C2>(self, connector: C2) -> ClientBuilder<C2>
where
C2: Service<http::Uri> + Clone + Send + Sync + Unpin + 'static,
<C2 as Service<http::Uri>>::Future: Unpin + Send,
<C2 as Service<http::Uri>>::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
<C2 as Service<http::Uri>>::Response: Connection + Unpin + Send + 'static,
{
pub fn with_connector(self, connector: HttpConnector) -> ClientBuilder {
ClientBuilder {
connector,
uri: self.uri,
Expand All @@ -113,10 +97,7 @@ where
}

/// Create the new client to interact with the Runtime API.
pub fn build(self) -> Result<Client<C>, Error>
where
C: Connect + Sync + Send + Clone + 'static,
{
pub fn build(self) -> Result<Client, Error> {
let uri = match self.uri {
Some(uri) => uri,
None => {
Expand Down
3 changes: 2 additions & 1 deletion lambda-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tower = { workspace = true, features = ["util"] }
tracing = { version = "0.1", features = ["log"] }

[dev-dependencies]
httpmock = "0.7.0"
hyper-util = { workspace = true, features = [
"client",
"client-legacy",
Expand All @@ -59,4 +60,4 @@ hyper-util = { workspace = true, features = [
"server-auto",
"tokio",
] }
pin-project-lite = { workspace = true }
pin-project-lite = { workspace = true }
Loading

0 comments on commit 99eb031

Please sign in to comment.