forked from ericallam/font_assets
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ericallam#4 from rubymaverick/mime_types
Refactor the way mime types are identified and set
- Loading branch information
Showing
9 changed files
with
279 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module FontAssets | ||
class MimeTypes | ||
DEFAULT_TYPE = 'application/octet-stream' | ||
MIME_TYPES = { | ||
'.eot' => 'application/vnd.ms-fontobject', | ||
'.svg' => 'image/svg+xml', | ||
'.ttf' => 'application/x-font-ttf', | ||
'.woff' => 'application/x-font-woff' | ||
} | ||
|
||
def initialize(types, default = DEFAULT_TYPE.dup) | ||
@types = types.dup | ||
@default = default | ||
|
||
MIME_TYPES.each_pair do |extension, type| | ||
set extension, type | ||
end | ||
end | ||
|
||
def [](extension) | ||
@types.fetch(extension, DEFAULT_TYPE.dup).dup | ||
end | ||
|
||
def font?(extension) | ||
MIME_TYPES.keys.include? extension | ||
end | ||
|
||
def set(extension, mime_type) | ||
if @types[extension].nil? || @types[extension] == @default | ||
set!(extension, mime_type) | ||
end | ||
end | ||
|
||
def set!(extension, mime_type) | ||
@types[extension] = mime_type | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'spec_helper' | ||
require 'font_assets/middleware' | ||
|
||
describe FontAssets::Middleware do | ||
it 'passes all Rack::Lint checks' do | ||
app = Rack::Lint.new(FontAssets::Middleware.new(inner_app, 'http://test.local')) | ||
request app, '/' | ||
end | ||
|
||
context 'for GET requests' do | ||
context 'to font assets' do | ||
let(:app) { load_app 'http://test.origin' } | ||
let(:call) { request app, '/test.ttf' } | ||
|
||
context 'the response headers' do | ||
subject { call[1] } | ||
|
||
its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" } | ||
its(["Access-Control-Max-Age"]) { should == "3628800" } | ||
its(['Access-Control-Allow-Methods']) { should == 'GET' } | ||
its(['Access-Control-Allow-Origin']) { should == 'http://test.origin' } | ||
its(['Content-Type']) { should == 'application/x-font-ttf' } | ||
end | ||
end | ||
|
||
context 'to non-font assets' do | ||
let(:app) { load_app } | ||
let(:call) { request app, '/' } | ||
|
||
context 'the response headers' do | ||
subject { call[1] } | ||
|
||
its(["Access-Control-Allow-Headers"]) { should be_nil } | ||
its(["Access-Control-Max-Age"]) { should be_nil } | ||
its(['Access-Control-Allow-Methods']) { should be_nil } | ||
its(['Access-Control-Allow-Origin']) { should be_nil } | ||
its(['Content-Type']) { should == 'text/plain' } | ||
end | ||
end | ||
end | ||
|
||
context 'for OPTIONS requests' do | ||
let(:app) { load_app 'http://test.options' } | ||
let(:call) { request app, '/test.ttf', :method => 'OPTIONS' } | ||
|
||
context 'the response headers' do | ||
subject { call[1] } | ||
|
||
its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" } | ||
its(["Access-Control-Max-Age"]) { should == "3628800" } | ||
its(['Access-Control-Allow-Methods']) { should == 'GET' } | ||
its(['Access-Control-Allow-Origin']) { should == 'http://test.options' } | ||
|
||
it 'should not contain a Content-Type' do | ||
subject['Content-Type'].should be_nil | ||
end | ||
end | ||
|
||
context 'the response body' do | ||
subject { call[2] } | ||
it { should be_empty } | ||
end | ||
end | ||
|
||
|
||
private | ||
|
||
|
||
def load_app(origin = 'http://test.local') | ||
FontAssets::Middleware.new(inner_app, origin) | ||
end | ||
|
||
def inner_app | ||
lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Success'] } | ||
end | ||
|
||
def request(app, path, options = {}) | ||
app.call Rack::MockRequest.env_for(path, options) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'spec_helper' | ||
require 'font_assets/mime_types' | ||
|
||
describe FontAssets::MimeTypes do | ||
context 'given an empty hash' do | ||
let(:hash) { Hash.new } | ||
subject { described_class.new(hash) } | ||
|
||
it 'adds the known mime types' do | ||
FontAssets::MimeTypes::MIME_TYPES.each_pair do |ext, type| | ||
subject[ext].should == type | ||
end | ||
end | ||
end | ||
|
||
context 'given a populated hash' do | ||
let(:default_type) { 'default/type' } | ||
let(:hash) { { '.ttf' => default_type, '.svg' => 'test/type' } } | ||
subject { described_class.new(hash, default_type) } | ||
|
||
it 'retains the non-default-matching mime types' do | ||
subject['.svg'].should == hash['.svg'] | ||
end | ||
|
||
it 'overrides the default-matching mime types' do | ||
subject['.ttf'].should_not == hash['.ttf'] | ||
end | ||
end | ||
|
||
context '#[]' do | ||
let(:types) { described_class.new({}) } | ||
|
||
it 'returns the mime type of the passed extension' do | ||
types['.woff'].should == 'application/x-font-woff' | ||
end | ||
|
||
it 'returns the default mime type for unknown extensions' do | ||
types['.bad'].should == 'application/octet-stream' | ||
end | ||
end | ||
|
||
context '#font?' do | ||
let(:types) { described_class.new({}) } | ||
|
||
it 'is true for known font extensions' do | ||
FontAssets::MimeTypes::MIME_TYPES.keys.each do |key| | ||
types.font?(key).should be_true | ||
end | ||
end | ||
|
||
it 'is false for unrecognized font extensions' do | ||
types.font?('.bad').should be_false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RSpec.configure do |config| | ||
config.treat_symbols_as_metadata_keys_with_true_values = true | ||
config.run_all_when_everything_filtered = true | ||
config.filter_run :focus | ||
end |