mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
fix redis session tests
This commit is contained in:
parent
ddfc2b5ffd
commit
1756cc336a
@ -22,7 +22,7 @@ async fn test() {
|
||||
.await
|
||||
.expect("drop collection should succeed");
|
||||
|
||||
let mut app = init_service(
|
||||
let app = init_service(
|
||||
App::new()
|
||||
.app_data(web::Data::new(client))
|
||||
.service(add_user)
|
||||
@ -42,13 +42,13 @@ async fn test() {
|
||||
.set_form(&user)
|
||||
.to_request();
|
||||
|
||||
let response = call_and_read_body(&mut app, req).await;
|
||||
let response = call_and_read_body(&app, req).await;
|
||||
assert_eq!(response, Bytes::from_static(b"user added"));
|
||||
|
||||
let req = TestRequest::get()
|
||||
.uri(&format!("/get_user/{}", &user.username))
|
||||
.to_request();
|
||||
|
||||
let response: User = call_and_read_body_json(&mut app, req).await;
|
||||
let response: User = call_and_read_body_json(&app, req).await;
|
||||
assert_eq!(response, user);
|
||||
}
|
||||
|
@ -109,41 +109,11 @@ mod test {
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_workflow() {
|
||||
// Step 1: GET index
|
||||
// - set-cookie actix-session will be in response (session cookie #1)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
// Step 2: GET index, including session cookie #1 in request
|
||||
// - set-cookie will *not* be in response
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
// Step 3: POST to do_something, including session cookie #1 in request
|
||||
// - adds new session state in redis: {"counter": 1}
|
||||
// - response should be: {"counter": 1, "user_id": None}
|
||||
// Step 4: POST again to do_something, including session cookie #1 in request
|
||||
// - updates session state in redis: {"counter": 2}
|
||||
// - response should be: {"counter": 2, "user_id": None}
|
||||
// Step 5: POST to login, including session cookie #1 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #2)
|
||||
// - updates session state in redis: {"counter": 2, "user_id": "ferris"}
|
||||
// Step 6: GET index, including session cookie #2 in request
|
||||
// - response should be: {"counter": 2, "user_id": "ferris"}
|
||||
// Step 7: POST again to do_something, including session cookie #2 in request
|
||||
// - updates session state in redis: {"counter": 3, "user_id": "ferris"}
|
||||
// - response should be: {"counter": 2, "user_id": None}
|
||||
// Step 8: GET index, including session cookie #1 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #3)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
// Step 9: POST to logout, including session cookie #2
|
||||
// - set-cookie actix-session will be in response with session cookie #2
|
||||
// invalidation logic
|
||||
// Step 10: GET index, including session cookie #2 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #3)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
|
||||
let private_key = actix_web::cookie::Key::generate();
|
||||
let srv = actix_test::start(move || {
|
||||
App::new()
|
||||
.wrap(
|
||||
RedisSession::new("127.0.0.1:6379", &private_key.master())
|
||||
RedisSession::new("127.0.0.1:6379", private_key.master())
|
||||
.cookie_name("test-session"),
|
||||
)
|
||||
.wrap(middleware::Logger::default())
|
||||
@ -154,17 +124,11 @@ mod test {
|
||||
});
|
||||
|
||||
// Step 1: GET index
|
||||
// - set-cookie actix-session will be in response (session cookie #1)
|
||||
// - set-cookie actix-session should NOT be in response (session data is empty)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
let request = srv.get("/").send();
|
||||
let mut resp_1 = request.await.unwrap();
|
||||
let cookie_1 = resp_1
|
||||
.cookies()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|c| c.name() == "test-session")
|
||||
.unwrap();
|
||||
assert!(resp_1.cookies().unwrap().is_empty());
|
||||
let result_1 = resp_1.json::<IndexResponse>().await.unwrap();
|
||||
assert_eq!(
|
||||
result_1,
|
||||
@ -174,24 +138,18 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 2: GET index, including session cookie #1 in request
|
||||
// - set-cookie will *not* be in response
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
let req_2 = srv.get("/").cookie(cookie_1.clone()).send();
|
||||
let resp_2 = req_2.await.unwrap();
|
||||
let cookie_2 = resp_2
|
||||
// Step 2: POST to do_something, including session cookie #1 in request
|
||||
// - adds new session state in redis: {"counter": 1}
|
||||
// - response should be: {"counter": 1, "user_id": None}
|
||||
let req_3 = srv.post("/do_something").send();
|
||||
let mut resp_3 = req_3.await.unwrap();
|
||||
let cookie_1 = resp_3
|
||||
.cookies()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|c| c.name() == "test-session");
|
||||
assert_eq!(cookie_2, None);
|
||||
|
||||
// Step 3: POST to do_something, including session cookie #1 in request
|
||||
// - adds new session state in redis: {"counter": 1}
|
||||
// - response should be: {"counter": 1, "user_id": None}
|
||||
let req_3 = srv.post("/do_something").cookie(cookie_1.clone()).send();
|
||||
let mut resp_3 = req_3.await.unwrap();
|
||||
.find(|c| c.name() == "test-session")
|
||||
.unwrap();
|
||||
let result_3 = resp_3.json::<IndexResponse>().await.unwrap();
|
||||
assert_eq!(
|
||||
result_3,
|
||||
@ -201,7 +159,7 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 4: POST again to do_something, including session cookie #1 in request
|
||||
// Step 3: POST again to do_something, including session cookie #1 in request
|
||||
// - updates session state in redis: {"counter": 2}
|
||||
// - response should be: {"counter": 2, "user_id": None}
|
||||
let req_4 = srv.post("/do_something").cookie(cookie_1.clone()).send();
|
||||
@ -215,7 +173,7 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 5: POST to login, including session cookie #1 in request
|
||||
// Step 4: POST to login, including session cookie #1 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #2)
|
||||
// - updates session state in redis: {"counter": 2, "user_id": "ferris"}
|
||||
let req_5 = srv
|
||||
@ -241,7 +199,7 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 6: GET index, including session cookie #2 in request
|
||||
// Step 5: GET index, including session cookie #2 in request
|
||||
// - response should be: {"counter": 2, "user_id": "ferris"}
|
||||
let req_6 = srv.get("/").cookie(cookie_2.clone()).send();
|
||||
let mut resp_6 = req_6.await.unwrap();
|
||||
@ -254,7 +212,7 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 7: POST again to do_something, including session cookie #2 in request
|
||||
// Step 6: POST again to do_something, including session cookie #2 in request
|
||||
// - updates session state in redis: {"counter": 3, "user_id": "ferris"}
|
||||
// - response should be: {"counter": 2, "user_id": None}
|
||||
let req_7 = srv.post("/do_something").cookie(cookie_2.clone()).send();
|
||||
@ -268,18 +226,12 @@ mod test {
|
||||
}
|
||||
);
|
||||
|
||||
// Step 8: GET index, including session cookie #1 in request
|
||||
// Step 7: GET index, including session cookie #1 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #3)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
let req_8 = srv.get("/").cookie(cookie_1.clone()).send();
|
||||
let mut resp_8 = req_8.await.unwrap();
|
||||
let cookie_3 = resp_8
|
||||
.cookies()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|c| c.name() == "test-session")
|
||||
.unwrap();
|
||||
assert!(resp_8.cookies().unwrap().is_empty());
|
||||
let result_8 = resp_8.json::<IndexResponse>().await.unwrap();
|
||||
assert_eq!(
|
||||
result_8,
|
||||
@ -288,9 +240,8 @@ mod test {
|
||||
counter: 0
|
||||
}
|
||||
);
|
||||
assert_ne!(cookie_3.value(), cookie_2.value());
|
||||
|
||||
// Step 9: POST to logout, including session cookie #2
|
||||
// Step 8: POST to logout, including session cookie #2
|
||||
// - set-cookie actix-session will be in response with session cookie #2
|
||||
// invalidation logic
|
||||
let req_9 = srv.post("/logout").cookie(cookie_2.clone()).send();
|
||||
@ -309,7 +260,7 @@ mod test {
|
||||
cookie_4.expires().unwrap().datetime().unwrap().year()
|
||||
);
|
||||
|
||||
// Step 10: GET index, including session cookie #2 in request
|
||||
// Step 9: GET index, including session cookie #2 in request
|
||||
// - set-cookie actix-session will be in response (session cookie #3)
|
||||
// - response should be: {"counter": 0, "user_id": None}
|
||||
let req_10 = srv.get("/").cookie(cookie_2.clone()).send();
|
||||
@ -322,14 +273,5 @@ mod test {
|
||||
counter: 0
|
||||
}
|
||||
);
|
||||
|
||||
let cookie_5 = resp_10
|
||||
.cookies()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|c| c.name() == "test-session")
|
||||
.unwrap();
|
||||
assert_ne!(cookie_5.value(), cookie_2.value());
|
||||
}
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ mod test {
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test() {
|
||||
let mut app = atest::init_service(App::new().service(index)).await;
|
||||
let app = atest::init_service(App::new().service(index)).await;
|
||||
|
||||
let req = atest::TestRequest::with_uri("/").to_request();
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
@ -60,7 +60,7 @@ mod test {
|
||||
bytes,
|
||||
Bytes::from_static(
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
|
||||
web</title></head><body><h1 id=\"welcome\" \
|
||||
Web</title></head><body><h1 id=\"welcome\" \
|
||||
class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
|
||||
<input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
|
||||
name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
|
||||
@ -69,7 +69,7 @@ mod test {
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
@ -90,7 +90,7 @@ mod test {
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_server_error());
|
||||
|
||||
@ -99,7 +99,7 @@ mod test {
|
||||
assert_eq!(bytes, Bytes::from_static("Bad query".as_ref()));
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
let resp = atest::call_service(&app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user