pub struct ReplayResponse { /* private fields */ }Expand description
A canned response served by ReplayTransport.
The body arrives in the chunks it was built with: one for
new and json, one per event for
sse, the given ones for chunks.
split_every re-chunks any body, down to a byte at
a time, to test that a decoder does not depend on where the network
splits it.
Implementations§
Source§impl ReplayResponse
impl ReplayResponse
Sourcepub fn chunks(
status: u16,
chunks: impl IntoIterator<Item = impl Into<Bytes>>,
) -> Self
pub fn chunks( status: u16, chunks: impl IntoIterator<Item = impl Into<Bytes>>, ) -> Self
A response whose body arrives in chunks, in order: a stream body
in another framing than SSE (NDJSON lines), or chunk boundaries a
test places on purpose.
use allama_http::{HttpRequest, HttpTransport, ReplayResponse, ReplayTransport};
use futures_util::StreamExt;
let ndjson = ReplayResponse::chunks(200, ["{\"text\":\"Hel\"}\n{\"te", "xt\":\"lo\"}\n"])
.header("content-type", "application/x-ndjson");
let transport = ReplayTransport::new([ndjson]);
let response = transport.send(HttpRequest::get("https://x")).await.unwrap();
assert_eq!(response.into_body().count().await, 2);Sourcepub fn json(status: u16, body: impl Into<Bytes>) -> Self
pub fn json(status: u16, body: impl Into<Bytes>) -> Self
A JSON response with body as written: a captured body, or one
that is not valid JSON on purpose.
Sourcepub fn json_value(status: u16, value: &impl Serialize) -> Self
pub fn json_value(status: u16, value: &impl Serialize) -> Self
Sourcepub fn sse(body: impl AsRef<str>) -> Self
pub fn sse(body: impl AsRef<str>) -> Self
A 200 server-sent-events response, delivered one event per chunk
the way a network would split it.
Sourcepub fn ndjson(body: impl AsRef<str>) -> Self
pub fn ndjson(body: impl AsRef<str>) -> Self
A 200 newline-delimited JSON response, delivered one line per
chunk the way a network would split it: the NDJSON counterpart of
sse. Use chunks to place chunk
boundaries yourself, or another status.
use allama_http::{HttpRequest, HttpTransport, ReplayResponse, ReplayTransport};
use futures_util::StreamExt;
let transport = ReplayTransport::new([ReplayResponse::ndjson(
"{\"delta\":\"Hel\"}\n{\"delta\":\"lo\"}\n{\"done\":true}\n",
)]);
let response = transport.send(HttpRequest::get("https://x")).await.unwrap();
assert_eq!(response.headers.get("content-type"), Some("application/x-ndjson"));
assert_eq!(response.into_body().count().await, 3);Sourcepub fn failure(kind: TransportErrorKind, message: impl Into<String>) -> Self
pub fn failure(kind: TransportErrorKind, message: impl Into<String>) -> Self
A request that gets no response, failing with kind.
Sourcepub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
pub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
Add a response header.
Sourcepub fn split_every(self, size: usize) -> Self
pub fn split_every(self, size: usize) -> Self
Deliver the body in chunks of size bytes (the last one may be
shorter), whatever chunks it was built with: split_every(1) sends
one byte at a time, splitting every line ending and UTF-8 sequence.
use allama_http::{HttpRequest, HttpTransport, ReplayResponse, ReplayTransport};
use futures_util::StreamExt;
let transport = ReplayTransport::new([ReplayResponse::sse("data: hi\n\n").split_every(4)]);
let response = transport.send(HttpRequest::get("https://x")).await.unwrap();
assert_eq!(response.into_body().count().await, 3);§Panics
When size is 0.
Sourcepub fn then_stall(self) -> Self
pub fn then_stall(self) -> Self
Never end the body after its chunks, like a connection that stalls
mid-stream: for testing idle timeouts such as StreamTimeouts.