From 4ce2ee6fd81bcb0f90835dcbab808b90954d6542 Mon Sep 17 00:00:00 2001 From: Dan Bond Date: Thu, 7 Apr 2022 08:39:51 -0700 Subject: [PATCH] test 404/405 responses Signed-off-by: Dan Bond --- Cargo.toml | 2 +- src/server.rs | 12 ++++++------ tests/server.rs | 10 ++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3dcdede..581ee67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "metrics_server" -version = "0.4.0" +version = "0.4.1" authors = ["Dan Bond "] edition = "2021" rust-version = "1.58" diff --git a/src/server.rs b/src/server.rs index 571a1b5..bed524f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -93,16 +93,16 @@ impl MetricsServer { break; } - // Only respond to GET requests. - if req.method() != &Method::Get { - let res = Response::empty(405); + // Only serve the /metrics path. + if req.url() != "/metrics" { + let res = Response::empty(404); let _ = req.respond(res); continue; } - // Only serve the /metrics path. - if req.url() != "/metrics" { - let res = Response::empty(404); + // Only respond to GET requests. + if req.method() != &Method::Get { + let res = Response::empty(405); let _ = req.respond(res); continue; } diff --git a/tests/server.rs b/tests/server.rs index 6abfb23..231ded2 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -10,6 +10,16 @@ fn test_http_server_invalid_address() { fn test_http_server_serve() { let server = MetricsServer::new("localhost:8001"); + // Assert calls to non /metrics endpoint returns 404. + let res = reqwest::blocking::get("http://localhost:8001/invalid").unwrap(); + assert_eq!(404, res.status()); + + // Assert non GET requests to /metrics endpoint returns 405. + let client = reqwest::blocking::Client::new(); + let res = client.post("http://localhost:8001/metrics").send().unwrap(); + assert_eq!(405, res.status()); + + // Assert calls to /metrics return correct response. for i in 0..3 { // Create mock data and update the metrics server. let v = vec![i];