Skip to content

Commit

Permalink
test 404/405 responses
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <[email protected]>
  • Loading branch information
loshz committed Apr 7, 2022
1 parent 7de63ef commit 4ce2ee6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metrics_server"
version = "0.4.0"
version = "0.4.1"
authors = ["Dan Bond <[email protected]>"]
edition = "2021"
rust-version = "1.58"
Expand Down
12 changes: 6 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 4ce2ee6

Please sign in to comment.