Permalink
Browse files

Mark all text docs as UTF-8 encoded

  • Loading branch information...
1 parent 6a95a38 commit 1b98bbf45b491e12b06615ee7f60e4981dc850cf @laurilehmijoki committed Jan 20, 2014
@@ -67,18 +67,23 @@ def gzipped_file
end
def upload_options
- opts = {
- :content_type => mime_type,
- :reduced_redundancy => config['s3_reduced_redundancy']
- }
-
- opts[:content_type] = "#{mime_type}; charset=utf-8" if ['text/html', 'application/json'].include? mime_type
+ opts = {}
+ opts[:reduced_redundancy] = config['s3_reduced_redundancy']
+ opts[:content_type] = resolve_content_type
opts[:content_encoding] = "gzip" if gzip?
opts[:cache_control] = cache_control_value if cache_control?
-
opts
end
+ def resolve_content_type
+ is_text = mime_type.start_with? 'text/' || mime_type == 'application/json'
+ if is_text
+ "#{mime_type}; charset=utf-8" # Let's consider UTF-8 as the de-facto encoding standard for text
+ else
+ mime_type
+ end
+ end
+
def cache_control_value
if max_age == 0
"no-cache, max-age=0"
@@ -120,7 +125,7 @@ def mime_type
if !!config['extensionless_mime_type'] && File.extname(path) == ""
config['extensionless_mime_type']
else
- MIME::Types.type_for(path).first
+ MIME::Types.type_for(path).first.to_s
end
end
end
@@ -75,8 +75,7 @@
s3_client = create_verifying_s3_client(file_to_upload) do |s3_object|
s3_object.should_receive(:write).with(
anything(),
- :content_type => 'text/css',
- :reduced_redundancy => false
+ include(:content_type => 'text/css; charset=utf-8')
)
end
S3Website::Upload.new(file_to_upload,
@@ -100,6 +99,26 @@
'features/support/test_site_dirs/my.blog.com/_site').perform!
end
+ describe 'encoding of text documents' do
+ context 'the user uploads a .txt document' do
+ let(:config) {{ }}
+
+ it 'sets the content type to text/plain' do
+ file_to_upload = 'file.txt'
+ s3_client = create_verifying_s3_client(file_to_upload) do |s3_object|
+ s3_object.should_receive(:write).with(
+ anything(),
+ include(:content_type => 'text/plain; charset=utf-8')
+ )
+ end
+ S3Website::Upload.new(file_to_upload,
+ s3_client,
+ config,
+ 'features/support/test_site_dirs/site-with-text-doc.com/_site').perform!
+ end
+ end
+ end
+
context 'the user specifies a mime-type for extensionless files' do
let(:config) {{
'extensionless_mime_type' => "text/html",

0 comments on commit 1b98bbf

Please sign in to comment.