From 452a594ba4f7e1a7f3cb901c809ab01cff88ab8b Mon Sep 17 00:00:00 2001 From: NathanosDev Date: Wed, 20 Nov 2024 15:49:17 +0100 Subject: [PATCH] feat(ic-http-gateway): use bytes for request body --- examples/http-gateway/rust/src/main.rs | 2 +- packages/ic-http-gateway/src/protocol/handler.rs | 3 ++- .../src/request/http_gateway_request_builder.rs | 3 ++- packages/ic-http-gateway/tests/custom_assets.rs | 3 ++- .../ic-http-gateway/tests/protocol_error_tests.rs | 3 ++- .../ic-http-gateway/tests/range_request_stream.rs | 11 ++++++----- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/examples/http-gateway/rust/src/main.rs b/examples/http-gateway/rust/src/main.rs index 8589d62..d137975 100644 --- a/examples/http-gateway/rust/src/main.rs +++ b/examples/http-gateway/rust/src/main.rs @@ -71,7 +71,7 @@ fn main() { async move { let canister_request = Request::builder().uri(req.uri()).method(req.method()); - let collected_req = req.collect().await.unwrap().to_bytes().to_vec(); + let collected_req = req.collect().await.unwrap().to_bytes(); let canister_request = canister_request.body(collected_req).unwrap(); let gateway_response = http_gateway_clone diff --git a/packages/ic-http-gateway/src/protocol/handler.rs b/packages/ic-http-gateway/src/protocol/handler.rs index a948802..dd305e8 100644 --- a/packages/ic-http-gateway/src/protocol/handler.rs +++ b/packages/ic-http-gateway/src/protocol/handler.rs @@ -457,6 +457,7 @@ fn handle_agent_error(error: &AgentError) -> CanisterResponse { #[cfg(test)] mod tests { use super::*; + use bytes::Bytes; use http::Request; #[test] @@ -465,7 +466,7 @@ mod tests { .uri("http://example.com/foo/bar/baz?q=hello+world&t=1") .header("Accept", "text/html") .header("Accept-Encoding", "gzip, deflate, br, zstd") - .body(b"body".to_vec()) + .body(Bytes::from("body")) .unwrap(); let http_request = convert_request(request).unwrap(); diff --git a/packages/ic-http-gateway/src/request/http_gateway_request_builder.rs b/packages/ic-http-gateway/src/request/http_gateway_request_builder.rs index 31b7ef3..6e5bc70 100644 --- a/packages/ic-http-gateway/src/request/http_gateway_request_builder.rs +++ b/packages/ic-http-gateway/src/request/http_gateway_request_builder.rs @@ -1,4 +1,5 @@ use crate::{protocol::process_request, HttpGatewayResponse}; +use bytes::Bytes; use candid::Principal; use http::Request; use ic_agent::Agent; @@ -11,7 +12,7 @@ pub struct HttpGatewayRequestArgs { pub canister_id: Principal, } -pub type CanisterRequest = Request>; +pub type CanisterRequest = Request; pub struct HttpGatewayRequestBuilderArgs<'a> { pub request_args: HttpGatewayRequestArgs, diff --git a/packages/ic-http-gateway/tests/custom_assets.rs b/packages/ic-http-gateway/tests/custom_assets.rs index 3da065c..a94a2da 100644 --- a/packages/ic-http-gateway/tests/custom_assets.rs +++ b/packages/ic-http-gateway/tests/custom_assets.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use http::Request; use http_body_util::BodyExt; use ic_agent::Agent; @@ -36,7 +37,7 @@ fn test_custom_assets_index_html() { http_gateway .request(HttpGatewayRequestArgs { canister_id, - canister_request: Request::builder().uri("/").body(vec![]).unwrap(), + canister_request: Request::builder().uri("/").body(Bytes::new()).unwrap(), }) .send() .await diff --git a/packages/ic-http-gateway/tests/protocol_error_tests.rs b/packages/ic-http-gateway/tests/protocol_error_tests.rs index 1524ca0..96d28f8 100644 --- a/packages/ic-http-gateway/tests/protocol_error_tests.rs +++ b/packages/ic-http-gateway/tests/protocol_error_tests.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use http::{status::StatusCode, Request}; use ic_agent::{export::Principal, Agent}; use ic_http_gateway::{HttpGatewayClient, HttpGatewayRequestArgs}; @@ -71,7 +72,7 @@ async fn test_rate_limiting_error() -> Result<(), Box> { let canister_request = Request::builder() .uri("/example") .method("GET") - .body(Vec::::new()) + .body(Bytes::new()) .unwrap(); let gateway_response = http_gateway diff --git a/packages/ic-http-gateway/tests/range_request_stream.rs b/packages/ic-http-gateway/tests/range_request_stream.rs index d27e3ad..2d4d109 100644 --- a/packages/ic-http-gateway/tests/range_request_stream.rs +++ b/packages/ic-http-gateway/tests/range_request_stream.rs @@ -1,4 +1,5 @@ use assert_matches::assert_matches; +use bytes::Bytes; use http::Request; use http_body_util::BodyExt; use ic_agent::hash_tree::Hash; @@ -83,7 +84,7 @@ fn test_long_asset_request_yields_entire_asset(#[case] asset_name: &str) { canister_id, canister_request: Request::builder() .uri(format!("/{asset_name}")) - .body(vec![]) + .body(Bytes::new()) .unwrap(), }) .send() @@ -202,7 +203,7 @@ fn test_corrupted_long_asset_request_fails( corrupted_chunk_index.to_string(), ) .uri(format!("/{asset_name}")) - .body(vec![]) + .body(Bytes::new()) .unwrap(), }) .send() @@ -272,7 +273,7 @@ fn test_long_asset_with_chunks_out_of_order_fails( canister_request: Request::builder() .header("Test-SwapChunkAtIndexWithNext", chunk_to_swap.to_string()) .uri(format!("/{asset_name}")) - .body(vec![]) + .body(Bytes::new()) .unwrap(), }) .send() @@ -348,7 +349,7 @@ fn test_corrupted_chunk_certificate_for_long_asset_request_fails( corrupted_chunk_index.to_string(), ) .uri(format!("/{asset_name}")) - .body(vec![]) + .body(Bytes::new()) .unwrap(), }) .send() @@ -413,7 +414,7 @@ fn test_range_request_yields_range_response(#[case] asset_name: &str) { canister_request: Request::builder() .uri(format!("/{asset_name}")) .header("Range", format!("bytes={}-", ASSET_CHUNK_SIZE)) - .body(vec![]) + .body(Bytes::new()) .unwrap(), }) .send()