mirror of https://github.com/fafhrd91/actix-web
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:
parent
1b5bef811b
commit
09efa6aeb2
|
@ -139,15 +139,24 @@ impl<T: fmt::Display> fmt::Display for Path<T> {
|
|||
/// #[macro_use] extern crate serde_derive;
|
||||
/// use actix_web::{App, Query, http};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Info {
|
||||
/// username: String,
|
||||
/// }
|
||||
///
|
||||
///#[derive(Debug, Deserialize)]
|
||||
///pub enum ResponseType {
|
||||
/// Token,
|
||||
/// Code
|
||||
///}
|
||||
///
|
||||
///#[derive(Deserialize)]
|
||||
///pub struct AuthRequest {
|
||||
/// id: u64,
|
||||
/// response_type: ResponseType,
|
||||
///}
|
||||
///
|
||||
/// // use `with` extractor for query info
|
||||
/// // this handler get called only if request's query contains `username` field
|
||||
/// fn index(info: Query<Info>) -> String {
|
||||
/// format!("Welcome {}!", info.username)
|
||||
/// // The correct request for this handler would be `/index.html?id=64&response_type=Code"`
|
||||
/// fn index(info: Query<AuthRequest>) -> String {
|
||||
/// format!("Authorization request for client with id={} and type={:?}!", info.id, info.response_type)
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
|
|
|
@ -91,6 +91,48 @@ fn test_query_extractor() {
|
|||
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]
|
||||
fn test_async_extractor_async() {
|
||||
let mut srv = test::TestServer::new(|app| {
|
||||
|
|
Loading…
Reference in New Issue