#![cfg(test)] use std::{future::Future, path::PathBuf, pin::Pin}; use crate::{controller::Controller, StaticContext}; 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 { context: StaticContext, repo: Repository, controller: C, } impl Server { fn new() -> Self { let (repo_path, repo) = temp_repo(); let origin = origin(); Self { context: StaticContext { repo_path, origin }, controller: SoloController::new(&repo), repo, } } } impl Server { fn new() -> Self { let (repo_path, repo) = temp_repo(); let origin = origin(); Self { context: StaticContext { repo_path, origin }, controller: MultiUserController::new(&repo), repo, } } } impl Server { async fn serve(&self, req: Request) -> Response { super::service(&self.context, &self.controller, req).await } } trait IntoText { fn into_text(self) -> Pin>>; } impl IntoText for Body { fn into_text(self) -> Pin>> { 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::::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::::new(); let res = server .serve( Builder::new() .header("host", server.context.origin.host()) .body("".into()) .unwrap(), ) .await; assert_eq!(res.status(), 200); assert!(res .into_body() .into_text() .await .contains("create files by editing the URL")); // TODO: create file and test that it is retrievable }