Skip to content

Commit

Permalink
run test servers on unqiue ports
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Dec 18, 2023
1 parent edbcc40 commit 63a23f7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dwn-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"

[dev-dependencies]
port_check = "0.1.5"
reqwest = { version = "0.11.22", features = ["json"] }
serde_json = "1.0.108"
13 changes: 11 additions & 2 deletions dwn-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ use axum::{
use dwn::data::{Message, RecordIdGenerator};
use tracing::{error, info, span, warn};

pub struct StartOptions {
pub port: u16,
}

impl Default for StartOptions {
fn default() -> Self {
Self { port: 3000 }
}
}

/// Start the server.
pub async fn start() {
pub async fn start(StartOptions { port }: StartOptions) {
let app = Router::new().route("/", post(post_handler));

let port = 3000;
let addr = SocketAddr::from(([127, 0, 0, 1], port));

let listener = match tokio::net::TcpListener::bind(addr).await {
Expand Down
4 changes: 3 additions & 1 deletion dwn-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use dwn_server::StartOptions;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
dwn_server::start().await;
dwn_server::start(StartOptions::default()).await;
}
32 changes: 19 additions & 13 deletions dwn-server/tests/messages.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use dwn::data::{DescriptorBuilder, Message, MessageBuilder, RequestBody};
use dwn_server::StartOptions;
use reqwest::StatusCode;

const SERVER_ADDR: &str = "http://localhost:3000";
fn spawn_server() -> u16 {
let port = port_check::free_local_port().expect("Failed to find free port");

fn spawn_server() {
tokio::spawn(async move {
dwn_server::start().await;
dwn_server::start(StartOptions { port }).await;
});

// Wait for server to start
std::thread::sleep(std::time::Duration::from_secs(2));

port
}

async fn send_post(data: dwn::data::RequestBody) -> StatusCode {
async fn send_post(data: dwn::data::RequestBody, port: u16) -> StatusCode {
let client = reqwest::Client::new();

let res = match client
.post(SERVER_ADDR)
.post(format!("http://localhost:{}", port))
.header("Content-Type", "application/json")
.body(serde_json::to_string(&data).expect("Failed to serialize data"))
.send()
Expand Down Expand Up @@ -44,18 +47,18 @@ fn empty_message() -> Message {

#[tokio::test]
async fn recieve_post() {
spawn_server();
let port = spawn_server();

let body = RequestBody {
messages: vec![empty_message()],
};

assert_eq!(send_post(body).await, StatusCode::OK);
assert_eq!(send_post(body, port).await, StatusCode::OK);
}

#[tokio::test]
async fn requires_valid_record_id() {
spawn_server();
let port = spawn_server();

let mut msg = empty_message();
msg.record_id = "invalid record id".to_string();
Expand All @@ -64,12 +67,15 @@ async fn requires_valid_record_id() {
messages: vec![msg],
};

assert_eq!(send_post(body).await, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(
send_post(body, port).await,
StatusCode::INTERNAL_SERVER_ERROR
);
}

#[tokio::test]
async fn requires_data_descriptors() {
spawn_server();
let port = spawn_server();

let mut msg = empty_message();
msg.data = Some("test data".to_string());
Expand All @@ -91,15 +97,15 @@ async fn requires_data_descriptors() {
without_both.messages[0].descriptor.data_format = None;

assert_eq!(
send_post(without_cid).await,
send_post(without_cid, port).await,
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
send_post(without_format).await,
send_post(without_format, port).await,
StatusCode::INTERNAL_SERVER_ERROR
);
assert_eq!(
send_post(without_both).await,
send_post(without_both, port).await,
StatusCode::INTERNAL_SERVER_ERROR
);
}

0 comments on commit 63a23f7

Please sign in to comment.