Implement a few tests

This commit is contained in:
Valentin Brandl 2019-09-10 22:45:32 +02:00
parent 0ea5f38f83
commit 97377a2442
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
5 changed files with 218 additions and 0 deletions

View File

@ -16,6 +16,9 @@ mod error;
mod service; mod service;
mod statics; mod statics;
#[cfg(test)]
mod test;
use crate::{ use crate::{
cdn::Cloudflare, cdn::Cloudflare,
data::{FilePath, State}, data::{FilePath, State},

View File

@ -0,0 +1,70 @@
use crate::{data::State, proxy_file, redirect, service::Bitbucket, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
#[test]
fn requesting_branch_redirects() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<Bitbucket>),
),
);
let req = test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/master/README.md")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::SEE_OTHER, resp.status());
}
#[test]
fn invalid_file_404() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<Bitbucket>),
),
);
let req =
test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md.invalid")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::NOT_FOUND, resp.status());
}
#[test]
fn valid_file_200() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<Bitbucket>),
),
);
let req =
test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
}

View File

@ -0,0 +1,71 @@
use crate::{data::State, proxy_file, redirect, service::Github, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
#[test]
fn requesting_branch_redirects() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/github/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<Github>),
),
);
let req = test::TestRequest::with_uri("/github/vbrandl/yagcdn/master/Cargo.toml").to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::SEE_OTHER, resp.status());
}
#[test]
fn invalid_file_404() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<Github>),
),
);
// github
let req = test::TestRequest::with_uri(
"/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/Cargo.toml.invalid",
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::NOT_FOUND, resp.status());
}
#[test]
fn valid_file_200() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<Github>),
),
);
// github
let req = test::TestRequest::with_uri(
"/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/backend/Cargo.toml",
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
}

View File

@ -0,0 +1,71 @@
use crate::{data::State, proxy_file, redirect, service::GitLab, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
#[test]
fn requesting_branch_redirects() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<GitLab>),
),
);
let req = test::TestRequest::with_uri("/gitlab/vbrandl/hoc/master/Cargo.toml").to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::SEE_OTHER, resp.status());
}
#[test]
fn invalid_file_404() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<GitLab>),
),
);
let req = test::TestRequest::with_uri(
"/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml.invalid",
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::NOT_FOUND, resp.status());
}
#[test]
fn valid_file_200() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(state)
.wrap(middleware::NormalizePath)
.route(
"/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}",
web::get().to_async(proxy_file::<GitLab>),
),
);
let req = test::TestRequest::with_uri(
"/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml",
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
}

3
backend/src/test/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod bitbucket;
mod github;
mod gitlab;