👌 Simplify test
Remove mock http server
This commit is contained in:
parent
ac883c1ddf
commit
8f019882d0
3 changed files with 11 additions and 104 deletions
51
Cargo.lock
generated
51
Cargo.lock
generated
|
|
@ -135,16 +135,6 @@ version = "1.7.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457"
|
||||
|
||||
[[package]]
|
||||
name = "assert-json-diff"
|
||||
version = "2.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "assert_cfg"
|
||||
version = "0.1.0"
|
||||
|
|
@ -824,16 +814,6 @@ version = "1.0.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "combine"
|
||||
version = "4.6.7"
|
||||
|
|
@ -2703,7 +2683,6 @@ dependencies = [
|
|||
"lru",
|
||||
"memmap2",
|
||||
"mime_guess",
|
||||
"mockito",
|
||||
"notify",
|
||||
"parking_lot",
|
||||
"rand 0.8.5",
|
||||
|
|
@ -3083,30 +3062,6 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mockito"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "652cd6d169a36eaf9d1e6bce1a221130439a966d7f27858af66a33a66e9c4ee2"
|
||||
dependencies = [
|
||||
"assert-json-diff",
|
||||
"bytes",
|
||||
"colored",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"log",
|
||||
"rand 0.8.5",
|
||||
"regex",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"similar",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "muda"
|
||||
version = "0.15.3"
|
||||
|
|
@ -4928,12 +4883,6 @@ version = "0.3.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
|
||||
|
||||
[[package]]
|
||||
name = "similar"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"
|
||||
|
||||
[[package]]
|
||||
name = "siphasher"
|
||||
version = "0.3.11"
|
||||
|
|
|
|||
|
|
@ -130,4 +130,3 @@ tracing-subscriber = "0.3"
|
|||
tokio-test = "0.4"
|
||||
tempfile = "3"
|
||||
rand = { version = "0.8", features = ["small_rng"] }
|
||||
mockito = "1.2"
|
||||
|
|
|
|||
|
|
@ -164,54 +164,13 @@ fn parse_ip_range(line: &str) -> Option<(IpAddr, IpAddr)> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use super::*;
|
||||
use async_compression::tokio::write::GzipEncoder;
|
||||
use mockito::{Server, ServerGuard};
|
||||
use std::thread::{self, JoinHandle};
|
||||
use futures::stream::once;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
struct TestServer {
|
||||
server: ServerGuard,
|
||||
mock: mockito::Mock,
|
||||
url: String,
|
||||
_thread: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl TestServer {
|
||||
fn new(content: &[u8], headers: &[(&str, &str)]) -> Self {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let server_thread = thread::spawn(move || {
|
||||
let mut server = Server::new();
|
||||
let url = server.url();
|
||||
let mock = server.mock("GET", "/").with_status(200);
|
||||
|
||||
tx.send((server, mock, url)).unwrap();
|
||||
thread::park();
|
||||
});
|
||||
|
||||
let (server, mut mock, url) = rx.recv().unwrap();
|
||||
|
||||
mock = mock.with_body(content);
|
||||
for &(key, value) in headers {
|
||||
mock = mock.with_header(key, value);
|
||||
}
|
||||
let mock = mock.create();
|
||||
|
||||
TestServer {
|
||||
server,
|
||||
mock,
|
||||
url,
|
||||
_thread: server_thread,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestServer {
|
||||
fn drop(&mut self) {
|
||||
self._thread.thread().unpark();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_blocklist_gzipped() -> Result<()> {
|
||||
let blocklist = r#"
|
||||
|
|
@ -227,13 +186,13 @@ mod tests {
|
|||
encoder.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
let server = TestServer::new(&gzipped_blocklist, &[("Content-Encoding", "gzip")]);
|
||||
|
||||
let blocklist = Blocklist::load_from_url(&server.url).await?;
|
||||
let stream = StreamReader::new(Box::pin(once(async {
|
||||
Ok::<_, std::io::Error>(Cursor::new(gzipped_blocklist))
|
||||
})));
|
||||
let blocklist = Blocklist::create_from_stream(stream).await?;
|
||||
assert!(blocklist.is_blocked(&"192.168.1.1".parse().unwrap()));
|
||||
assert!(!blocklist.is_blocked(&"8.8.8.8".parse().unwrap()));
|
||||
|
||||
server.mock.assert();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -245,13 +204,13 @@ mod tests {
|
|||
localv6:2001:db8::1-2001:db8::ffff
|
||||
"#;
|
||||
|
||||
let server = TestServer::new(blocklist.as_bytes(), &[]);
|
||||
|
||||
let blocklist = Blocklist::load_from_url(&server.url).await?;
|
||||
let stream = StreamReader::new(Box::pin(once(async {
|
||||
Ok::<_, std::io::Error>(Cursor::new(blocklist.as_bytes().to_vec()))
|
||||
})));
|
||||
let blocklist = Blocklist::create_from_stream(stream).await?;
|
||||
assert!(blocklist.is_blocked(&"192.168.1.1".parse().unwrap()));
|
||||
assert!(!blocklist.is_blocked(&"8.8.8.8".parse().unwrap()));
|
||||
|
||||
server.mock.assert();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue