Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <[email protected]>
  • Loading branch information
loshz committed Apr 1, 2022
1 parent 08bccaa commit cbb5cac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tiny_http = "0.10"

[dev-dependencies]
prometheus-client = "0.15"
reqwest = { version = "0.11", features = ["blocking"] }
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 23 additions & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = vec![];
res.copy_to(&mut buf).unwrap();
assert_eq!(v, buf);
}
}

0 comments on commit cbb5cac

Please sign in to comment.