Skip to content

Commit

Permalink
Add tests for base_path
Browse files Browse the repository at this point in the history
  • Loading branch information
aldur authored and matze committed Feb 25, 2024
1 parent b80ed4f commit dc61e4a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ jobs:
components: clippy
- run: cargo clippy -- -W clippy::pedantic
- run: cargo test
- run: WASTEBIN_BASE_URL="http://127.0.0.1:8080/wastebin" cargo test # port is not relevant
46 changes: 25 additions & 21 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ pub fn routes() -> Router<AppState> {
#[cfg(test)]
mod tests {
use crate::db::write::Entry;
use crate::env::base_path;
use crate::routes;
use crate::test_helpers::{make_app, Client};
use http::StatusCode;
use reqwest::header;
use serde::Serialize;

// TODO: Add tests for base path

#[tokio::test]
async fn unknown_paste() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(make_app()?).await;

let res = client.get("/000000").send().await?;
let res = client.get(&base_path().join("000000")).send().await?;
assert_eq!(res.status(), StatusCode::NOT_FOUND);

Ok(())
Expand All @@ -52,7 +51,7 @@ mod tests {
password: "".to_string(),
};

let res = client.post("/").form(&data).send().await?;
let res = client.post(base_path().path()).form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let location = res.headers().get("location").unwrap().to_str()?;
Expand Down Expand Up @@ -100,24 +99,24 @@ mod tests {
password: "".to_string(),
};

let res = client.post("/").form(&data).send().await?;
let res = client.post(base_path().path()).form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let location = res.headers().get("location").unwrap().to_str()?;

// Location is the `/burn/foo` page not the paste itself, so ignore the prefix.
let location = location.split_at(5).1;
// Location is the `/burn/foo` page not the paste itself, so remove the prefix.
let location = location.replace("burn/", "");

let res = client
.get(location)
.get(&location)
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;

assert_eq!(res.status(), StatusCode::OK);

let res = client
.get(location)
.get(&location)
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
Expand All @@ -139,16 +138,16 @@ mod tests {
password: password.to_string(),
};

let res = client.post("/").form(&data).send().await?;
let res = client.post(base_path().path()).form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let location = res.headers().get("location").unwrap().to_str()?;

// Location is the `/burn/foo` page not the paste itself, so ignore the prefix.
let location = location.split_at(5).1;
// Location is the `/burn/foo` page not the paste itself, so remove the prefix.
let location = location.replace("burn/", "");

let res = client
.get(location)
.get(&location)
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
Expand All @@ -165,7 +164,7 @@ mod tests {
};

let res = client
.post(location)
.post(&location)
.form(&data)
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
Expand All @@ -174,7 +173,7 @@ mod tests {
assert_eq!(res.status(), StatusCode::OK);

let res = client
.get(location)
.get(&location)
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
Expand All @@ -193,7 +192,7 @@ mod tests {
..Default::default()
};

let res = client.post("/").json(&entry).send().await?;
let res = client.post(base_path().path()).json(&entry).send().await?;
assert_eq!(res.status(), StatusCode::OK);

let payload = res.json::<routes::json::RedirectResponse>().await?;
Expand All @@ -216,7 +215,7 @@ mod tests {
..Default::default()
};

let res = client.post("/").json(&entry).send().await?;
let res = client.post(base_path().path()).json(&entry).send().await?;
assert_eq!(res.status(), StatusCode::OK);

let payload = res.json::<routes::json::RedirectResponse>().await?;
Expand Down Expand Up @@ -244,16 +243,21 @@ mod tests {
password: "".to_string(),
};

let res = client.post("/").form(&data).send().await?;
let res = client.post(base_path().path()).form(&data).send().await?;
let uid_cookie = res.cookies().find(|cookie| cookie.name() == "uid");
assert!(uid_cookie.is_some());
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let location = res.headers().get("location").unwrap().to_str()?;
let res = client.get(&format!("/delete{location}")).send().await?;
let id = location.replace(base_path().path(), "");

let res = client
.get(&base_path().join(&format!("delete/{id}")))
.send()
.await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let res = client.get(location).send().await?;
let res = client.get(&base_path().join(&id)).send().await?;
assert_eq!(res.status(), StatusCode::NOT_FOUND);

Ok(())
Expand All @@ -270,7 +274,7 @@ mod tests {
password: "".to_string(),
};

let res = client.post("/").form(&data).send().await?;
let res = client.post(base_path().path()).form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);

let location = res.headers().get("location").unwrap().to_str()?;
Expand Down
3 changes: 2 additions & 1 deletion src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cache::Cache;
use crate::db::{self, Database};
use crate::env::base_url;
use axum::extract::Request;
use axum::response::Response;
use axum::Router;
Expand Down Expand Up @@ -56,7 +57,7 @@ pub(crate) fn make_app() -> Result<Router, Box<dyn std::error::Error>> {
let db = Database::new(db::Open::Memory)?;
let cache = Cache::new(NonZeroUsize::new(128).unwrap());
let key = Key::generate();
let base_url = None;
let base_url = base_url().unwrap();
let state = crate::AppState {
db,
cache,
Expand Down

0 comments on commit dc61e4a

Please sign in to comment.