aboutsummaryrefslogtreecommitdiff
path: root/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests.rs')
-rw-r--r--src/tests.rs104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/tests.rs b/src/tests.rs
new file mode 100644
index 0000000..3eb71ec
--- /dev/null
+++ b/src/tests.rs
@@ -0,0 +1,104 @@
+#![cfg(test)]
+use std::{future::Future, path::PathBuf, pin::Pin};
+
+use crate::controller::Controller;
+
+use super::{
+ controller::{MultiUserController, SoloController},
+ origins::HttpOrigin,
+};
+use git2::Repository;
+use hyper::{body, http::request::Builder, Body, Request, Response};
+use tempdir::TempDir;
+
+fn temp_repo() -> (PathBuf, Repository) {
+ let path = TempDir::new("gitpad-test").unwrap().into_path();
+ let repo = Repository::init_bare(&path).unwrap();
+ (path, repo)
+}
+
+fn origin() -> HttpOrigin {
+ "http://pad.example.com".parse().unwrap()
+}
+
+struct Server<C> {
+ repo_path: PathBuf,
+ repo: Repository,
+ origin: HttpOrigin,
+ controller: C,
+}
+
+impl Server<SoloController> {
+ fn new() -> Self {
+ let (repo_path, repo) = temp_repo();
+ let origin = origin();
+ Self {
+ repo_path,
+ origin,
+ controller: SoloController::new(&repo),
+ repo,
+ }
+ }
+}
+
+impl Server<MultiUserController> {
+ fn new() -> Self {
+ let (repo_path, repo) = temp_repo();
+ let origin = origin();
+ Self {
+ repo_path,
+ origin,
+ controller: MultiUserController::new(&repo),
+ repo,
+ }
+ }
+}
+
+impl<C: Controller> Server<C> {
+ async fn serve(&self, req: Request<Body>) -> Response<Body> {
+ super::service(&self.repo_path, &self.origin, &self.controller, req).await
+ }
+}
+
+trait IntoText {
+ fn into_text(self) -> Pin<Box<dyn Future<Output = String>>>;
+}
+
+impl IntoText for Body {
+ fn into_text(self) -> Pin<Box<dyn Future<Output = String>>> {
+ Box::pin(async move {
+ std::str::from_utf8(&body::to_bytes(self).await.unwrap())
+ .unwrap()
+ .to_string()
+ })
+ }
+}
+
+#[tokio::test]
+async fn test_no_host_header() {
+ let server = Server::<SoloController>::new();
+ let res = server.serve(Builder::new().body("".into()).unwrap()).await;
+ assert_eq!(res.status(), 400);
+ assert!(res
+ .into_body()
+ .into_text()
+ .await
+ .contains("Host header required"));
+}
+
+#[tokio::test]
+async fn test_missing_branch() {
+ let server = Server::<SoloController>::new();
+
+ let res = server
+ .serve(
+ Builder::new()
+ .header("host", server.origin.host())
+ .body("".into())
+ .unwrap(),
+ )
+ .await;
+ assert_eq!(res.status(), 404); // branch not found
+
+ // TODO: create commit with file and test that it is retrievable
+}