mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 15:07:42 +02:00
added TestServer::client_headers (#2097)
Co-authored-by: fakeshadow <24548779@qq.com> Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
use actix::prelude::*;
|
||||
use actix_web::{test, web, App, HttpRequest};
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
test, web, App, HttpRequest, HttpResponse,
|
||||
};
|
||||
use actix_web_actors::*;
|
||||
use bytes::Bytes;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
@ -56,3 +59,51 @@ async fn test_simple() {
|
||||
let item = framed.next().await.unwrap().unwrap();
|
||||
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_with_credentials() {
|
||||
let mut srv = test::start(|| {
|
||||
App::new().service(web::resource("/").to(
|
||||
|req: HttpRequest, stream: web::Payload| async move {
|
||||
if req.headers().contains_key("Authorization") {
|
||||
ws::start(Ws, &req, stream)
|
||||
} else {
|
||||
Ok(HttpResponse::new(StatusCode::UNAUTHORIZED))
|
||||
}
|
||||
},
|
||||
))
|
||||
});
|
||||
|
||||
// client service without credentials
|
||||
match srv.ws().await {
|
||||
Ok(_) => panic!("WebSocket client without credentials should panic"),
|
||||
Err(awc::error::WsClientError::InvalidResponseStatus(status)) => {
|
||||
assert_eq!(status, StatusCode::UNAUTHORIZED)
|
||||
}
|
||||
Err(e) => panic!("Invalid error from WebSocket client: {}", e),
|
||||
}
|
||||
|
||||
let headers = srv.client_headers().unwrap();
|
||||
headers.insert(
|
||||
header::AUTHORIZATION,
|
||||
header::HeaderValue::from_static("Bearer Something"),
|
||||
);
|
||||
|
||||
// client service with credentials
|
||||
let client = srv.ws();
|
||||
|
||||
let mut framed = client.await.unwrap();
|
||||
|
||||
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
||||
|
||||
let item = framed.next().await.unwrap().unwrap();
|
||||
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
|
||||
|
||||
framed
|
||||
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let item = framed.next().await.unwrap().unwrap();
|
||||
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
|
||||
}
|
||||
|
Reference in New Issue
Block a user