mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
add identity middleware tests
This commit is contained in:
parent
0f0d6b65ca
commit
b8829bbf22
@ -461,3 +461,69 @@ impl IdentityPolicy for CookieIdentityPolicy {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::http::StatusCode;
|
||||
use crate::test::{self, TestRequest};
|
||||
use crate::{web, App, HttpResponse};
|
||||
|
||||
#[test]
|
||||
fn test_identity() {
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.middleware(IdentityService::new(
|
||||
CookieIdentityPolicy::new(&[0; 32])
|
||||
.domain("www.rust-lang.org")
|
||||
.name("actix_auth")
|
||||
.path("/")
|
||||
.secure(true),
|
||||
))
|
||||
.service(web::resource("/index").to(|id: Identity| {
|
||||
if id.identity().is_some() {
|
||||
HttpResponse::Created()
|
||||
} else {
|
||||
HttpResponse::Ok()
|
||||
}
|
||||
}))
|
||||
.service(web::resource("/login").to(|id: Identity| {
|
||||
id.remember("test".to_string());
|
||||
HttpResponse::Ok()
|
||||
}))
|
||||
.service(web::resource("/logout").to(|id: Identity| {
|
||||
if id.identity().is_some() {
|
||||
id.forget();
|
||||
HttpResponse::Ok()
|
||||
} else {
|
||||
HttpResponse::BadRequest()
|
||||
}
|
||||
})),
|
||||
);
|
||||
let resp =
|
||||
test::call_success(&mut srv, TestRequest::with_uri("/index").to_request());
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let resp =
|
||||
test::call_success(&mut srv, TestRequest::with_uri("/login").to_request());
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
let c = resp.cookies().next().unwrap().to_owned();
|
||||
|
||||
let resp = test::call_success(
|
||||
&mut srv,
|
||||
TestRequest::with_uri("/index")
|
||||
.cookie(c.clone())
|
||||
.to_request(),
|
||||
);
|
||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||
|
||||
let resp = test::call_success(
|
||||
&mut srv,
|
||||
TestRequest::with_uri("/logout")
|
||||
.cookie(c.clone())
|
||||
.to_request(),
|
||||
);
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert!(resp.headers().contains_key(header::SET_COOKIE))
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ use actix_rt::Runtime;
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::{IntoNewService, NewService, Service};
|
||||
use bytes::Bytes;
|
||||
use cookie::Cookie;
|
||||
use futures::Future;
|
||||
|
||||
use crate::config::{AppConfig, AppConfigInner};
|
||||
@ -241,6 +242,12 @@ impl TestRequest {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set cookie for this request
|
||||
pub fn cookie<'a>(mut self, cookie: Cookie<'a>) -> Self {
|
||||
self.req.cookie(cookie);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set request payload
|
||||
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
||||
self.req.set_payload(data);
|
||||
|
Loading…
Reference in New Issue
Block a user