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]

[src]

Create a FunctionTestbench for a function with the provided initializer.

[src]

Adds a configuration variable to the environment in which the function under test will run.

[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.

[src]

Enqueues a request (represented as a hyper::Request) to the function.

[src]

Helper to enqueue a very simple GET request with a string body and the appropriate content length.

[src]

Runs the specified function code and stores the resulting responses. This clears the list of enqueued requests and overwrites the list of responses.

[src]

Gets the list of responses that a function run has just produced.

[src]

Drains the list of responses that a function run has just produced.

[src]

Gets the raw output of the function run (i.e. the serialized responses).

[src]

Gets the raw error log of the function run.