From cf0d13562eb25d10b0a4b170855fc6feece33b5f Mon Sep 17 00:00:00 2001 From: Mirus Date: Tue, 19 Dec 2023 21:08:09 +0800 Subject: [PATCH 1/9] fix: cargo test --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e04fa06..bfe030b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,10 @@ //! ## Create live server //! ``` //! use live_server::listen; -//! listen("127.0.0.1", 8080, "./").await.unwrap(); +//! +//! async fn serve() { +//! listen("127.0.0.1", 8080, "./").await.unwrap(); +//! } //! ``` //! //! ## Enable logs (Optional) @@ -26,7 +29,10 @@ static TX: OnceCell> = OnceCell::const_new(); /// Watch the directory and create a static server. /// ``` /// use live_server::listen; -/// listen("127.0.0.1", 8080, "./").await.unwrap(); +/// +/// async fn serve() { +/// listen("127.0.0.1", 8080, "./").await.unwrap(); +/// } /// ``` pub async fn listen>( host: &str, From 56b26ac5938038a3200279d08781963e047ffcf9 Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 19:04:41 +0800 Subject: [PATCH 2/9] chore(test): add test case --- Cargo.lock | 560 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 + tests/page/index.html | 11 + tests/page/index.js | 1 + tests/test.rs | 63 +++++ 5 files changed, 625 insertions(+), 13 deletions(-) create mode 100644 tests/page/index.html create mode 100644 tests/page/index.js create mode 100644 tests/test.rs diff --git a/Cargo.lock b/Cargo.lock index 4bbf9e5..b7d9903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,9 +103,9 @@ dependencies = [ "bytes", "futures-util", "http 1.0.0", - "http-body", + "http-body 1.0.0", "http-body-util", - "hyper", + "hyper 1.1.0", "hyper-util", "itoa", "matchit", @@ -137,7 +137,7 @@ dependencies = [ "bytes", "futures-util", "http 1.0.0", - "http-body", + "http-body 1.0.0", "http-body-util", "mime", "pin-project-lite", @@ -189,6 +189,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bumpalo" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" + [[package]] name = "byteorder" version = "1.4.3" @@ -259,6 +265,22 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "cpufeatures" version = "0.2.11" @@ -320,6 +342,15 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "encoding_rs" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +dependencies = [ + "cfg-if", +] + [[package]] name = "env_logger" version = "0.10.1" @@ -350,6 +381,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "errno-dragonfly" version = "0.1.2" @@ -360,6 +401,12 @@ dependencies = [ "libc", ] +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + [[package]] name = "file-id" version = "0.2.1" @@ -377,7 +424,7 @@ checksum = "d4029edd3e734da6fe05b6cd7bd2960760a616bd2ddd0d59a0124746d6272af0" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.3.5", "windows-sys 0.48.0", ] @@ -387,6 +434,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -521,6 +583,25 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "h2" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.11", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "h2" version = "0.4.0" @@ -580,6 +661,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.11", + "pin-project-lite", +] + [[package]] name = "http-body" version = "1.0.0" @@ -599,7 +691,7 @@ dependencies = [ "bytes", "futures-util", "http 1.0.0", - "http-body", + "http-body 1.0.0", "pin-project-lite", ] @@ -621,6 +713,30 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "hyper" +version = "0.14.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper" version = "1.1.0" @@ -630,9 +746,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2", + "h2 0.4.0", "http 1.0.0", - "http-body", + "http-body 1.0.0", "httparse", "httpdate", "itoa", @@ -640,6 +756,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.28", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "hyper-util" version = "0.1.1" @@ -650,8 +779,8 @@ dependencies = [ "futures-channel", "futures-util", "http 1.0.0", - "http-body", - "hyper", + "http-body 1.0.0", + "hyper 1.1.0", "pin-project-lite", "socket2", "tokio", @@ -710,6 +839,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "ipnet" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" + [[package]] name = "is-terminal" version = "0.4.3" @@ -718,7 +853,7 @@ checksum = "22e18b0a45d56fe973d6db23972bf5bc46f988a4a2385deac9cc29572f09daef" dependencies = [ "hermit-abi", "io-lifetimes", - "rustix", + "rustix 0.36.8", "windows-sys 0.45.0", ] @@ -728,6 +863,15 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "js-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kqueue" version = "1.0.8" @@ -748,6 +892,12 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.151" @@ -760,6 +910,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "linux-raw-sys" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" + [[package]] name = "live-server" version = "0.6.2" @@ -773,6 +929,7 @@ dependencies = [ "mime_guess", "notify", "notify-debouncer-full", + "reqwest", "tokio", ] @@ -856,6 +1013,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "neli" version = "0.6.4" @@ -939,6 +1114,50 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "openssl" +version = "0.10.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b8419dc8cc6d866deb801274bba2e6f8f6108c1bb7fcc10ee5ab864931dbb45" +dependencies = [ + "bitflags 2.4.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3eaad34cdd97d81de97964fc7f29e2d104f483840d906ef56daa1912338460b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -957,7 +1176,7 @@ checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.3.5", "smallvec", "windows-targets 0.48.0", ] @@ -1000,6 +1219,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1063,6 +1288,15 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "regex" version = "1.7.1" @@ -1080,6 +1314,44 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "reqwest" +version = "0.11.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b1ae8d9ac08420c66222fb9096fc5de435c3c48542bc5336c51892cffafb41" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.6", + "hyper 0.14.28", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1093,13 +1365,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644" dependencies = [ "bitflags 1.3.2", - "errno", + "errno 0.2.8", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.1.4", "windows-sys 0.45.0", ] +[[package]] +name = "rustix" +version = "0.38.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +dependencies = [ + "bitflags 2.4.0", + "errno 0.3.8", + "libc", + "linux-raw-sys 0.4.12", + "windows-sys 0.52.0", +] + [[package]] name = "rustversion" version = "1.0.14" @@ -1121,12 +1406,44 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "schannel" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "security-framework" +version = "2.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.193" @@ -1259,6 +1576,40 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "tempfile" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall 0.4.1", + "rustix 0.38.28", + "windows-sys 0.48.0", +] + [[package]] name = "termcolor" version = "1.1.3" @@ -1333,6 +1684,16 @@ dependencies = [ "syn 2.0.38", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-tungstenite" version = "0.20.1" @@ -1407,6 +1768,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "tungstenite" version = "0.20.1" @@ -1485,6 +1852,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" @@ -1502,12 +1875,97 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.38", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" + +[[package]] +name = "web-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1557,6 +2015,15 @@ dependencies = [ "windows-targets 0.48.0", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-targets" version = "0.42.1" @@ -1587,6 +2054,21 @@ dependencies = [ "windows_x86_64_msvc 0.48.0", ] +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.1" @@ -1599,6 +2081,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.42.1" @@ -1611,6 +2099,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.42.1" @@ -1623,6 +2117,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.42.1" @@ -1635,6 +2135,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.42.1" @@ -1647,6 +2153,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.1" @@ -1659,6 +2171,12 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.42.1" @@ -1670,3 +2188,19 @@ name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] diff --git a/Cargo.toml b/Cargo.toml index 2b70cdf..6dcd69d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,6 @@ tokio = { version = "1.35.0", features = ["full"] } axum = { version = "0.7.2", features = ["ws"]} futures = "0.3.29" mime_guess = "2.0.3" + +[dev-dependencies] +reqwest = "0.11.23" diff --git a/tests/page/index.html b/tests/page/index.html new file mode 100644 index 0000000..8a512f8 --- /dev/null +++ b/tests/page/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + 测试 テスト test + + \ No newline at end of file diff --git a/tests/page/index.js b/tests/page/index.js new file mode 100644 index 0000000..7e2c877 --- /dev/null +++ b/tests/page/index.js @@ -0,0 +1 @@ +console.log('Hello World!') diff --git a/tests/test.rs b/tests/test.rs new file mode 100644 index 0000000..1bc5c51 --- /dev/null +++ b/tests/test.rs @@ -0,0 +1,63 @@ +use live_server::listen; +use reqwest::StatusCode; + +#[tokio::test] +async fn request() { + tokio::spawn(async { + listen("127.0.0.1", 8000, "./tests/page").await.unwrap(); + }); + + // Test requesting index.html + let response = reqwest::get("http://127.0.0.1:8000").await.unwrap(); + + assert_eq!(response.status(), StatusCode::OK); + + let content_type = response.headers().get("content-type").unwrap(); + assert_eq!(content_type, "text/html"); + + let text = response.text().await.unwrap().replace("\r\n", "\n"); + let target_text = format!( + "{}{}", + include_str!("./page/index.html"), + format!(include_str!("../src/templates/websocket.html"), "127.0.0.1", 8000) + ) + .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") + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::OK); + + let content_type = response.headers().get("content-type").unwrap(); + assert_eq!(content_type, "application/javascript"); + + let text = response.text().await.unwrap().replace("\r\n", "\n"); + let target_text = include_str!("./page/index.js").replace("\r\n", "\n"); + assert_eq!(text, target_text); + + // Test requesting non-existent html file + let response = reqwest::get("http://127.0.0.1:8000/404.html") + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + + let content_type = response.headers().get("content-type").unwrap(); + assert_eq!(content_type, "text/html"); + + let text = response.text().await.unwrap(); + assert!(text.starts_with("")); + + // Test requesting non-existent asset + let response = reqwest::get("http://127.0.0.1:8000/favicon.ico") + .await + .unwrap(); + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + + let content_type = response.headers().get("content-type").unwrap(); + assert_eq!(content_type, "image/x-icon"); +} From a9975a886706d1d0a0078cb456bb57a41c86133d Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 19:09:20 +0800 Subject: [PATCH 3/9] chore(fmt): cargo fmt --- src/lib.rs | 4 ++-- tests/test.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bfe030b..503c7ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! ## Create live server //! ``` //! use live_server::listen; -//! +//! //! async fn serve() { //! listen("127.0.0.1", 8080, "./").await.unwrap(); //! } @@ -29,7 +29,7 @@ static TX: OnceCell> = OnceCell::const_new(); /// Watch the directory and create a static server. /// ``` /// use live_server::listen; -/// +/// /// async fn serve() { /// listen("127.0.0.1", 8080, "./").await.unwrap(); /// } diff --git a/tests/test.rs b/tests/test.rs index 1bc5c51..494111b 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -19,7 +19,10 @@ async fn request() { let target_text = format!( "{}{}", include_str!("./page/index.html"), - format!(include_str!("../src/templates/websocket.html"), "127.0.0.1", 8000) + format!( + include_str!("../src/templates/websocket.html"), + "127.0.0.1", 8000 + ) ) .replace("\r\n", "\n"); assert_eq!(text, target_text); @@ -38,7 +41,7 @@ async fn request() { let target_text = include_str!("./page/index.js").replace("\r\n", "\n"); assert_eq!(text, target_text); - // Test requesting non-existent html file + // Test requesting non-existent html file let response = reqwest::get("http://127.0.0.1:8000/404.html") .await .unwrap(); @@ -51,7 +54,7 @@ async fn request() { let text = response.text().await.unwrap(); assert!(text.starts_with("")); - // Test requesting non-existent asset + // Test requesting non-existent asset let response = reqwest::get("http://127.0.0.1:8000/favicon.ico") .await .unwrap(); From 0117892b8993c9545e5067b13f13d8dc3dcaa41b Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 19:10:10 +0800 Subject: [PATCH 4/9] chore(ci): add test script --- .github/workflows/check.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 72a4142..f732f8d 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -22,3 +22,4 @@ jobs: default: true - run: cargo fmt --check - run: cargo clippy + - run: cargo test From 14555462732068ee31ba15095de0f1e14cc96c3f Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 21:41:29 +0800 Subject: [PATCH 5/9] feat: add `try_to_switch_to_an_available_port` option --- src/lib.rs | 10 ++++++---- src/main.rs | 2 +- src/server.rs | 19 +++++++++++++------ tests/test.rs | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 503c7ab..613c148 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ //! use live_server::listen; //! //! async fn serve() { -//! listen("127.0.0.1", 8080, "./").await.unwrap(); +//! listen("127.0.0.1", 8080, "./", true).await.unwrap(); //! } //! ``` //! @@ -31,13 +31,14 @@ static TX: OnceCell> = OnceCell::const_new(); /// use live_server::listen; /// /// async fn serve() { -/// listen("127.0.0.1", 8080, "./").await.unwrap(); +/// listen("127.0.0.1", 8080, "./", true).await.unwrap(); /// } /// ``` pub async fn listen>( host: &str, port: u16, root: R, + try_to_switch_to_an_available_port: bool ) -> Result<(), Box> { HOST.set(host.to_string()).unwrap(); ROOT.set(root.into()).unwrap(); @@ -45,9 +46,10 @@ pub async fn listen>( TX.set(tx).unwrap(); let watcher_future = tokio::spawn(watcher::watch()); - let server_future = tokio::spawn(server::serve(port)); + let server_future = tokio::spawn(server::serve(port, try_to_switch_to_an_available_port)); - tokio::try_join!(watcher_future, server_future)?; + let (_, server_result) = tokio::try_join!(watcher_future, server_future)?; + server_result?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 7660319..d27d9b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,5 +38,5 @@ async fn main() { }, }; - listen(&host, args.port, args.root).await.unwrap(); + listen(&host, args.port, args.root, true).await.unwrap(); } diff --git a/src/server.rs b/src/server.rs index 1aea396..4eacbc0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,13 +13,15 @@ use tokio::net::TcpListener; use crate::{HOST, PORT, ROOT, TX}; -pub async fn serve(port: u16) { - let listener = create_listener(port).await; +pub async fn serve(port: u16, switch_to_available_ports: bool) -> Result<(), String> { + let listener = create_listener(port, switch_to_available_ports).await?; let app = create_server(); axum::serve(listener, app).await.unwrap(); + + Ok(()) } -async fn create_listener(port: u16) -> TcpListener { +async fn create_listener(port: u16, switch_to_available_ports: bool) -> Result { let host = HOST.get().unwrap(); let mut port = port; // Loop until the port is available @@ -28,12 +30,17 @@ async fn create_listener(port: u16) -> TcpListener { Ok(listener) => { log::info!("Listening on http://{}:{}/", host, port); PORT.set(port).unwrap(); - break listener; + break Ok(listener); } Err(err) => { if let std::io::ErrorKind::AddrInUse = err.kind() { - log::warn!("Port {} is already in use", port); - port += 1; + if switch_to_available_ports { + log::warn!("Port {} is already in use", port); + port += 1; + } else { + log::error!("Port {} is already in use", port); + return Err(format!("Port {} is already in use", port)); + } } else { log::error!("Failed to listen on {}:{}: {}", host, port, err); } diff --git a/tests/test.rs b/tests/test.rs index 494111b..881da9f 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -4,7 +4,7 @@ use reqwest::StatusCode; #[tokio::test] async fn request() { tokio::spawn(async { - listen("127.0.0.1", 8000, "./tests/page").await.unwrap(); + listen("127.0.0.1", 8000, "./tests/page", false).await.unwrap(); }); // Test requesting index.html From 405f9735fcfaeb42d6d3ccc13846fe66fc2a6cae Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 21:42:56 +0800 Subject: [PATCH 6/9] chore(fmt): cargo fmt --- src/lib.rs | 2 +- src/server.rs | 5 ++++- tests/test.rs | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 613c148..fcc59f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ pub async fn listen>( host: &str, port: u16, root: R, - try_to_switch_to_an_available_port: bool + try_to_switch_to_an_available_port: bool, ) -> Result<(), Box> { HOST.set(host.to_string()).unwrap(); ROOT.set(root.into()).unwrap(); diff --git a/src/server.rs b/src/server.rs index 4eacbc0..469f9cd 100644 --- a/src/server.rs +++ b/src/server.rs @@ -21,7 +21,10 @@ pub async fn serve(port: u16, switch_to_available_ports: bool) -> Result<(), Str Ok(()) } -async fn create_listener(port: u16, switch_to_available_ports: bool) -> Result { +async fn create_listener( + port: u16, + switch_to_available_ports: bool, +) -> Result { let host = HOST.get().unwrap(); let mut port = port; // Loop until the port is available diff --git a/tests/test.rs b/tests/test.rs index 881da9f..41e6aff 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -4,7 +4,9 @@ use reqwest::StatusCode; #[tokio::test] async fn request() { tokio::spawn(async { - listen("127.0.0.1", 8000, "./tests/page", false).await.unwrap(); + listen("127.0.0.1", 8000, "./tests/page", false) + .await + .unwrap(); }); // Test requesting index.html From 3e1c55064549f8dc385545643d6e0729619db751 Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 21:57:01 +0800 Subject: [PATCH 7/9] chore: adjust `switch_port` name & add docs --- src/lib.rs | 7 +++++-- src/server.rs | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fcc59f7..babab1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,11 +34,14 @@ static TX: OnceCell> = OnceCell::const_new(); /// listen("127.0.0.1", 8080, "./", true).await.unwrap(); /// } /// ``` +/// When the `port` you specified is unavailable and `switch_port` +/// is set to `true`, live-server will try to switch to `8081` +/// and then `8082` until it finds an available port. pub async fn listen>( host: &str, port: u16, root: R, - try_to_switch_to_an_available_port: bool, + switch_port: bool, ) -> Result<(), Box> { HOST.set(host.to_string()).unwrap(); ROOT.set(root.into()).unwrap(); @@ -46,7 +49,7 @@ pub async fn listen>( TX.set(tx).unwrap(); let watcher_future = tokio::spawn(watcher::watch()); - let server_future = tokio::spawn(server::serve(port, try_to_switch_to_an_available_port)); + let server_future = tokio::spawn(server::serve(port, switch_port)); let (_, server_result) = tokio::try_join!(watcher_future, server_future)?; server_result?; diff --git a/src/server.rs b/src/server.rs index 469f9cd..4668d34 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,8 +13,8 @@ use tokio::net::TcpListener; use crate::{HOST, PORT, ROOT, TX}; -pub async fn serve(port: u16, switch_to_available_ports: bool) -> Result<(), String> { - let listener = create_listener(port, switch_to_available_ports).await?; +pub async fn serve(port: u16, switch_port: bool) -> Result<(), String> { + let listener = create_listener(port, switch_port).await?; let app = create_server(); axum::serve(listener, app).await.unwrap(); @@ -23,7 +23,7 @@ pub async fn serve(port: u16, switch_to_available_ports: bool) -> Result<(), Str async fn create_listener( port: u16, - switch_to_available_ports: bool, + switch_port: bool, ) -> Result { let host = HOST.get().unwrap(); let mut port = port; @@ -37,7 +37,7 @@ async fn create_listener( } Err(err) => { if let std::io::ErrorKind::AddrInUse = err.kind() { - if switch_to_available_ports { + if switch_port { log::warn!("Port {} is already in use", port); port += 1; } else { From bf502c8a364bb93e82b0c9b18e1904897a0248a2 Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 22:30:00 +0800 Subject: [PATCH 8/9] chore(fmt): cargo fmt --- src/server.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/server.rs b/src/server.rs index 4668d34..6df6b81 100644 --- a/src/server.rs +++ b/src/server.rs @@ -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 { +async fn create_listener(port: u16, switch_port: bool) -> Result { let host = HOST.get().unwrap(); let mut port = port; // Loop until the port is available From 69685b28485e3b5ed80702528d998452340c5c5b Mon Sep 17 00:00:00 2001 From: Mirus Date: Wed, 20 Dec 2023 22:44:23 +0800 Subject: [PATCH 9/9] chore(test): sleep 5s before requesting --- tests/test.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test.rs b/tests/test.rs index 41e6aff..ff73bc6 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,8 @@ +use std::time::Duration; + use live_server::listen; use reqwest::StatusCode; +use tokio::time::sleep; #[tokio::test] async fn request() { @@ -9,6 +12,8 @@ async fn request() { .unwrap(); }); + sleep(Duration::from_secs(5)).await; + // Test requesting index.html let response = reqwest::get("http://127.0.0.1:8000").await.unwrap();