mini-serve 0.13.12

An HTTP server: trie router, middleware, CORS, optional TLS. Built on hyper + tokio.
Documentation
use super::*;
use crate::router::PathParams;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
struct Item {
	id: u64,
}

fn request_with_params(params: HashMap<String, String>) -> Request<()> {
	Request::builder()
		.extension(PathParams(params))
		.body(())
		.unwrap()
}

#[test]
fn extracts_typed_numeric_field() {
	let mut params = HashMap::new();
	params.insert("id".to_string(), "42".to_string());
	let req = request_with_params(params);

	let item: Item = path_params(&req).unwrap();
	assert_eq!(item.id, 42);
}

#[test]
fn unparseable_segment_returns_400() {
	let mut params = HashMap::new();
	params.insert("id".to_string(), "not-a-number".to_string());
	let req = request_with_params(params);

	let err = path_params::<Item, _>(&req).unwrap_err();
	assert_eq!(err.code, 400);
	assert_eq!(err.message, "invalid path parameters");
}

/// Absent `PathParams` used to be a `500`, on the reasoning that the router always
/// inserts them so their absence meant the server was broken. The router no longer
/// inserts an empty map — a route that captures nothing has nothing to insert — so
/// absence now means "this route has no params", which is a `400`: the handler asked
/// its own route a question the route cannot answer. Same answer as a param that
/// failed to parse, and for the same reason.
#[test]
fn no_params_captured_returns_400() {
	let req = Request::builder().body(()).unwrap();
	let err = path_params::<Item, _>(&req).unwrap_err();
	assert_eq!(err.code, 400);
	assert_eq!(err.message, "invalid path parameters");
}