Expand description
Actix middleware that computes strong ETags for responses and enforces
conditional request semantics for If-Match and If-None-Match headers.
Wrap your Actix App with ETag to automatically add ETag headers to
successful responses and to short-circuit requests when the client’s cached
representation is still current. The middleware emits strong ETags by
default; call ETag::weak when you need weak validators instead.
§Examples
use actix_web::{web, App, HttpResponse, test, dev::Service};
use etag_actix_middleware::ETag;
let mut app = test::init_service(
App::new()
.wrap(ETag::strong())
.route("/", web::get().to(|| async { HttpResponse::Ok().body("hello") }))
).await;
let response = test::call_service(&mut app, test::TestRequest::get().uri("/").to_request()).await;
assert_eq!(response.status(), actix_web::http::StatusCode::OK);
assert!(response.headers().contains_key(actix_web::http::header::ETAG));use actix_web::{web, App, HttpResponse, test, dev::Service};
use etag_actix_middleware::ETag;
let mut app = test::init_service(
App::new()
.wrap(ETag::weak())
.route("/", web::get().to(|| async { HttpResponse::Ok().body("hello") }))
).await;
// First response provides the current weak ETag.
let initial = test::call_service(&mut app, test::TestRequest::get().uri("/").to_request()).await;
let etag = initial.headers().get(actix_web::http::header::ETAG).unwrap().clone();
// Revalidation request with If-None-Match short-circuits to 304 Not Modified.
let request = test::TestRequest::get()
.uri("/")
.insert_header((actix_web::http::header::IF_NONE_MATCH, etag))
.to_request();
let response = test::call_service(&mut app, request).await;
assert_eq!(response.status(), actix_web::http::StatusCode::NOT_MODIFIED);Structs§
- ETag
- Middleware that injects ETag headers and evaluates conditional requests.
- ETag
Middleware - Internal service wrapper that materializes response bodies before hashing.