From bc28015d3e0fa796bf5e6d767a5d2f114ee2b9fd Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 14 Oct 2023 16:10:15 +0800 Subject: [PATCH] Add trillium::Conn::response_body() to borrow the response body trillium_http::Conn supports returning a borrow of the response body, but trillium:Conn doesn't have a corresponding method. This is useful for examining a response body non-destructively without having to take it and re-set it afterwards. --- trillium/src/conn.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/trillium/src/conn.rs b/trillium/src/conn.rs index da7a4cc324..71c3245f2e 100644 --- a/trillium/src/conn.rs +++ b/trillium/src/conn.rs @@ -204,6 +204,24 @@ impl Conn { self.inner.take_response_body() } + /** + Borrows the response body from the `Conn` + + ``` + use trillium_testing::prelude::*; + let mut conn = get("/").on(&()); + + conn.set_body("hello"); + let body = conn.response_body().unwrap(); + assert_eq!(body.len(), Some(5)); + assert!(body.is_static()); + assert_eq!(body.static_bytes(), Some(&b"hello"[..])); + ``` + */ + pub fn response_body(&self) -> Option<&Body> { + self.inner.response_body() + } + /** Attempts to retrieve a &T from the state set