feat(test): use random ports for testing

- port is assigned by the OS.
This commit is contained in:
2024-03-29 16:20:53 -04:00
parent cd677039b1
commit 83915acbd1
3 changed files with 20 additions and 7 deletions

View File

@@ -1,16 +1,17 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web::dev::Server;
use std::net::TcpListener;
async fn healthcheck_route() -> HttpResponse {
return HttpResponse::Ok().finish()
}
pub fn run() -> Result<Server, std::io::Error>{
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error>{
let server = HttpServer::new(||{
App::new()
.route("/health_check", web::get().to(healthcheck_route))
})
.bind("127.0.0.1:8000")?
.listen(listener)?
.run();
Ok(server)

View File

@@ -1,8 +1,12 @@
use std::net::TcpListener;
use email_newsletter_api::run;
#[tokio::main]
async fn main() -> Result<(), std::io::Error>{
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000");
// Move the error up the call stack
// otherwise await for the HttpServer
run()?.await
run(listener)?.await
}