Skip to content

Commit

Permalink
chore: set LISTENING_HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus committed Dec 20, 2023
1 parent 3e1c550 commit 3993e8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub async fn serve(port: u16, switch_port: bool) -> Result<(), String> {
Ok(())
}

async fn create_listener(
port: u16,
switch_port: bool,
) -> Result<TcpListener, String> {
async fn create_listener(port: u16, switch_port: bool) -> Result<TcpListener, String> {
let host = HOST.get().unwrap();
let mut port = port;
// Loop until the port is available
Expand Down
18 changes: 12 additions & 6 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use live_server::listen;
use reqwest::StatusCode;

const LISTENING_HOST: &str = "0.0.0.0";
const REQUESTING_HOST: &str = "127.0.0.1";
const PORT: u16 = 8000;

#[tokio::test]
async fn request() {
tokio::spawn(async {
listen("127.0.0.1", 8000, "./tests/page", false)
listen(LISTENING_HOST, PORT, "./tests/page", false)
.await
.unwrap();
});

// Test requesting index.html
let response = reqwest::get("http://127.0.0.1:8000").await.unwrap();
let response = reqwest::get(format!("http://{}:{}", REQUESTING_HOST, PORT))
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);

Expand All @@ -23,14 +29,14 @@ async fn request() {
include_str!("./page/index.html"),
format!(
include_str!("../src/templates/websocket.html"),
"127.0.0.1", 8000
LISTENING_HOST, PORT
)
)
.replace("\r\n", "\n");
assert_eq!(text, target_text);

// Test requesting index.js
let response = reqwest::get("http://127.0.0.1:8000/index.js")
let response = reqwest::get(format!("http://{}:{}/index.js", REQUESTING_HOST, PORT))
.await
.unwrap();

Expand All @@ -44,7 +50,7 @@ async fn request() {
assert_eq!(text, target_text);

// Test requesting non-existent html file
let response = reqwest::get("http://127.0.0.1:8000/404.html")
let response = reqwest::get(format!("http://{}:{}/404.html", REQUESTING_HOST, PORT))
.await
.unwrap();

Expand All @@ -57,7 +63,7 @@ async fn request() {
assert!(text.starts_with("<!DOCTYPE html>"));

// Test requesting non-existent asset
let response = reqwest::get("http://127.0.0.1:8000/favicon.ico")
let response = reqwest::get(format!("http://{}:{}/favicon.ico", REQUESTING_HOST, PORT))
.await
.unwrap();

Expand Down

0 comments on commit 3993e8f

Please sign in to comment.