From cbb5cac23ed2e8d3c7fced6d96d5de8dd30df5a9 Mon Sep 17 00:00:00 2001 From: Dan Bond Date: Fri, 1 Apr 2022 14:15:32 -0700 Subject: [PATCH] add integration tests Signed-off-by: Dan Bond --- Cargo.toml | 1 + README.md | 6 ++---- src/server.rs | 2 +- tests/server.rs | 24 +++++++++++++++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60c49b3..44b2dfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ tiny_http = "0.10" [dev-dependencies] prometheus-client = "0.15" +reqwest = { version = "0.11", features = ["blocking"] } diff --git a/README.md b/README.md index dc94c00..fe88b54 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,17 @@ # Metrics Server -> _Note: this library is **NOT** production ready! Use with caution and submit bugs where possible._ - [![CI](https://github.com/loshz/metrics_server/actions/workflows/ci.yml/badge.svg)](https://github.com/loshz/metrics_server/actions/workflows/ci.yml) [![Version](https://img.shields.io/crates/v/metrics_server.svg)](https://crates.io/crates/metrics_server) [![Docs](https://docs.rs/metrics_server/badge.svg)](https://docs.rs/metrics_server) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/loshz/metrics_server/blob/main/LICENSE) -A hassle-free, single-responsibility, safe HTTP server used to easily expose metrics in your application. +A hassle-free, single-responsibility, safe HTTP server used to easily expose metrics in an application. ## Usage Include the lib in your `Cargo.toml` dependencies: ```toml [dependencies] -metrics_server = "0.1" +metrics_server = "0.2" ``` In your application: diff --git a/src/server.rs b/src/server.rs index e3d771e..2ceff0e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -87,7 +87,7 @@ impl MetricsServer { } }; - // Only respond to GET requests(?). + // Only respond to GET requests. if req.method() != &Method::Get { let res = Response::empty(405); if let Err(e) = req.respond(res) { diff --git a/tests/server.rs b/tests/server.rs index d18b949..b61f207 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -2,7 +2,29 @@ use metrics_server::MetricsServer; #[test] #[should_panic] -fn test_server_serve() { +fn test_server_invalid_address() { let server = MetricsServer::new(); server.serve("invalid:99999999"); } + +#[test] +fn test_server_serve() { + let server = MetricsServer::new(); + server.serve("localhost:8001"); + + for i in 0..3 { + // Create mock data and update the metrics server. + let v = vec![i]; + let b = server.update(v.clone()); + assert_eq!(1, b); + + // Make a HTTP request to the metrics server. + let mut res = reqwest::blocking::get("http://localhost:8001/metrics").unwrap(); + assert_eq!(200, res.status()); + + // Read the response body and check it is what we expected. + let mut buf: Vec = vec![]; + res.copy_to(&mut buf).unwrap(); + assert_eq!(v, buf); + } +}