font_assets 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  *.swp
6
+ bin/
@@ -4,15 +4,16 @@ require 'font_assets/mime_types'
4
4
  module FontAssets
5
5
  class Middleware
6
6
 
7
- def initialize(app, origin)
7
+ def initialize(app, origin, options={})
8
8
  @app = app
9
9
  @origin = origin
10
+ @options = options
10
11
  @mime_types = FontAssets::MimeTypes.new(Rack::Mime::MIME_TYPES)
11
12
  end
12
13
 
13
14
  def access_control_headers
14
15
  {
15
- "Access-Control-Allow-Origin" => @origin,
16
+ "Access-Control-Allow-Origin" => origin,
16
17
  "Access-Control-Allow-Methods" => "GET",
17
18
  "Access-Control-Allow-Headers" => "x-requested-with",
18
19
  "Access-Control-Max-Age" => "3628800"
@@ -20,6 +21,7 @@ module FontAssets
20
21
  end
21
22
 
22
23
  def call(env)
24
+ @ssl_request = env['HTTPS'] == 'on' or env['rack.url_scheme'] == 'https'
23
25
  # intercept the "preflight" request
24
26
  if env["REQUEST_METHOD"] == "OPTIONS"
25
27
  return [200, access_control_headers, []]
@@ -33,6 +35,23 @@ module FontAssets
33
35
 
34
36
  private
35
37
 
38
+ def origin
39
+ if allow_ssl? and ssl_request?
40
+ uri = URI(@origin)
41
+ uri.scheme = "https"
42
+ uri.to_s
43
+ else
44
+ @origin
45
+ end
46
+ end
47
+
48
+ def ssl_request?
49
+ @ssl_request
50
+ end
51
+
52
+ def allow_ssl?
53
+ @options[:allow_ssl]
54
+ end
36
55
 
37
56
  def extension(path)
38
57
  "." + path.split("?").first.split(".").last
@@ -1,3 +1,3 @@
1
1
  module FontAssets
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -9,17 +9,52 @@ describe FontAssets::Middleware do
9
9
 
10
10
  context 'for GET requests' do
11
11
  context 'to font assets' do
12
- let(:app) { load_app 'http://test.origin' }
13
- let(:response) { request app, '/test.ttf' }
12
+ context 'with an app that allows ssl' do
13
+ let(:app) { load_app 'http://test.origin', allow_ssl: true }
14
+
15
+ context 'and makes a https request' do
16
+ let(:response) { request app, 'https://test.origin/test.ttf' }
17
+
18
+ context 'the response headers' do
19
+ subject { response[1] }
20
+
21
+ its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
22
+ its(["Access-Control-Max-Age"]) { should == "3628800" }
23
+ its(['Access-Control-Allow-Methods']) { should == 'GET' }
24
+ its(['Access-Control-Allow-Origin']) { should == 'https://test.origin' }
25
+ its(['Content-Type']) { should == 'application/x-font-ttf' }
26
+ end
27
+ end
28
+
29
+ context 'and makes a http request' do
30
+ let(:response) { request app, '/test.ttf' }
31
+
32
+ context 'the response headers' do
33
+ subject { response[1] }
34
+
35
+ its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
36
+ its(["Access-Control-Max-Age"]) { should == "3628800" }
37
+ its(['Access-Control-Allow-Methods']) { should == 'GET' }
38
+ its(['Access-Control-Allow-Origin']) { should == 'http://test.origin' }
39
+ its(['Content-Type']) { should == 'application/x-font-ttf' }
40
+ end
41
+ end
42
+
43
+ end
14
44
 
15
- context 'the response headers' do
16
- subject { response[1] }
45
+ context 'with an app with just the origin specified' do
46
+ let(:app) { load_app 'http://test.origin' }
47
+ let(:response) { request app, '/test.ttf' }
48
+
49
+ context 'the response headers' do
50
+ subject { response[1] }
17
51
 
18
- its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
19
- its(["Access-Control-Max-Age"]) { should == "3628800" }
20
- its(['Access-Control-Allow-Methods']) { should == 'GET' }
21
- its(['Access-Control-Allow-Origin']) { should == 'http://test.origin' }
22
- its(['Content-Type']) { should == 'application/x-font-ttf' }
52
+ its(["Access-Control-Allow-Headers"]) { should == "x-requested-with" }
53
+ its(["Access-Control-Max-Age"]) { should == "3628800" }
54
+ its(['Access-Control-Allow-Methods']) { should == 'GET' }
55
+ its(['Access-Control-Allow-Origin']) { should == 'http://test.origin' }
56
+ its(['Content-Type']) { should == 'application/x-font-ttf' }
57
+ end
23
58
  end
24
59
  end
25
60
 
@@ -66,8 +101,8 @@ describe FontAssets::Middleware do
66
101
  private
67
102
 
68
103
 
69
- def load_app(origin = 'http://test.local')
70
- FontAssets::Middleware.new(inner_app, origin)
104
+ def load_app(origin = 'http://test.local', options={})
105
+ FontAssets::Middleware.new(inner_app, origin, options)
71
106
  end
72
107
 
73
108
  def inner_app
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: font_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-08 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack