1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/tests/test_handlers.rs

678 lines
20 KiB
Rust
Raw Normal View History

2018-03-28 23:24:32 +02:00
extern crate actix;
extern crate actix_web;
2018-04-14 01:02:01 +02:00
extern crate bytes;
2018-03-28 23:24:32 +02:00
extern crate futures;
extern crate h2;
extern crate http;
2018-05-25 06:03:16 +02:00
extern crate tokio_timer;
2018-04-14 01:02:01 +02:00
#[macro_use]
extern crate serde_derive;
2018-05-07 00:11:36 +02:00
extern crate serde_json;
2018-03-28 23:24:32 +02:00
use std::io;
2018-05-25 06:03:16 +02:00
use std::time::{Duration, Instant};
2018-05-07 00:11:36 +02:00
2018-03-28 23:24:32 +02:00
use actix_web::*;
use bytes::Bytes;
2018-05-07 00:11:36 +02:00
use futures::Future;
2018-03-28 23:24:32 +02:00
use http::StatusCode;
2018-05-07 00:11:36 +02:00
use serde_json::Value;
2018-05-25 06:03:16 +02:00
use tokio_timer::Delay;
2018-03-28 23:24:32 +02:00
#[derive(Deserialize)]
struct PParam {
username: String,
}
#[test]
fn test_path_extractor() {
let mut srv = test::TestServer::new(|app| {
2018-04-14 01:02:01 +02:00
app.resource("/{username}/index.html", |r| {
r.with(|p: Path<PParam>| format!("Welcome {}!", p.username))
});
});
2018-03-28 23:24:32 +02:00
// client request
2018-05-17 21:20:20 +02:00
let request = srv.get().uri(srv.url("/test/index.html")).finish().unwrap();
2018-03-28 23:24:32 +02:00
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
}
2018-06-21 07:20:21 +02:00
#[test]
fn test_async_handler() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with(|p: Path<PParam>| {
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| Ok(format!("Welcome {}!", p.username)))
.responder()
})
});
});
// client request
let request = srv.get().uri(srv.url("/test/index.html")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
}
2018-03-28 23:24:32 +02:00
#[test]
fn test_query_extractor() {
let mut srv = test::TestServer::new(|app| {
2018-04-14 01:02:01 +02:00
app.resource("/index.html", |r| {
r.with(|p: Query<PParam>| format!("Welcome {}!", p.username))
});
});
2018-03-28 23:24:32 +02:00
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/index.html?username=test"))
.finish()
.unwrap();
2018-03-28 23:24:32 +02:00
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
// client request
2018-05-17 21:20:20 +02:00
let request = srv.get().uri(srv.url("/index.html")).finish().unwrap();
2018-03-28 23:24:32 +02:00
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[derive(Deserialize, Debug)]
pub enum ResponseType {
Token,
2018-07-16 07:17:45 +02:00
Code,
}
#[derive(Debug, Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: ResponseType,
}
#[test]
fn test_query_enum_extractor() {
let mut srv = test::TestServer::new(|app| {
app.resource("/index.html", |r| {
r.with(|p: Query<AuthRequest>| format!("{:?}", p.into_inner()))
});
});
// client request
let request = srv
.get()
.uri(srv.url("/index.html?id=64&response_type=Code"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-07-16 07:17:45 +02:00
assert_eq!(
bytes,
Bytes::from_static(b"AuthRequest { id: 64, response_type: Code }")
);
2018-07-16 07:17:45 +02:00
let request = srv
.get()
.uri(srv.url("/index.html?id=64&response_type=Co"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
2018-07-16 07:17:45 +02:00
let request = srv
.get()
.uri(srv.url("/index.html?response_type=Code"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
2018-05-07 05:05:31 +02:00
#[test]
fn test_async_extractor_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with(|data: Json<Value>| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 05:05:31 +02:00
.and_then(move |_| Ok(format!("{}", data.0)))
.responder()
})
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 05:05:31 +02:00
.uri(srv.url("/test1/index.html"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"{\"test\":1}"));
}
2018-06-12 21:42:15 +02:00
#[derive(Deserialize, Serialize)]
struct FormData {
username: String,
}
#[test]
fn test_form_extractor() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-14 08:37:19 +02:00
r.route()
.with(|form: Form<FormData>| format!("{}", form.username))
});
});
// client request
let request = srv
.post()
.uri(srv.url("/test1/index.html"))
2018-06-14 08:37:19 +02:00
.form(FormData {
username: "test".to_string(),
2018-08-23 18:48:01 +02:00
}).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"test"));
}
2018-06-13 00:33:28 +02:00
#[test]
fn test_form_extractor2() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-21 08:04:00 +02:00
r.route().with_config(
|form: Form<FormData>| format!("{}", form.username),
|cfg| {
cfg.0.error_handler(|err, _| {
2018-06-21 08:04:00 +02:00
error::InternalError::from_response(
err,
HttpResponse::Conflict().finish(),
).into()
});
},
);
2018-06-13 00:33:28 +02:00
});
});
// client request
let request = srv
.post()
.uri(srv.url("/test1/index.html"))
.header("content-type", "application/x-www-form-urlencoded")
.body("918237129hdk:D:D:D:D:D:DjASHDKJhaswkjeq")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_client_error());
}
#[test]
fn test_path_and_query_extractor() {
let mut srv = test::TestServer::new(|app| {
2018-04-14 01:02:01 +02:00
app.resource("/{username}/index.html", |r| {
2018-06-01 19:54:23 +02:00
r.route().with(|(p, q): (Path<PParam>, Query<PParam>)| {
2018-04-14 01:02:01 +02:00
format!("Welcome {} - {}!", p.username, q.username)
})
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn test_path_and_query_extractor2() {
let mut srv = test::TestServer::new(|app| {
2018-04-14 01:02:01 +02:00
app.resource("/{username}/index.html", |r| {
2018-04-29 18:09:08 +02:00
r.route()
2018-06-01 19:54:23 +02:00
.with(|(_r, p, q): (HttpRequest, Path<PParam>, Query<PParam>)| {
2018-04-29 18:09:08 +02:00
format!("Welcome {} - {}!", p.username, q.username)
})
2018-04-14 01:02:01 +02:00
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
2018-05-07 00:11:36 +02:00
#[test]
fn test_path_and_query_extractor2_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-01 19:54:23 +02:00
r.route().with(
|(p, _q, data): (Path<PParam>, Query<PParam>, Json<Value>)| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 00:11:36 +02:00
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
2018-08-23 18:48:01 +02:00
}).responder()
2018-06-01 19:54:23 +02:00
},
)
2018-05-07 00:11:36 +02:00
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 00:11:36 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-05-17 21:20:20 +02:00
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - {\"test\":1}!"));
2018-05-07 00:11:36 +02:00
}
#[test]
fn test_path_and_query_extractor3_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-01 19:54:23 +02:00
r.route().with(|(p, data): (Path<PParam>, Json<Value>)| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 00:11:36 +02:00
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
2018-08-23 18:48:01 +02:00
}).responder()
2018-05-07 00:11:36 +02:00
})
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 00:11:36 +02:00
.uri(srv.url("/test1/index.html"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
}
#[test]
fn test_path_and_query_extractor4_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-01 19:54:23 +02:00
r.route().with(|(data, p): (Json<Value>, Path<PParam>)| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 00:11:36 +02:00
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
2018-08-23 18:48:01 +02:00
}).responder()
2018-05-07 00:11:36 +02:00
})
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 00:11:36 +02:00
.uri(srv.url("/test1/index.html"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
}
#[test]
fn test_path_and_query_extractor2_async2() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-06-01 19:54:23 +02:00
r.route().with(
|(p, data, _q): (Path<PParam>, Json<Value>, Query<PParam>)| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 00:11:36 +02:00
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
2018-08-23 18:48:01 +02:00
}).responder()
2018-06-01 19:54:23 +02:00
},
)
2018-05-07 00:11:36 +02:00
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 00:11:36 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-05-17 21:20:20 +02:00
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - {\"test\":1}!"));
2018-05-07 00:11:36 +02:00
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-05-07 00:11:36 +02:00
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
2018-05-07 05:05:31 +02:00
#[test]
fn test_path_and_query_extractor2_async3() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
2018-08-23 18:48:01 +02:00
r.route()
.with(|data: Json<Value>, p: Path<PParam>, _: Query<PParam>| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 05:05:31 +02:00
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
2018-08-23 18:48:01 +02:00
}).responder()
})
2018-05-07 05:05:31 +02:00
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 05:05:31 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-05-17 21:20:20 +02:00
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - {\"test\":1}!"));
2018-05-07 05:05:31 +02:00
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-05-07 05:05:31 +02:00
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn test_path_and_query_extractor2_async4() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route()
.with(|data: (Json<Value>, Path<PParam>, Query<PParam>)| {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
2018-05-07 05:05:31 +02:00
.and_then(move |_| {
2018-05-17 21:20:20 +02:00
Ok(format!("Welcome {} - {}!", data.1.username, (data.0).0))
2018-08-23 18:48:01 +02:00
}).responder()
2018-05-07 05:05:31 +02:00
})
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
2018-05-07 05:05:31 +02:00
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-05-17 21:20:20 +02:00
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - {\"test\":1}!"));
2018-05-07 05:05:31 +02:00
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-05-07 05:05:31 +02:00
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
2018-05-21 02:04:08 +02:00
#[test]
fn test_scope_and_path_extractor() {
let mut srv = test::TestServer::with_factory(move || {
App::new().scope("/sc", |scope| {
scope.resource("/{num}/index.html", |r| {
r.route()
.with(|p: Path<(usize,)>| format!("Welcome {}!", p.0))
2018-05-21 02:04:08 +02:00
})
})
});
// client request
let request = srv
.get()
.uri(srv.url("/sc/10/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome 10!"));
// client request
let request = srv
.get()
.uri(srv.url("/sc/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[test]
fn test_nested_scope_and_path_extractor() {
let mut srv = test::TestServer::with_factory(move || {
App::new().scope("/sc", |scope| {
scope.nested("/{num}", |scope| {
scope.resource("/{num}/index.html", |r| {
r.route().with(|p: Path<(usize, usize)>| {
format!("Welcome {} {}!", p.0, p.1)
})
2018-05-21 02:04:08 +02:00
})
})
})
});
// client request
let request = srv
.get()
.uri(srv.url("/sc/10/12/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"Welcome 10 12!"));
// client request
let request = srv
.get()
.uri(srv.url("/sc/10/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[cfg(actix_impl_trait)]
fn test_impl_trait(
data: (Json<Value>, Path<PParam>, Query<PParam>),
) -> impl Future<Item = String, Error = io::Error> {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "timeout"))
2018-05-17 21:20:20 +02:00
.and_then(move |_| Ok(format!("Welcome {} - {}!", data.1.username, (data.0).0)))
}
#[cfg(actix_impl_trait)]
fn test_impl_trait_err(
_data: (Json<Value>, Path<PParam>, Query<PParam>),
) -> impl Future<Item = String, Error = io::Error> {
2018-05-25 06:03:16 +02:00
Delay::new(Instant::now() + Duration::from_millis(10))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "timeout"))
.and_then(move |_| Err(io::Error::new(io::ErrorKind::Other, "other")))
}
#[cfg(actix_impl_trait)]
#[test]
fn test_path_and_query_extractor2_async4_impl_trait() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with_async(test_impl_trait)
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-05-17 21:20:20 +02:00
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - {\"test\":1}!"));
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
.uri(srv.url("/test1/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[cfg(actix_impl_trait)]
#[test]
fn test_path_and_query_extractor2_async4_impl_trait_err() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with_async(test_impl_trait_err)
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.post()
.uri(srv.url("/test1/index.html?username=test2"))
.header("content-type", "application/json")
.body("{\"test\": 1}")
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn test_non_ascii_route() {
let mut srv = test::TestServer::new(|app| {
app.resource("/中文/index.html", |r| r.f(|_| "success"));
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/中文/index.html"))
.finish()
.unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static(b"success"));
}
2018-04-17 21:55:13 +02:00
#[test]
fn test_unsafe_path_route() {
let mut srv = test::TestServer::new(|app| {
app.resource("/test/{url}", |r| {
r.f(|r| format!("success: {}", &r.match_info()["url"]))
});
});
// client request
2018-05-17 21:20:20 +02:00
let request = srv
.get()
2018-04-29 18:09:08 +02:00
.uri(srv.url("/test/http%3A%2F%2Fexample.com"))
.finish()
.unwrap();
2018-04-17 21:55:13 +02:00
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-04-29 18:09:08 +02:00
assert_eq!(
bytes,
Bytes::from_static(b"success: http:%2F%2Fexample.com")
);
2018-04-17 21:55:13 +02:00
}