Skip to content

Commit

Permalink
feat(ic-http-gateway): use bytes for request body (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev authored Nov 20, 2024
1 parent 2c5366b commit 192559e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/http-gateway/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/ic-http-gateway/src/protocol/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ fn handle_agent_error(error: &AgentError) -> CanisterResponse {
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
use http::Request;

#[test]
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{protocol::process_request, HttpGatewayResponse};
use bytes::Bytes;
use candid::Principal;
use http::Request;
use ic_agent::Agent;
Expand All @@ -11,7 +12,7 @@ pub struct HttpGatewayRequestArgs {
pub canister_id: Principal,
}

pub type CanisterRequest = Request<Vec<u8>>;
pub type CanisterRequest = Request<Bytes>;

pub struct HttpGatewayRequestBuilderArgs<'a> {
pub request_args: HttpGatewayRequestArgs,
Expand Down
3 changes: 2 additions & 1 deletion packages/ic-http-gateway/tests/custom_assets.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bytes::Bytes;
use http::Request;
use http_body_util::BodyExt;
use ic_agent::Agent;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/ic-http-gateway/tests/protocol_error_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bytes::Bytes;
use http::{status::StatusCode, Request};
use ic_agent::{export::Principal, Agent};
use ic_http_gateway::{HttpGatewayClient, HttpGatewayRequestArgs};
Expand Down Expand Up @@ -71,7 +72,7 @@ async fn test_rate_limiting_error() -> Result<(), Box<dyn std::error::Error>> {
let canister_request = Request::builder()
.uri("/example")
.method("GET")
.body(Vec::<u8>::new())
.body(Bytes::new())
.unwrap();

let gateway_response = http_gateway
Expand Down
11 changes: 6 additions & 5 deletions packages/ic-http-gateway/tests/range_request_stream.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 192559e

Please sign in to comment.