Skip to content

Commit

Permalink
Fix integration tests for imageflow_server to actually check status c…
Browse files Browse the repository at this point in the history
…ode; add response bytes when failure occurs
  • Loading branch information
lilith committed Mar 12, 2021
1 parent c213550 commit 33a010e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions imageflow_http_helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl fmt::Display for FetchError {
pub type FetchResult = ::std::result::Result<FetchedResponse,FetchError>;

pub struct FetchedResponse {
pub code: reqwest::StatusCode,
pub bytes: Vec<u8>,
pub content_type: reqwest::header:: HeaderValue,
}
Expand Down Expand Up @@ -115,6 +116,7 @@ pub fn fetch(url: &str, config: Option<FetchConfig>) -> std::result::Result<Fetc
let mut source_bytes = Vec::new();
let _ = res.read_to_end(&mut source_bytes)?;
Some(FetchedResponse {
code: res.status(),
bytes: source_bytes,
content_type: res.headers().get(reqwest::header::CONTENT_TYPE).expect("content type required").clone()
})
Expand Down
23 changes: 17 additions & 6 deletions imageflow_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum ServerError {
DiskCacheReadIoError(std::io::Error),
DiskCacheWriteIoError(std::io::Error),
UpstreamResponseError(reqwest::StatusCode),
UpstreamResponseErrorWithBytes((reqwest::StatusCode, Vec<u8>)),
UpstreamHyperError(hyper::Error),
UpstreamReqwestError(reqwest::Error),
UpstreamIoError(std::io::Error),
Expand Down Expand Up @@ -150,11 +151,19 @@ fn fetch_bytes(url: &str, config: Option<FetchConfig>) -> std::result::Result<Fe
let downloaded = precise_time_ns();

match result{
Ok(r) => Ok(FetchedResponse{
bytes: r.bytes,
content_type: r.content_type,
perf: AcquirePerf { fetch_ns: downloaded - start, ..Default::default() }
}),
Ok(r) => {
if r.code.is_success() {
Ok(FetchedResponse {
bytes: r.bytes,
content_type: r.content_type,
perf: AcquirePerf { fetch_ns: downloaded - start, ..Default::default() }
})
}else if r.bytes.len() > 0{
Err(ServerError::UpstreamResponseErrorWithBytes((r.code, r.bytes)))
} else {
Err(ServerError::UpstreamResponseError(r.code))
}
},
Err(e) => Err(error_upstream(e.into()))
}
}
Expand Down Expand Up @@ -196,7 +205,7 @@ fn fetch_bytes_using_cache_by_url(cache: &CacheFolder, url: &str) -> std::result
Err(e) => Err(ServerError::DiskCacheReadIoError(e))
}
} else {
let result = fetch_bytes(url, None);
let result = fetch_bytes(url, Some(FetchConfig{ custom_ca_trust_file: None, read_error_body: Some(true) }));
if let Ok(FetchedResponse { bytes, perf, .. }) = result {
let start = precise_time_ns();
match entry.write(&bytes) {
Expand Down Expand Up @@ -623,6 +632,8 @@ pub fn serve(c: StartServerConfig) {
"Imageflow Server is healthy.")))
}, "imageflow-health");

mou.mount("/", router);

let mut chain = Chain::new(mou);

chain.link(persistent::Read::<SharedData>::both(shared_data));
Expand Down
9 changes: 8 additions & 1 deletion imageflow_server/tests/test_ir4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ lazy_static! {
fn assert_valid_image(url: &str) {
match fetch(url, Some(FetchConfig{ custom_ca_trust_file: None, read_error_body: Some(true)})){
Ok(v) => {
if !v.code.is_success(){
panic!("Error {:?} for {}", v.code, &url);
}
fc::clients::stateless::LibClient {}.get_image_info(&v.bytes).expect("Image response should be valid");
},
Err(e) => { panic!("{:?} for {}", &e, &url); }
Expand All @@ -39,7 +42,11 @@ fn assert_valid_image(url: &str) {

fn assert_ok(url: &str) {
match fetch(url, Some(FetchConfig{ custom_ca_trust_file: None, read_error_body: Some(true)})){
Ok(_) => { },
Ok(response) => {
if !response.code.is_success(){
panic!("Error {:?} for {}", response.code, &url);
}
},
Err(e) => { panic!("{:?} for {}", &e, &url); }
}
}
Expand Down

0 comments on commit 33a010e

Please sign in to comment.