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

@ -1,122 +1,122 @@
[package] [package]
name = "actix-web" name = "actix-web"
version = "0.7.0" version = "0.7.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust." description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md" readme = "README.md"
keywords = ["http", "web", "framework", "async", "futures"] keywords = ["http", "web", "framework", "async", "futures"]
homepage = "https://actix.rs" homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-web.git" repository = "https://github.com/actix/actix-web.git"
documentation = "https://docs.rs/actix-web/" documentation = "https://docs.rs/actix-web/"
categories = ["network-programming", "asynchronous", categories = ["network-programming", "asynchronous",
"web-programming::http-server", "web-programming::http-server",
"web-programming::http-client", "web-programming::http-client",
"web-programming::websocket"] "web-programming::websocket"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"] exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
build = "build.rs" build = "build.rs"
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = ["tls", "alpn", "session", "brotli", "flate2-c"] features = ["tls", "alpn", "session", "brotli", "flate2-c"]
[badges] [badges]
travis-ci = { repository = "actix/actix-web", branch = "master" } travis-ci = { repository = "actix/actix-web", branch = "master" }
appveyor = { repository = "fafhrd91/actix-web-hdy9d" } appveyor = { repository = "fafhrd91/actix-web-hdy9d" }
codecov = { repository = "actix/actix-web", branch = "master", service = "github" } codecov = { repository = "actix/actix-web", branch = "master", service = "github" }
[lib] [lib]
name = "actix_web" name = "actix_web"
path = "src/lib.rs" path = "src/lib.rs"
[features] [features]
default = ["session", "brotli", "flate2-c"] default = ["session", "brotli", "flate2-c"]
# tls # tls
tls = ["native-tls", "tokio-tls"] tls = ["native-tls", "tokio-tls"]
# openssl # openssl
alpn = ["openssl", "tokio-openssl"] alpn = ["openssl", "tokio-openssl"]
# sessions feature, session require "ring" crate and c compiler # sessions feature, session require "ring" crate and c compiler
session = ["cookie/secure"] session = ["cookie/secure"]
# brotli encoding, requires c compiler # brotli encoding, requires c compiler
brotli = ["brotli2"] brotli = ["brotli2"]
# miniz-sys backend for flate2 crate # miniz-sys backend for flate2 crate
flate2-c = ["flate2/miniz-sys"] flate2-c = ["flate2/miniz-sys"]
# rust backend for flate2 crate # rust backend for flate2 crate
flate2-rust = ["flate2/rust_backend"] flate2-rust = ["flate2/rust_backend"]
[dependencies] [dependencies]
actix = "0.7.0" actix = "0.7.0"
base64 = "0.9" base64 = "0.9"
bitflags = "1.0" bitflags = "1.0"
failure = "0.1.1" failure = "0.1.1"
h2 = "0.1" h2 = "0.1"
htmlescape = "0.3" htmlescape = "0.3"
http = "^0.1.5" http = "^0.1.5"
httparse = "1.3" httparse = "1.3"
log = "0.4" log = "0.4"
mime = "0.3" mime = "0.3"
mime_guess = "2.0.0-alpha" mime_guess = "2.0.0-alpha"
num_cpus = "1.0" num_cpus = "1.0"
percent-encoding = "1.0" percent-encoding = "1.0"
rand = "0.5" rand = "0.5"
regex = "1.0" regex = "1.0"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_urlencoded = { path = "./serde_urlencoded/" } serde_urlencoded = { path = "./serde_urlencoded/" }
sha1 = "0.6" sha1 = "0.6"
smallvec = "0.6" smallvec = "0.6"
time = "0.1" time = "0.1"
encoding = "0.2" encoding = "0.2"
language-tags = "0.2" language-tags = "0.2"
lazy_static = "1.0" lazy_static = "1.0"
lazycell = "1.0.0" lazycell = "1.0.0"
parking_lot = "0.6" parking_lot = "0.6"
url = { version="1.7", features=["query_encoding"] } url = { version="1.7", features=["query_encoding"] }
cookie = { version="0.10", features=["percent-encode"] } cookie = { version="0.10", features=["percent-encode"] }
brotli2 = { version="^0.3.2", optional = true } brotli2 = { version="^0.3.2", optional = true }
flate2 = { version="1.0", optional = true, default-features = false } flate2 = { version="1.0", optional = true, default-features = false }
# io # io
mio = "^0.6.13" mio = "^0.6.13"
net2 = "0.2" net2 = "0.2"
bytes = "0.4" bytes = "0.4"
byteorder = "1.2" byteorder = "1.2"
futures = "0.1" futures = "0.1"
futures-cpupool = "0.1" futures-cpupool = "0.1"
slab = "0.4" slab = "0.4"
tokio = "0.1" tokio = "0.1"
tokio-io = "0.1" tokio-io = "0.1"
tokio-tcp = "0.1" tokio-tcp = "0.1"
tokio-timer = "0.2" tokio-timer = "0.2"
tokio-reactor = "0.1" tokio-reactor = "0.1"
# native-tls # native-tls
native-tls = { version="0.1", optional = true } native-tls = { version="0.1", optional = true }
tokio-tls = { version="0.1", optional = true } tokio-tls = { version="0.1", optional = true }
# openssl # openssl
openssl = { version="0.10", optional = true } openssl = { version="0.10", optional = true }
tokio-openssl = { version="0.2", optional = true } tokio-openssl = { version="0.2", optional = true }
[dev-dependencies] [dev-dependencies]
env_logger = "0.5" env_logger = "0.5"
serde_derive = "1.0" serde_derive = "1.0"
[build-dependencies] [build-dependencies]
version_check = "0.1" version_check = "0.1"
[profile.release] [profile.release]
lto = true lto = true
opt-level = 3 opt-level = 3
codegen-units = 1 codegen-units = 1
[workspace] [workspace]
members = [ members = [
"./", "./",
] ]

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| {