Skip to content

Commit

Permalink
chore: move some tests to viz
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 2, 2023
1 parent 07630cc commit 8d3fb42
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 319 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ license = "MIT"
rust-version = "1.63" # follows `tokio` and `hyper`

[workspace.dependencies]
viz = { version = "0.5.0", path = "viz" }
viz = { version = "0.5.0-rc.2", path = "viz" }
viz-core = { version = "0.5.0", path = "viz-core" }
viz-router = { version = "0.5.0", path = "viz-router" }
viz-handlers = { version = "0.5.0", path = "viz-handlers", default-features = false }
Expand Down
2 changes: 0 additions & 2 deletions viz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ tokio-stream = { workspace = true, optional = true }
tokio-util = { workspace = true, optional = true }

[dev-dependencies]
viz = { workspace = true, features = ["session"] }
viz-test.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

[package.metadata.docs.rs]
Expand Down
291 changes: 2 additions & 289 deletions viz-core/tests/request.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
use std::collections::{BTreeMap, HashMap};

use headers::{authorization::Bearer, Authorization, ContentType, HeaderValue};
use headers::{ContentType, HeaderValue};
use http::uri::Scheme;
use serde::{Deserialize, Serialize};
use viz_core::{
// TODO: reqwest and hyper haven't used the same version of `http`.
// header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE},
// StatusCode,
header::CONTENT_TYPE,
types::{self, PayloadError},
Error,
types::PayloadError,
IncomingBody,
IntoResponse,
Request,
RequestExt,
Response,
ResponseExt,
Result,
};

Expand Down Expand Up @@ -70,284 +64,3 @@ fn request_ext() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn request_body() -> Result<()> {
use futures_util::stream::TryStreamExt;
use viz::{
middleware::{cookie, limits},
Router,
};
use viz_test::http::{
header::{AUTHORIZATION, COOKIE},
StatusCode,
};
use viz_test::TestServer;

let router = Router::new()
.get("/:id", |req: Request| async move {
let id = req.param::<String>("id")?;
Ok(id)
})
.get("/:username/:repo", |req: Request| async move {
let (username, repo): (String, String) = req.params()?;
Ok(format!("{username}/{repo}"))
})
.get("/extract-token", |mut req: Request| async move {
let header: types::Header<Authorization<Bearer>> = req.extract().await?;
Ok(header.into_inner().token().to_string())
})
.post("/extract-body", |mut req: Request| async move {
let form: types::Form<BTreeMap<String, String>> = req.extract().await?;
Ok(Response::json(form.into_inner()))
})
.get("/cookies", |req: Request| async move {
let cookies = req.cookies()?;
let jar = cookies
.jar()
.lock()
.map_err(|e| Error::Responder(e.to_string().into_response()))?;
Ok(jar.iter().count().to_string())
})
.get("/cookie", |req: Request| async move {
Ok(req.cookie("viz").unwrap().value().to_string())
})
.with(cookie::Config::default())
.post("/bytes", |mut req: Request| async move {
let data = req.bytes().await?;
Ok(data)
})
.post("/bytes-with-limit", |mut req: Request| async move {
let data = req.bytes_with(None, 4).await?;
Ok(data)
})
.post("/bytes-used", |mut req: Request| async move {
req.bytes().await?;
let data = req.bytes().await?;
Ok(data)
})
.post("/text", |mut req: Request| async move {
let data = req.text().await?;
Ok(Response::text(data))
})
.post("/json", |mut req: Request| async move {
let data = req.json::<Page>().await?;
Ok(Response::json(data))
})
.post("/form", |mut req: Request| async move {
let data = req.form::<HashMap<String, String>>().await?;
Ok(Response::json(data))
})
.post("/multipart", |mut req: Request| async move {
let mut multipart = req.multipart().await?;
let mut data = HashMap::new();

while let Some(mut field) = multipart.try_next().await? {
let buf = field.bytes().await?.to_vec();
data.insert(field.name, String::from_utf8(buf).map_err(Error::normal)?);
}

Ok(Response::json(data))
})
.with(limits::Config::new().limits(types::Limits::new()));

let client = TestServer::new(router).await?;

let resp = client.get("/7").send().await.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "7");

let resp = client
.get("/viz-rs/viz")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "viz-rs/viz");

let resp = client
.get("/extract-token")
.header(AUTHORIZATION, "Bearer viz.rs")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "viz.rs");

let mut form = BTreeMap::new();
form.insert("password", "rs");
form.insert("username", "viz");
let resp = client
.post("/extract-body")
.form(&form)
.send()
.await
.map_err(Error::normal)?;
assert_eq!(
resp.text().await.map_err(Error::normal)?,
r#"{"password":"rs","username":"viz"}"#
);

