Tightening up path usage in requests

This commit is contained in:
Dave Hrycyszyn
2019-12-11 00:34:34 +00:00
parent 48501b9305
commit e1f5de6cb7
2 changed files with 27 additions and 18 deletions
@@ -2,6 +2,7 @@ use reqwest::Response;
pub struct Request {
base_url: String,
path: String,
}
pub trait HealthCheckRequester {
@@ -11,11 +12,14 @@ pub trait HealthCheckRequester {
impl HealthCheckRequester for Request {
fn new(base_url: String) -> Self {
Request { base_url }
Request {
base_url,
path: "/api/healthcheck".to_string(),
}
}
fn get(&self) -> Result<Response, reqwest::Error> {
let url = format!("{}/healthcheck", self.base_url);
let url = format!("{}{}", self.base_url, self.path);
reqwest::get(&url)
}
}
@@ -34,9 +38,9 @@ mod healthcheck_requests {
#[test]
#[should_panic]
fn it_returns_an_error() {
let _m = mock("GET", "/healthcheck").with_status(400).create();
let _m = mock("GET", "/api/healthcheck").with_status(400).create();
let req = Request::new(mockito::server_url());
assert_eq!(true, req.get().is_err());
assert!(req.get().is_err());
}
}
@@ -46,10 +50,10 @@ mod healthcheck_requests {
#[test]
fn it_returns_a_response_with_200_status() {
let _m = mock("GET", "/healthcheck").with_status(200).create();
let _m = mock("GET", "/api/healthcheck").with_status(200).create();
let req = Request::new(mockito::server_url());
assert_eq!(true, req.get().is_ok());
assert!(req.get().is_ok());
}
}
}
@@ -12,7 +12,10 @@ pub trait PresenceTopologyGetRequester {
impl PresenceTopologyGetRequester for Request {
fn new(base_url: String) -> Self {
Request { base_url, path: "/topology".to_string()}
Request {
base_url,
path: "/api/presence/topology".to_string(),
}
}
fn get(&self) -> Result<Topology, reqwest::Error> {
@@ -25,7 +28,6 @@ impl PresenceTopologyGetRequester for Request {
#[cfg(test)]
mod topology_requests {
use super::*;
use crate::clients::directory::presence::CocoPresence;
#[cfg(test)]
use mockito::mock;
#[cfg(test)]
@@ -34,7 +36,7 @@ mod topology_requests {
#[test]
#[should_panic]
fn it_panics() {
let _m = mock("GET", "/topology")
let _m = mock("GET", "/api/presence/topology")
.with_status(400)
.with_body("badbody")
.create();
@@ -48,21 +50,24 @@ mod topology_requests {
#[test]
fn it_returns_a_response_with_200_status_and_a_correct_topology() {
let json = fixtures::topology_response_json();
let _m = mock("GET", "/topology")
let _m = mock("GET", "/api/presence/topology")
.with_status(200)
.with_body(json)
.create();
let req = Request::new(mockito::server_url());
let resp = req.get();
assert_eq!(true, resp.is_ok());
assert_eq!(1575915097085539300, resp.unwrap().coco_nodes.first().unwrap().last_seen)
let result = req.get();
assert_eq!(true, result.is_ok());
assert_eq!(
1575915097085539300,
result.unwrap().coco_nodes.first().unwrap().last_seen
)
}
}
#[cfg(test)]
pub mod fixtures {
pub mod fixtures {
#[cfg(test)]
pub fn topology_response_json() -> String {
r#"{
pub fn topology_response_json() -> String {
r#"{
"cocoNodes": [
{
"host": "3.8.244.109:4000",
@@ -294,6 +299,6 @@ mod topology_requests {
}
]
}"#.to_string()
}
}
}
}
}