Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add js response content-type charset #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mime_guess::{Mime, MimeGuess};
use std::fs::Metadata;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::path::PathBuf;
use std::str::FromStr;
use tokio::fs::File;

/// The result of `resolve`.
Expand Down Expand Up @@ -99,7 +100,7 @@ pub async fn resolve_path(

// If not a directory, serve this file.
if !is_dir_request {
let mime = MimeGuess::from_path(&full_path).first_or_octet_stream();
let mime = set_charset(MimeGuess::from_path(&full_path).first_or_octet_stream());
return Ok(ResolveResult::Found(file, metadata, mime));
}

Expand All @@ -119,3 +120,11 @@ pub async fn resolve_path(
let mime = MimeGuess::from_path(full_path).first_or_octet_stream();
Ok(ResolveResult::Found(file, metadata, mime))
}

fn set_charset(mime: Mime) -> Mime {
let javascript_mime: Mime = "application/javascript".parse().unwrap();
if mime == javascript_mime {
return Mime::from_str("application/javascript; charset=utf-8").unwrap();
}
mime
}
13 changes: 13 additions & 0 deletions tests/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ async fn changes_content_type_on_extension() {
);
}

#[tokio::test]
async fn changes_content_type_on_extension_js() {
let harness = Harness::new(vec![("file1.js", "this is file1")]);

let res = harness.get("/file1.js").await.unwrap();
assert_eq!(
res.headers().get(header::CONTENT_TYPE),
Some(&header::HeaderValue::from_static(
"application/javascript; charset=utf-8"
))
);
}

#[tokio::test]
async fn serves_file_with_old_if_modified_since() {
let harness = Harness::new(vec![("file1.html", "this is file1")]);
Expand Down