let resp = client
.get("/cookie")
.header(COOKIE, "viz=crate")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "crate");

let resp = client
.get("/cookies")
.header(COOKIE, "auth=true;dark=false")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "2");

let resp = client
.post("/bytes")
.body("bytes")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "bytes");

let resp = client
.post("/bytes-with-limit")
.body("rust")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.text().await.map_err(Error::normal)?, "rust");

let resp = client
.post("/bytes-with-limit")
.body("crate")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
assert_eq!(
resp.text().await.map_err(Error::normal)?,
"payload is too large"
);

let resp = client
.post("/bytes-used")
.body("used")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(
resp.text().await.map_err(Error::normal)?,
"payload has been used"
);

let resp = client
.post("/text")
.body("text")
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "text");

let resp = client
.post("/json")
.json(&Page { p: 1 })
.send()
.await
.map_err(Error::normal)?;
assert_eq!(
resp.json::<Page>().await.map_err(Error::normal)?,
Page { p: 1 }
);

let mut form = HashMap::new();
form.insert("username", "viz");
form.insert("password", "rs");
let resp = client
.post("/form")
.form(&form)
.send()
.await
.map_err(Error::normal)?;
assert_eq!(
resp.json::<HashMap<String, String>>()
.await
.map_err(Error::normal)?,
{
let mut form = HashMap::new();
form.insert("username".to_string(), "viz".to_string());
form.insert("password".to_string(), "rs".to_string());
form
}
);

let form = viz_test::multipart::Form::new()
.text("key3", "3")
.text("key4", "4");
let resp = client
.post("/multipart")
.multipart(form)
.send()
.await
.map_err(Error::normal)?;
assert_eq!(
resp.json::<HashMap<String, String>>()
.await
.map_err(Error::normal)?,
{
let mut form = HashMap::new();
form.insert("key3".to_string(), "3".to_string());
form.insert("key4".to_string(), "4".to_string());
form
}
);

Ok(())
}

#[tokio::test]
async fn request_session() -> Result<()> {
use viz::{
middleware::{cookie, helper::CookieOptions, session},
Router,
};
use viz_test::http::header::{COOKIE, SET_COOKIE};
use viz_test::{nano_id, sessions, TestServer};

let router = Router::new()
.post("/session/set", |req: Request| async move {
let counter = req.session().get::<u64>("counter")?.unwrap_or_default() + 1;
req.session().set("counter", counter)?;
Ok(counter.to_string())
})
.with(session::Config::new(
session::Store::new(
sessions::MemoryStorage::new(),
nano_id::base64::<32>,
|sid: &str| sid.len() == 32,
),
CookieOptions::default(),
))
.with(cookie::Config::default());

let client = TestServer::new(router).await?;

let resp = client
.post("/session/set")
.send()
.await
.map_err(Error::normal)?;
let cookie = resp.headers().get(SET_COOKIE).cloned().unwrap();
assert_eq!(resp.text().await.map_err(Error::normal)?, "1");

let resp = client
.post("/session/set")
.header(COOKIE, cookie)
.send()
.await
.map_err(Error::normal)?;
assert_eq!(resp.text().await.map_err(Error::normal)?, "2");

Ok(())
}
27 changes: 0 additions & 27 deletions viz-core/tests/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,3 @@ async fn response_ext() -> Result<()> {
fn response_ext_panic() {
Response::redirect_with_status("/oauth", StatusCode::OK);
}

#[tokio::test]
async fn response_ext_with_server() -> Result<()> {
use viz::{Request, Router};
use viz_test::TestServer;

let router = Router::new()
.get("/", |_: Request| async move { Ok("") })
.post("/", |_: Request| async move {
Ok(Response::with(
Full::new("<xml/>".into()),
mime::TEXT_XML.as_ref(),
))
});

let client = TestServer::new(router).await?;

let resp = client.get("/").send().await.map_err(Error::normal)?;
assert_eq!(resp.content_length(), Some(0));
assert_eq!(resp.text().await.map_err(Error::normal)?, "");

let resp = client.post("/").send().await.map_err(Error::normal)?;
assert_eq!(resp.content_length(), Some(6));
assert_eq!(resp.text().await.map_err(Error::normal)?, "<xml/>");

Ok(())
}
7 changes: 7 additions & 0 deletions viz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ tokio-rustls = { workspace = true, optional = true }
tokio-native-tls = { workspace = true, optional = true }

[dev-dependencies]
viz-test.workspace = true
bytes.workspace = true
headers.workspace = true
http.workspace = true
http-body-util.workspace = true
mime.workspace = true
serde.workspace = true
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread"] }

[package.metadata.docs.rs]
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8d3fb42

Please sign in to comment.