Add more tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Valentin Brandl 2019-09-10 23:13:50 +02:00
parent 97377a2442
commit a85ff2a54b
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 223 additions and 64 deletions

View File

@ -1,25 +1,30 @@
use crate::{data::State, proxy_file, redirect, service::Bitbucket, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use crate::{
data::{Key, Service, State},
proxy_file, purge_local_cache, redirect,
service::Bitbucket,
REDIRECT_AGE,
};
use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
use time_cache::{Cache, CacheResult};
#[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>),
.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 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());
@ -30,18 +35,19 @@ 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>),
.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 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());
@ -52,19 +58,68 @@ 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>),
.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 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());
}
#[test]
fn redirect_cache() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(Arc::clone(&state))
.wrap(middleware::NormalizePath)
.route(
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<Bitbucket>),
)
.route(
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
web::delete().to_async(purge_local_cache::<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());
let key = Key::new(
Service::Bitbucket,
Arc::new("vbrandl".to_string()),
Arc::new("vbrandl.net".to_string()),
Arc::new("master".to_string()),
);
{
let cache = state.read().unwrap();
let res = cache.get(&key);
assert_ne!(CacheResult::Empty, res);
assert_ne!(CacheResult::Invalid, res);
} // release the lock
let req = test::TestRequest::delete()
.uri("/bitbucket/vbrandl/vbrandl.net/master/README.md")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
{
let cache = state.read().unwrap();
assert_eq!(CacheResult::Empty, cache.get(&key));
} // release the lock
}

View File

@ -1,8 +1,13 @@
use crate::{data::State, proxy_file, redirect, service::Github, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use crate::{
data::{Key, Service, State},
proxy_file, purge_local_cache, redirect,
service::Github,
REDIRECT_AGE,
};
use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
use time_cache::{Cache, CacheResult};
#[test]
fn requesting_branch_redirects() {
@ -37,7 +42,7 @@ fn invalid_file_404() {
web::get().to_async(proxy_file::<Github>),
),
);
// github
let req = test::TestRequest::with_uri(
"/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/Cargo.toml.invalid",
)
@ -69,3 +74,50 @@ fn valid_file_200() {
assert_eq!(StatusCode::OK, resp.status());
}
#[test]
fn redirect_cache() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(Arc::clone(&state))
.wrap(middleware::NormalizePath)
.route(
"/github/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<Github>),
)
.route(
"/github/{user}/{repo}/{commit}/{file:.*}",
web::delete().to_async(purge_local_cache::<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());
let key = Key::new(
Service::GitHub,
Arc::new("vbrandl".to_string()),
Arc::new("yagcdn".to_string()),
Arc::new("master".to_string()),
);
{
let cache = state.read().unwrap();
let res = cache.get(&key);
assert_ne!(CacheResult::Empty, res);
assert_ne!(CacheResult::Invalid, res);
} // release the lock
let req = test::TestRequest::delete()
.uri("/github/vbrandl/yagcdn/master/Cargo.toml")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
{
let cache = state.read().unwrap();
assert_eq!(CacheResult::Empty, cache.get(&key));
} // release the lock
}

View File

@ -1,22 +1,27 @@
use crate::{data::State, proxy_file, redirect, service::GitLab, REDIRECT_AGE};
use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App};
use crate::{
data::{Key, Service, State},
proxy_file, purge_local_cache, redirect,
service::GitLab,
REDIRECT_AGE,
};
use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App};
use awc::Client;
use std::sync::{Arc, RwLock};
use time_cache::Cache;
use time_cache::{Cache, CacheResult};
#[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>),
.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();
@ -29,19 +34,19 @@ 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>),
.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();
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::NOT_FOUND, resp.status());
@ -52,20 +57,67 @@ 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>),
.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();
)
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
}
#[test]
fn redirect_cache() {
let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE)));
let mut app = test::init_service(
App::new()
.data(Client::new())
.data(Arc::clone(&state))
.wrap(middleware::NormalizePath)
.route(
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(redirect::<GitLab>),
)
.route(
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
web::delete().to_async(purge_local_cache::<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());
let key = Key::new(
Service::GitLab,
Arc::new("vbrandl".to_string()),
Arc::new("hoc".to_string()),
Arc::new("master".to_string()),
);
{
let cache = state.read().unwrap();
let res = cache.get(&key);
assert_ne!(CacheResult::Empty, res);
assert_ne!(CacheResult::Invalid, res);
} // release the lock
let req = test::TestRequest::delete()
.uri("/gitlab/vbrandl/hoc/master/Cargo.toml")
.to_request();
let resp = test::block_fn(|| app.call(req)).unwrap();
assert_eq!(StatusCode::OK, resp.status());
{
let cache = state.read().unwrap();
assert_eq!(CacheResult::Empty, cache.get(&key));
} // release the lock
}