Add test to verify enum in query

Docs are updated to show example of how to use enum.
Closes  #371
This commit is contained in:
Douman 2018-07-12 19:32:54 +03:00
parent 1b5bef811b
commit 09efa6aeb2
3 changed files with 179 additions and 128 deletions

View File

@ -139,15 +139,24 @@ impl<T: fmt::Display> fmt::Display for Path<T> {
/// #[macro_use] extern crate serde_derive; /// #[macro_use] extern crate serde_derive;
/// use actix_web::{App, Query, http}; /// use actix_web::{App, Query, http};
/// ///
/// #[derive(Deserialize)] ///
/// struct Info { ///#[derive(Debug, Deserialize)]
/// username: String, ///pub enum ResponseType {
/// } /// Token,
/// Code
///}
///
///#[derive(Deserialize)]
///pub struct AuthRequest {
/// id: u64,
/// response_type: ResponseType,
///}
/// ///
/// // use `with` extractor for query info /// // use `with` extractor for query info
/// // this handler get called only if request's query contains `username` field /// // this handler get called only if request's query contains `username` field
/// fn index(info: Query<Info>) -> String { /// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`
/// format!("Welcome {}!", info.username) /// fn index(info: Query<AuthRequest>) -> String {
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
/// } /// }
/// ///
/// fn main() { /// fn main() {

View File

@ -91,6 +91,48 @@ fn test_query_extractor() {
assert_eq!(response.status(), StatusCode::BAD_REQUEST); assert_eq!(response.status(), StatusCode::BAD_REQUEST);
} }
#[derive(Deserialize, Debug)]
pub enum ResponseType {
Token,
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();
assert_eq!(bytes, Bytes::from_static(b"AuthRequest { id: 64, response_type: Code }"));
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);
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);
}
#[test] #[test]
fn test_async_extractor_async() { fn test_async_extractor_async() {
let mut srv = test::TestServer::new(|app| { let mut srv = test::TestServer::new(|app| {