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,8 +1,13 @@
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() {
@ -18,8 +23,8 @@ fn requesting_branch_redirects() {
),
);
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());
@ -39,8 +44,9 @@ fn invalid_file_404() {
),
);
let req =
test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md.invalid")
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();
@ -61,10 +67,59 @@ fn valid_file_200() {
),
);
let req =
test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md")
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,8 +1,13 @@
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() {
@ -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(
"/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
}