Skip to content

Commit a0ff6c1

Browse files
Nemo157syphar
authored andcommitted
Serve json source files as source pages instead of raw
1 parent a474b17 commit a0ff6c1

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/web/source.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,11 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
273273
};
274274

275275
let (file, file_content) = if let Some(blob) = blob {
276+
let is_text = blob.mime.starts_with("text") || blob.mime == "application/json";
276277
// serve the file with DatabaseFileHandler if file isn't text and not empty
277-
if !blob.mime.starts_with("text") && !blob.is_empty() {
278+
if !is_text && !blob.is_empty() {
278279
return Ok(DbFile(blob).serve());
279-
} else if blob.mime.starts_with("text") && !blob.is_empty() {
280+
} else if is_text && !blob.is_empty() {
280281
let path = blob
281282
.path
282283
.rsplit_once('/')
@@ -478,4 +479,29 @@ mod tests {
478479
Ok(())
479480
});
480481
}
482+
483+
#[test]
484+
fn json_is_served_as_rendered_html() {
485+
wrapper(|env| {
486+
env.fake_release()
487+
.name("fake")
488+
.version("0.1.0")
489+
.source_file("config.json", b"{}")
490+
.create()?;
491+
492+
let web = env.frontend();
493+
494+
let response = web.get("/crate/fake/0.1.0/source/config.json").send()?;
495+
assert!(response
496+
.headers()
497+
.get("content-type")
498+
.unwrap()
499+
.to_str()
500+
.unwrap()
501+
.starts_with("text/html"));
502+
assert!(response.text()?.starts_with(r#"<!DOCTYPE html>"#));
503+
504+
Ok(())
505+
});
506+
}
481507
}

0 commit comments

Comments
 (0)