2017-12-27 02:14:37 +01:00
|
|
|
# Testing
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
Every application should be well tested. Actix provides tools to perform unit and
|
2017-12-27 02:14:37 +01:00
|
|
|
integration tests.
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
## Unit tests
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
For unit testing actix provides a request builder type and simple handler runner.
|
|
|
|
[*TestRequest*](../actix_web/test/struct.TestRequest.html) implements a builder-like pattern.
|
|
|
|
You can generate a `HttpRequest` instance with `finish()` or you can
|
|
|
|
run your handler with `run()` or `run_async()`.
|
2017-12-27 04:48:02 +01:00
|
|
|
|
|
|
|
```rust
|
|
|
|
# extern crate http;
|
|
|
|
# extern crate actix_web;
|
|
|
|
use http::{header, StatusCode};
|
|
|
|
use actix_web::*;
|
|
|
|
use actix_web::test::TestRequest;
|
|
|
|
|
|
|
|
fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
|
|
|
|
if let Ok(s) = hdr.to_str() {
|
2018-03-02 04:12:59 +01:00
|
|
|
return httpcodes::HttpOk.into()
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 04:12:59 +01:00
|
|
|
httpcodes::HttpBadRequest.into()
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let resp = TestRequest::with_header("content-type", "text/plain")
|
|
|
|
.run(index)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let resp = TestRequest::default()
|
|
|
|
.run(index)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-12-27 02:14:37 +01:00
|
|
|
## Integration tests
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
There are several methods how you can test your application. Actix provides
|
2017-12-27 02:14:37 +01:00
|
|
|
[*TestServer*](../actix_web/test/struct.TestServer.html)
|
2018-03-28 22:16:01 +02:00
|
|
|
server that can be used to run the whole application of just specific handlers
|
|
|
|
in real http server. *TestServer::get()*, *TestServer::post()* or *TestServer::client()*
|
|
|
|
methods can be used to send requests to the test server.
|
2017-12-27 02:14:37 +01:00
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
In simple form *TestServer* can be configured to use handler. *TestServer::new* method
|
2017-12-27 02:14:37 +01:00
|
|
|
accepts configuration function, only argument for this function is *test application*
|
2018-03-28 22:16:01 +02:00
|
|
|
instance. You can check the [api documentation](../actix_web/test/struct.TestApp.html)
|
2017-12-27 02:14:37 +01:00
|
|
|
for more information.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
use actix_web::*;
|
|
|
|
use actix_web::test::TestServer;
|
|
|
|
|
|
|
|
fn index(req: HttpRequest) -> HttpResponse {
|
2018-03-02 04:12:59 +01:00
|
|
|
httpcodes::HttpOk.into()
|
2017-12-27 02:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-02-19 22:18:18 +01:00
|
|
|
let mut srv = TestServer::new(|app| app.handler(index)); // <- Start new test server
|
2018-03-02 04:12:59 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let request = srv.get().finish().unwrap(); // <- create client request
|
|
|
|
let response = srv.execute(request.send()).unwrap(); // <- send request to the server
|
|
|
|
assert!(response.status().is_success()); // <- check response
|
2018-03-02 04:12:59 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let bytes = srv.execute(response.body()).unwrap(); // <- read response body
|
2017-12-27 02:14:37 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
The other option is to use an application factory. In this case you need to pass the factory
|
|
|
|
function same way as you would for real http server configuration.
|
2017-12-27 02:14:37 +01:00
|
|
|
|
|
|
|
```rust
|
2018-02-19 22:18:18 +01:00
|
|
|
# extern crate http;
|
2017-12-27 02:14:37 +01:00
|
|
|
# extern crate actix_web;
|
2018-02-19 22:18:18 +01:00
|
|
|
use http::Method;
|
2017-12-27 02:14:37 +01:00
|
|
|
use actix_web::*;
|
|
|
|
use actix_web::test::TestServer;
|
|
|
|
|
|
|
|
fn index(req: HttpRequest) -> HttpResponse {
|
2018-03-02 04:12:59 +01:00
|
|
|
httpcodes::HttpOk.into()
|
2017-12-27 02:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function get called by http server.
|
|
|
|
fn create_app() -> Application {
|
|
|
|
Application::new()
|
|
|
|
.resource("/test", |r| r.h(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-02-19 22:18:18 +01:00
|
|
|
let mut srv = TestServer::with_factory(create_app); // <- Start new test server
|
|
|
|
|
|
|
|
let request = srv.client(Method::GET, "/test").finish().unwrap(); // <- create client request
|
|
|
|
let response = srv.execute(request.send()).unwrap(); // <- send request to the server
|
|
|
|
|
|
|
|
assert!(response.status().is_success()); // <- check response
|
2017-12-27 02:14:37 +01:00
|
|
|
}
|
|
|
|
```
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
## WebSocket server tests
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
It is possible to register a *handler* with `TestApp::handler()` that
|
|
|
|
initiates a web socket connection. *TestServer* provides `ws()` which connects to
|
|
|
|
the websocket server and returns ws reader and writer objects. *TestServer* also
|
|
|
|
provides an `execute()` method which runs future objects to completion and returns
|
2018-01-31 00:13:33 +01:00
|
|
|
result of the future computation.
|
|
|
|
|
2018-03-28 22:16:01 +02:00
|
|
|
Here is a simple example that shows how to test server websocket handler.
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
```rust
|
|
|
|
# extern crate actix;
|
|
|
|
# extern crate actix_web;
|
|
|
|
# extern crate futures;
|
|
|
|
# extern crate http;
|
|
|
|
# extern crate bytes;
|
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use futures::Stream;
|
|
|
|
# use actix::prelude::*;
|
|
|
|
|
|
|
|
struct Ws; // <- WebSocket actor
|
|
|
|
|
|
|
|
impl Actor for Ws {
|
|
|
|
type Context = ws::WebsocketContext<Self>;
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:01:07 +02:00
|
|
|
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
2018-01-31 00:13:33 +01:00
|
|
|
|
|
|
|
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
|
|
|
match msg {
|
2018-02-10 09:05:20 +01:00
|
|
|
ws::Message::Text(text) => ctx.text(text),
|
2018-01-31 00:13:33 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut srv = test::TestServer::new( // <- start our server with ws handler
|
|
|
|
|app| app.handler(|req| ws::start(req, Ws)));
|
|
|
|
|
|
|
|
let (reader, mut writer) = srv.ws().unwrap(); // <- connect to ws server
|
|
|
|
|
|
|
|
writer.text("text"); // <- send message to server
|
2018-03-28 22:16:01 +02:00
|
|
|
|
2018-01-31 00:13:33 +01:00
|
|
|
let (item, reader) = srv.execute(reader.into_future()).unwrap(); // <- wait for one message
|
|
|
|
assert_eq!(item, Some(ws::Message::Text("text".to_owned())));
|
|
|
|
}
|
|
|
|
```
|