mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
first pass at Testing.
This commit is contained in:
10
examples/testing/Cargo.toml
Normal file
10
examples/testing/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "testing"
|
||||
version = "0.1.0"
|
||||
authors = ["Cameron Dershem <cameron@pinkhatbeard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
||||
bytes = "0.4"
|
22
examples/testing/src/integration_one.rs
Normal file
22
examples/testing/src/integration_one.rs
Normal file
@ -0,0 +1,22 @@
|
||||
// <integration-one>
|
||||
// use actix_web::test::TestServer;
|
||||
// use actix_web::HttpRequest;
|
||||
// use std::str;
|
||||
|
||||
// fn index(req: HttpRequest) -> &'static str {
|
||||
// "Hello world!"
|
||||
// }
|
||||
|
||||
// fn main() {
|
||||
// // start new test server
|
||||
// let mut srv = TestServer::new(|app| app.handler(index));
|
||||
|
||||
// let request = srv.get().finish().unwrap();
|
||||
// let response = srv.execute(request.send()).unwrap();
|
||||
// assert!(response.status().is_success());
|
||||
|
||||
// let bytes = srv.execute(response.body()).unwrap();
|
||||
// let body = str::from_utf8(&bytes).unwrap();
|
||||
// assert_eq!(body, "Hello world!");
|
||||
// }
|
||||
// </integration-one>
|
21
examples/testing/src/integration_three.rs
Normal file
21
examples/testing/src/integration_three.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// <integration-three>
|
||||
// #[test]
|
||||
// fn test() {
|
||||
// let srv = TestServer::build_with_state(|| {
|
||||
// // we can start diesel actors
|
||||
// let addr = SyncArbiter::start(3, || {
|
||||
// DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
||||
// });
|
||||
// // then we can construct custom state, or it could be `()`
|
||||
// MyState{addr: addr}
|
||||
// })
|
||||
|
||||
// // register server handlers and start test server
|
||||
// .start(|app| {
|
||||
// app.resource(
|
||||
// "/{username}/index.html", |r| r.with(
|
||||
// |p: Path<PParam>| format!("Welcome {}!", p.username)));
|
||||
// });
|
||||
// // now we can run our test code
|
||||
// );
|
||||
// </integration-three>
|
21
examples/testing/src/integration_two.rs
Normal file
21
examples/testing/src/integration_two.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// <integration-two>
|
||||
// use actix_web::{http, test, App, HttpRequest, HttpResponse};
|
||||
|
||||
// fn index(req: &HttpRequest) -> HttpResponse {
|
||||
// HttpResponse::Ok().into()
|
||||
// }
|
||||
|
||||
// /// This function get called by http server.
|
||||
// fn create_app() -> App {
|
||||
// App::new().resource("/test", |r| r.h(index))
|
||||
// }
|
||||
|
||||
// fn main() {
|
||||
// let mut srv = test::TestServer::with_factory(create_app);
|
||||
|
||||
// let request = srv.client(http::Method::GET, "/test").finish().unwrap();
|
||||
// let response = srv.execute(request.send()).unwrap();
|
||||
|
||||
// assert!(response.status().is_success());
|
||||
// }
|
||||
// </integration-two>
|
29
examples/testing/src/main.rs
Normal file
29
examples/testing/src/main.rs
Normal file
@ -0,0 +1,29 @@
|
||||
mod integration_one;
|
||||
mod integration_three;
|
||||
mod integration_two;
|
||||
mod stream_response;
|
||||
mod websockets;
|
||||
// <unit-tests>
|
||||
use actix_web::{http, test, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
if let Some(hdr) = req.headers().get(http::header::CONTENT_TYPE) {
|
||||
if let Ok(_s) = hdr.to_str() {
|
||||
return HttpResponse::Ok().into();
|
||||
}
|
||||
}
|
||||
HttpResponse::BadRequest().into()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let req =
|
||||
test::TestRequest::with_header("content-type", "text/plain").to_http_request();
|
||||
|
||||
let resp = test::block_on(index(req)).unwrap();
|
||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||
|
||||
let req = test::TestRequest::default().to_http_request();
|
||||
let resp = test::block_on(index(req)).unwrap();
|
||||
assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);
|
||||
}
|
||||
// </unit-tests>
|
50
examples/testing/src/stream_response.rs
Normal file
50
examples/testing/src/stream_response.rs
Normal file
@ -0,0 +1,50 @@
|
||||
// <stream-response>
|
||||
// use bytes::Bytes;
|
||||
// use futures::stream::poll_fn;
|
||||
// use futures::{Async, Poll, Stream};
|
||||
|
||||
// use actix_web::http::{ContentEncoding, StatusCode};
|
||||
// use actix_web::test::TestServer;
|
||||
// use actix_web::{Error, HttpRequest, HttpResponse};
|
||||
|
||||
// fn sse(_req: &HttpRequest) -> HttpResponse {
|
||||
// let mut counter = 5usize;
|
||||
// // yields `data: N` where N in [5; 1]
|
||||
// let server_events = poll_fn(move || -> Poll<Option<Bytes>, Error> {
|
||||
// if counter == 0 {
|
||||
// return Ok(Async::Ready(None));
|
||||
// }
|
||||
// let payload = format!("data: {}\n\n", counter);
|
||||
// counter -= 1;
|
||||
// Ok(Async::Ready(Some(Bytes::from(payload))))
|
||||
// });
|
||||
|
||||
// HttpResponse::build(StatusCode::OK)
|
||||
// .content_encoding(ContentEncoding::Identity)
|
||||
// .content_type("text/event-stream")
|
||||
// .streaming(server_events)
|
||||
// }
|
||||
|
||||
// fn main() {
|
||||
// // start new test server
|
||||
// let mut srv = TestServer::new(|app| app.handler(sse));
|
||||
|
||||
// // request stream
|
||||
// let request = srv.get().finish().unwrap();
|
||||
// let response = srv.execute(request.send()).unwrap();
|
||||
// assert!(response.status().is_success());
|
||||
|
||||
// // convert ClientResponse to future, start read body and wait first chunk
|
||||
// let mut stream = response.payload();
|
||||
// loop {
|
||||
// match srv.execute(stream.into_future()) {
|
||||
// Ok((Some(bytes), remain)) => {
|
||||
// println!("{:?}", bytes);
|
||||
// stream = remain
|
||||
// }
|
||||
// Ok((None, _)) => break,
|
||||
// Err(_) => panic!(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// </stream-response>
|
30
examples/testing/src/websockets.rs
Normal file
30
examples/testing/src/websockets.rs
Normal file
@ -0,0 +1,30 @@
|
||||
// <web-socket>
|
||||
use actix::{Actor, StreamHandler};
|
||||
use actix_web::*;
|
||||
use futures::Stream;
|
||||
|
||||
struct Ws; // <- WebSocket actor
|
||||
|
||||
impl Actor for Ws {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
||||
match msg {
|
||||
ws::Message::Text(text) => ctx.text(text),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
|
||||
|
||||
let (reader, mut writer) = srv.ws().unwrap();
|
||||
writer.text("text");
|
||||
|
||||
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
||||
assert_eq!(item, Some(ws::Message::Text("text".to_owned())));
|
||||
}
|
||||
// </web-socket>
|
Reference in New Issue
Block a user