Struct fdk::FunctionTestbench
[−]
[src]
pub struct FunctionTestbench<S: Sized> { /* fields omitted */ }
A testing wrapper that behaves like a Function
but additionally provides
methods to create testing conditions (including setting configuration and
enqueuing requests) and to read results back in order for tests to check
the behaviour of a function.
use std::process; fn code(_: &mut (), i: String) -> Result<String, fdk::FunctionError> { if !i.is_empty() { Ok(format!("Hello, {}!\n", i).into()) } else { Err(fdk::FunctionError::invalid_input( "Requires a non-empty UTF-8 string\n", )) } } fn main() { let exit_code = fdk::Function::new(fdk::STATELESS).run(code); process::exit(exit_code); } #[test] fn test_some_functionality() { let mut testbench = fdk::FunctionTestbench::new(fdk::STATELESS); // Enqueue a request: enqueue_simple() is a helper for simplicity, // but you can enqueue() a full hyper::Request if needed. testbench.enqueue_simple("Blah"); // Run the test let exit_code = testbench.run(code); assert_eq!(exit_code, 0); // Perform some checks on the responses let mut responses = testbench.drain_responses(); assert_eq!(responses.len(), 1); let rb = fdk::body_as_bytes(responses.pop().unwrap().body()).unwrap(); assert_eq!(String::from_utf8_lossy(&rb), "Hello, Blah!\n"); }
Methods
impl<S: Sized> FunctionTestbench<S>
[src]
fn new(
code: fn(_: &RuntimeContext) -> Result<S, FunctionError>
) -> FunctionTestbench<S>
[src]
code: fn(_: &RuntimeContext) -> Result<S, FunctionError>
) -> FunctionTestbench<S>
Create a FunctionTestbench
for a function with the provided
initializer.
fn set_config(&mut self, key: &str, value: &str)
[src]
Adds a configuration variable to the environment in which the function under test will run.
fn with_config(self, key: &str, value: &str) -> Self
[src]
Adds a configuration variable to the environment in which the function under test will run and return self. Used for the owned builder pattern.
fn enqueue(&mut self, req: Request) -> &mut Self
[src]
Enqueues a request (represented as a hyper::Request
) to the function.
fn enqueue_simple(&mut self, body: &str) -> &mut Self
[src]
Helper to enqueue a very simple GET request with a string body and the appropriate content length.
fn run<T, U>(
&mut self,
code: fn(_: &mut S, _: T) -> Result<U, FunctionError>
) -> i32 where
T: InputCoercible,
U: OutputCoercible,
[src]
&mut self,
code: fn(_: &mut S, _: T) -> Result<U, FunctionError>
) -> i32 where
T: InputCoercible,
U: OutputCoercible,
Runs the specified function code and stores the resulting responses. This clears the list of enqueued requests and overwrites the list of responses.
fn responses(&self) -> &Vec<Response>
[src]
Gets the list of responses that a function run has just produced.
fn drain_responses(&mut self) -> Vec<Response>
[src]
Drains the list of responses that a function run has just produced.
fn output(&self) -> &Vec<u8>
[src]
Gets the raw output of the function run (i.e. the serialized responses).
fn errlog(&self) -> &Vec<u8>
[src]
Gets the raw error log of the function run.