From 28a459a274dba8215764ea3a3fa920cfcf99da9f Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Tue, 23 Jan 2024 09:56:22 -0800 Subject: [PATCH] chore: add test for signer status endpoint --- libsigner/src/events.rs | 16 +++++----------- libsigner/src/tests/mod.rs | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libsigner/src/events.rs b/libsigner/src/events.rs index 19b5133758..4bacbdd20b 100644 --- a/libsigner/src/events.rs +++ b/libsigner/src/events.rs @@ -384,17 +384,11 @@ impl EventReceiver for SignerEventReceiver { } let request = http_server.recv()?; - if request.method() == &HttpMethod::Get { - if request.url() == "/status" { - request - .respond(HttpResponse::from_string("OK")) - .expect("response failed"); - return Ok(SignerEvent::StatusCheck); - } - return Err(EventError::MalformedRequest(format!( - "Unrecognized GET request '{}'", - &request.url(), - ))); + if request.url() == "/status" { + request + .respond(HttpResponse::from_string("OK")) + .expect("response failed"); + return Ok(SignerEvent::StatusCheck); } if request.method() != &HttpMethod::Post { diff --git a/libsigner/src/tests/mod.rs b/libsigner/src/tests/mod.rs index deefc1018f..60889fc8c1 100644 --- a/libsigner/src/tests/mod.rs +++ b/libsigner/src/tests/mod.rs @@ -16,7 +16,7 @@ mod http; -use std::io::Write; +use std::io::{Read, Write}; use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; use std::sync::mpsc::{channel, Receiver, Sender}; use std::time::Duration; @@ -138,6 +138,24 @@ fn test_simple_signer() { num_sent += 1; } + // Test the /status endpoint + { + let mut sock = match TcpStream::connect(endpoint) { + Ok(sock) => sock, + Err(..) => { + sleep_ms(100); + return; + } + }; + let req = "GET /status HTTP/1.0\r\nConnection: close\r\n\r\n"; + sock.write_all(req.as_bytes()).unwrap(); + let mut buf = [0; 128]; + sock.read(&mut buf).unwrap(); + let res_str = std::str::from_utf8(&buf).unwrap(); + let expected_status_res = "HTTP/1.0 200 OK\r\n"; + assert_eq!(expected_status_res, &res_str[..expected_status_res.len()]); + sock.flush().unwrap(); + } }); let running_signer = signer.spawn(endpoint).unwrap();