From 0f5e64650ee181a5437bfa74f4f4636791c5bcdb Mon Sep 17 00:00:00 2001 From: Tpt Date: Tue, 1 Feb 2022 21:20:22 +0100 Subject: [PATCH] Improves server test coverage --- server/src/main.rs | 72 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 40b4470a..52ba6c37 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1057,15 +1057,26 @@ mod tests { #[test] fn get_query() { - ServerTest::new().test_status( - Request::builder( - Method::GET, - "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}" - .parse() - .unwrap(), - ) - .build(), - Status::OK, + let server = ServerTest::new(); + + let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) + .with_header(HeaderName::CONTENT_TYPE, "application/trig") + .unwrap() + .with_body(" ."); + server.test_status(request, Status::NO_CONTENT); + + let request = Request::builder( + Method::GET, + "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" + .parse() + .unwrap(), + ) + .with_header(HeaderName::ACCEPT, "text/csv") + .unwrap() + .build(); + server.test_body( + request, + "s,p,o\r\nhttp://example.com,http://example.com,http://example.com", ); } @@ -1073,21 +1084,24 @@ mod tests { fn get_query_accept_star() { let request = Request::builder( Method::GET, - "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}" + "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" .parse() .unwrap(), ) .with_header(HeaderName::ACCEPT, "*/*") .unwrap() .build(); - ServerTest::new().test_status(request, Status::OK); + ServerTest::new().test_body( + request, + "{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}", + ); } #[test] fn get_query_accept_good() { let request = Request::builder( Method::GET, - "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}" + "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" .parse() .unwrap(), ) @@ -1097,7 +1111,10 @@ mod tests { ) .unwrap() .build(); - ServerTest::new().test_status(request, Status::OK); + ServerTest::new().test_body( + request, + "{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}", + ); } #[test] @@ -1128,11 +1145,26 @@ mod tests { #[test] fn get_query_union_graph() { - ServerTest::new().test_status(Request::builder( + let server = ServerTest::new(); + + let request = Request::builder(Method::PUT, "http://localhost/store/1".parse().unwrap()) + .with_header(HeaderName::CONTENT_TYPE, "text/turtle") + .unwrap() + .with_body(" ."); + server.test_status(request, Status::CREATED); + + let request = Request::builder( Method::GET, - "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph".parse() + "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph" + .parse() .unwrap(), - ).build(), Status::OK); + ).with_header(HeaderName::ACCEPT, "text/csv") + .unwrap() + .build(); + server.test_body( + request, + "s,p,o\r\nhttp://example.com,http://example.com,http://example.com", + ); } #[test] @@ -1540,6 +1572,14 @@ mod tests { response.body_mut().read_to_string(&mut buf).unwrap(); assert_eq!(response.status(), expected_status, "Error message: {}", buf); } + + fn test_body(&self, request: Request, expected_body: &str) { + let mut response = self.exec(request); + let mut buf = String::new(); + response.body_mut().read_to_string(&mut buf).unwrap(); + assert_eq!(response.status(), Status::OK, "Error message: {}", buf); + assert_eq!(&buf, expected_body); + } } #[test]