Skip to content

Commit

Permalink
Serve json source files as source pages instead of raw
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Oct 17, 2022
1 parent 73a3f59 commit c465bc8
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,11 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
};

let (file, file_content) = if let Some(blob) = blob {
let is_text = blob.mime.starts_with("text") || blob.mime == "application/json";
// serve the file with DatabaseFileHandler if file isn't text and not empty
if !blob.mime.starts_with("text") && !blob.is_empty() {
if !is_text && !blob.is_empty() {
return Ok(DbFile(blob).serve());
} else if blob.mime.starts_with("text") && !blob.is_empty() {
} else if is_text && !blob.is_empty() {
let path = blob
.path
.rsplit_once('/')
Expand Down Expand Up @@ -478,4 +479,29 @@ mod tests {
Ok(())
});
}

#[test]
fn json_is_served_as_rendered_html() {
wrapper(|env| {
env.fake_release()
.name("fake")
.version("0.1.0")
.source_file("config.json", b"{}")
.create()?;

let web = env.frontend();

let response = web.get("/crate/fake/0.1.0/source/config.json").send()?;
assert!(response
.headers()
.get("content-type")
.unwrap()
.to_str()
.unwrap()
.starts_with("text/html"));
assert!(response.text()?.starts_with(r#"<!DOCTYPE html>"#));

Ok(())
});
}
}

0 comments on commit c465bc8

Please sign in to comment.