从上面的代码中可以看到 State 是 handler 函数中使用提取器获取的, 由于 State 不需要访问 Request 正文,那么从而 State 的提取器是实现了FormRequestParts, 上面 axum 源码中可以看出, 也许也会好奇, State 是如何绑定到 Request 上的, 在 Axum 或者是 Actix 状态的存储都是在 Request Extension 的 type map ...
("Body: {}", body);// Body: Missing request extension: Extension of type `fs::user::CurrentUser` was not found. Perhaps you forgot to add it? See `axum::Extension`.// Fails with 500 error.// assert_eq!(res.status(), StatusCode::OK);assert!(body.contains(DEV_TOKEN_UID))}//...
Router::with_statementionsExtensionas an alternative toState, but only after scrolling past 4 examples.extract::Statedoesn't mentionExtensionat all. extensions are more modular, as they can be added to various handlers while keeping other handlers pure....
.extension(hyper::ext::Protocol::from_static("websocket")) .uri("/echo") .header("sec-websocket-version", "13") .header("Host", "server.example.com") .body(Body::empty()) .unwrap(); let response = send_request.send_request(req).await.unwrap(); let status = response.status(); ...