mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-28 01:52:57 +01:00
add identity middleware tests
This commit is contained in:
parent
0f0d6b65ca
commit
b8829bbf22
@ -461,3 +461,69 @@ impl IdentityPolicy for CookieIdentityPolicy {
|
|||||||
Ok(())
|
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_server_config::ServerConfig;
|
||||||
use actix_service::{IntoNewService, NewService, Service};
|
use actix_service::{IntoNewService, NewService, Service};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use cookie::Cookie;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppConfigInner};
|
use crate::config::{AppConfig, AppConfigInner};
|
||||||
@ -241,6 +242,12 @@ impl TestRequest {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set cookie for this request
|
||||||
|
pub fn cookie<'a>(mut self, cookie: Cookie<'a>) -> Self {
|
||||||
|
self.req.cookie(cookie);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set request payload
|
/// Set request payload
|
||||||
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
||||||
self.req.set_payload(data);
|
self.req.set_payload(data);
|
||||||
|
Loading…
Reference in New Issue
Block a user