🚧 Prepare code for testing
This commit is contained in:
parent
916e5d3300
commit
d7762a10fa
8 changed files with 80 additions and 71 deletions
|
|
@ -14,21 +14,17 @@ use crate::db;
|
||||||
use actix_session::config::{BrowserSession, PersistentSession, SessionLifecycle};
|
use actix_session::config::{BrowserSession, PersistentSession, SessionLifecycle};
|
||||||
use actix_session::storage::CookieSessionStore;
|
use actix_session::storage::CookieSessionStore;
|
||||||
use actix_session::SessionMiddleware;
|
use actix_session::SessionMiddleware;
|
||||||
use actix_web::cookie::{time::Duration, Key};
|
|
||||||
use actix_web::dev::Server;
|
|
||||||
|
|
||||||
use std::io::Error;
|
|
||||||
|
|
||||||
use substring::Substring;
|
use substring::Substring;
|
||||||
|
|
||||||
pub struct Application {
|
use std::io::Error;
|
||||||
server: Server,
|
|
||||||
}
|
pub struct Application;
|
||||||
|
|
||||||
impl Application {
|
impl Application {
|
||||||
pub fn prepare(app: ModuleRef) -> Result<Self, Error> {
|
pub fn prepare(app: ModuleRef) -> Result<Self, Error> {
|
||||||
// Rótulo de presentación.
|
// On startup.
|
||||||
print_on_startup();
|
show_banner();
|
||||||
|
|
||||||
// Inicia registro de trazas y eventos.
|
// Inicia registro de trazas y eventos.
|
||||||
LazyStatic::force(&trace::TRACING);
|
LazyStatic::force(&trace::TRACING);
|
||||||
|
|
@ -53,46 +49,74 @@ impl Application {
|
||||||
// Ejecuta actualizaciones pendientes de la base de datos.
|
// Ejecuta actualizaciones pendientes de la base de datos.
|
||||||
module::all::run_migrations();
|
module::all::run_migrations();
|
||||||
|
|
||||||
|
Ok(Self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(self) -> Result<service::Server, Error> {
|
||||||
|
// Generate cookie key.
|
||||||
|
let secret_key = service::cookie::Key::generate();
|
||||||
|
|
||||||
// Prepara el servidor web.
|
// Prepara el servidor web.
|
||||||
let secret_key = get_secret_key();
|
Ok(service::HttpServer::new(move || {
|
||||||
let server = service::HttpServer::new(move || {
|
service_app()
|
||||||
service::App::new()
|
|
||||||
.wrap(tracing_actix_web::TracingLogger::default())
|
.wrap(tracing_actix_web::TracingLogger::default())
|
||||||
.wrap(
|
.wrap(
|
||||||
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
|
||||||
.session_lifecycle(match config::SETTINGS.server.session_lifetime {
|
.session_lifecycle(match config::SETTINGS.server.session_lifetime {
|
||||||
0 => SessionLifecycle::BrowserSession(BrowserSession::default()),
|
0 => SessionLifecycle::BrowserSession(BrowserSession::default()),
|
||||||
_ => SessionLifecycle::PersistentSession(
|
_ => SessionLifecycle::PersistentSession(
|
||||||
PersistentSession::default().session_ttl(Duration::seconds(
|
PersistentSession::default().session_ttl(
|
||||||
config::SETTINGS.server.session_lifetime,
|
service::cookie::time::Duration::seconds(
|
||||||
)),
|
config::SETTINGS.server.session_lifetime,
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.configure(module::all::configure_services)
|
|
||||||
.default_service(service::web::route().to(service_not_found))
|
|
||||||
})
|
})
|
||||||
.bind(format!(
|
.bind(format!(
|
||||||
"{}:{}",
|
"{}:{}",
|
||||||
&config::SETTINGS.server.bind_address,
|
&config::SETTINGS.server.bind_address,
|
||||||
&config::SETTINGS.server.bind_port
|
&config::SETTINGS.server.bind_port
|
||||||
))?
|
))?
|
||||||
.run();
|
.run())
|
||||||
|
|
||||||
Ok(Self { server })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(self) -> Result<Server, Error> {
|
pub fn test(
|
||||||
Ok(self.server)
|
self,
|
||||||
}
|
) -> service::App<
|
||||||
|
impl service::Factory<
|
||||||
pub fn server(self) -> Server {
|
service::Request,
|
||||||
self.server
|
Config = (),
|
||||||
|
Response = service::Response<service::BoxBody>,
|
||||||
|
Error = service::Error,
|
||||||
|
InitError = (),
|
||||||
|
>,
|
||||||
|
> {
|
||||||
|
service_app()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_on_startup() {
|
fn service_app() -> service::App<
|
||||||
|
impl service::Factory<
|
||||||
|
service::Request,
|
||||||
|
Config = (),
|
||||||
|
Response = service::Response<service::BoxBody>,
|
||||||
|
Error = service::Error,
|
||||||
|
InitError = (),
|
||||||
|
>,
|
||||||
|
> {
|
||||||
|
service::App::new()
|
||||||
|
.configure(module::all::configure_services)
|
||||||
|
.default_service(service::web::route().to(service_not_found))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn service_not_found(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||||
|
Err(FatalError::NotFound(request))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_banner() {
|
||||||
if config::SETTINGS.app.startup_banner.to_lowercase() != "off" {
|
if config::SETTINGS.app.startup_banner.to_lowercase() != "off" {
|
||||||
// Application name.
|
// Application name.
|
||||||
let mut app_name = config::SETTINGS.app.name.to_string();
|
let mut app_name = config::SETTINGS.app.name.to_string();
|
||||||
|
|
@ -121,11 +145,3 @@ fn print_on_startup() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_secret_key() -> Key {
|
|
||||||
Key::generate()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn service_not_found(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
|
|
||||||
Err(FatalError::NotFound(request))
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,13 @@
|
||||||
//! Tipos y funciones para operar con el servidor web ([actix-web](https://docs.rs/actix-web)).
|
//! Tipos y funciones para operar con el servidor web ([actix-web](https://docs.rs/actix-web)).
|
||||||
|
|
||||||
pub use actix_session::Session;
|
pub use actix_session::Session;
|
||||||
pub use actix_web::{
|
pub use actix_web::body::BoxBody;
|
||||||
cookie, get, http, rt, web, App, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder,
|
pub use actix_web::dev::Server;
|
||||||
};
|
pub use actix_web::dev::ServiceFactory as Factory;
|
||||||
|
pub use actix_web::dev::ServiceRequest as Request;
|
||||||
|
pub use actix_web::dev::ServiceResponse as Response;
|
||||||
|
pub use actix_web::{cookie, get, http, rt, test, web};
|
||||||
|
pub use actix_web::{App, Error, HttpMessage, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||||
|
|
||||||
pub use actix_web_files::Files as ActixFiles;
|
pub use actix_web_files::Files as ActixFiles;
|
||||||
pub use actix_web_static_files::ResourceFiles;
|
pub use actix_web_static_files::ResourceFiles;
|
||||||
|
|
|
||||||
2
pagetop/tests/main.rs
Normal file
2
pagetop/tests/main.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod server;
|
||||||
20
pagetop/tests/server/health_check.rs
Normal file
20
pagetop/tests/server/health_check.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
use pagetop::prelude::*;
|
||||||
|
|
||||||
|
new_handle!(MODULE_TEST_SERVER_HEALTH_CHECK);
|
||||||
|
|
||||||
|
struct HealthCheck;
|
||||||
|
|
||||||
|
impl ModuleTrait for HealthCheck {
|
||||||
|
fn handle(&self) -> Handle {
|
||||||
|
MODULE_TEST_SERVER_HEALTH_CHECK
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn health_check_works() {
|
||||||
|
let app = service::test::init_service(Application::prepare(&HealthCheck).unwrap().test()).await;
|
||||||
|
let req = service::test::TestRequest::get().uri("/").to_request();
|
||||||
|
let _resp = service::test::call_service(&app, req).await;
|
||||||
|
|
||||||
|
// assert_eq!("OK", "OK");
|
||||||
|
}
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "tests"
|
|
||||||
version = "0.0.1"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
authors = [
|
|
||||||
"Manuel Cillero <manuel@cillero.es>"
|
|
||||||
]
|
|
||||||
description = """\
|
|
||||||
Tests for PageTop.\
|
|
||||||
"""
|
|
||||||
homepage = "https://pagetop.cillero.es"
|
|
||||||
repository = "https://github.com/manuelcillero/pagetop"
|
|
||||||
license = "Apache-2.0 OR MIT"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
pagetop = { version = "0.0", path = "../pagetop", features = ["mysql"], default-features = false }
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
mod server;
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
use pagetop::prelude::*;
|
|
||||||
|
|
||||||
struct HealthCheck;
|
|
||||||
|
|
||||||
impl ModuleTrait for HealthCheck {}
|
|
||||||
|
|
||||||
async fn spawn_app() {
|
|
||||||
let server = Application::prepare(&HealthCheck).unwrap().server();
|
|
||||||
let _ = actix_web::rt::spawn(server);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::test]
|
|
||||||
async fn health_check_works() {
|
|
||||||
spawn_app();
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue