sinatra-cross_origin 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +0 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/lib/sinatra/cross_origin.rb +60 -0
- data/sinatra-cross_origin.gemspec +49 -0
- data/test/app/test_app.rb +44 -0
- data/test/test_all.rb +61 -0
- data/test/test_helper.rb +4 -0
- metadata +64 -0
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "sinatra-cross_origin"
|
5
|
+
gemspec.summary = "Cross Origin Resource Sharing helper for Sinatra"
|
6
|
+
gemspec.description = gemspec.summary
|
7
|
+
gemspec.email = "brit@britg.com"
|
8
|
+
gemspec.homepage = "http://github.com/britg/sinatra-cross_origin"
|
9
|
+
gemspec.authors = ["Brit Gardner"]
|
10
|
+
end
|
11
|
+
Jeweler::GemcutterTasks.new
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'rake/testtask'
|
17
|
+
|
18
|
+
Rake::TestTask.new do |t|
|
19
|
+
t.libs << "test"
|
20
|
+
t.test_files = FileList['test/test_all.rb']
|
21
|
+
t.verbose = true
|
22
|
+
end
|
23
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
# Helper to enable cross origin requests.
|
4
|
+
# More on Cross Origin Resource Sharing here:
|
5
|
+
# https://developer.mozilla.org/En/HTTP_access_control
|
6
|
+
#
|
7
|
+
# To enable cross origin requests for all routes:
|
8
|
+
# configure do
|
9
|
+
# enable :cross_origin
|
10
|
+
# ...
|
11
|
+
#
|
12
|
+
# To enable cross origin requests for a single domain:
|
13
|
+
# get '/' do
|
14
|
+
# cross_origin
|
15
|
+
# ...
|
16
|
+
#
|
17
|
+
# More info at:
|
18
|
+
# http://github.com/britg/sinatra-cross_origin
|
19
|
+
#
|
20
|
+
|
21
|
+
module Sinatra
|
22
|
+
module CrossOrigin
|
23
|
+
module Helpers
|
24
|
+
|
25
|
+
# Apply cross origin headers either
|
26
|
+
# from global config or custom config passed
|
27
|
+
# as a hash
|
28
|
+
def cross_origin(hash=nil)
|
29
|
+
return unless request.env['HTTP_ORIGIN']
|
30
|
+
options.set hash if hash
|
31
|
+
|
32
|
+
origin = options.allow_origin == :any ? request.env['HTTP_ORIGIN'] : options.allow_origin
|
33
|
+
methods = options.allow_methods.map{ |m| m.to_s.upcase! }.join(', ')
|
34
|
+
|
35
|
+
headers 'Access-Control-Allow-Origin' => origin,
|
36
|
+
'Access-Control-Allow-Methods' => methods,
|
37
|
+
'Access-Control-Allow-Credentials' => options.allow_credentials.to_s,
|
38
|
+
'Access-Control-Max-Age' => options.max_age.to_s
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.registered(app)
|
43
|
+
|
44
|
+
app.helpers CrossOrigin::Helpers
|
45
|
+
|
46
|
+
app.set :cross_origin, false
|
47
|
+
app.set :allow_origin, :any
|
48
|
+
app.set :allow_methods, [:post, :get, :options]
|
49
|
+
app.set :allow_credentials, true
|
50
|
+
app.set :max_age, 1728000
|
51
|
+
|
52
|
+
|
53
|
+
app.before do
|
54
|
+
cross_origin if options.cross_origin
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sinatra-cross_origin}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brit Gardner"]
|
12
|
+
s.date = %q{2009-12-28}
|
13
|
+
s.description = %q{Cross Origin Resource Sharing helper for Sinatra}
|
14
|
+
s.email = %q{brit@britg.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"lib/sinatra/cross_origin.rb",
|
23
|
+
"sinatra-cross_origin.gemspec",
|
24
|
+
"test/app/test_app.rb",
|
25
|
+
"test/test_all.rb",
|
26
|
+
"test/test_helper.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/britg/sinatra-cross_origin}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.5}
|
32
|
+
s.summary = %q{Cross Origin Resource Sharing helper for Sinatra}
|
33
|
+
s.test_files = [
|
34
|
+
"test/app/test_app.rb",
|
35
|
+
"test/test_all.rb",
|
36
|
+
"test/test_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
class TestApp < Sinatra::Base
|
4
|
+
register Sinatra::CrossOrigin
|
5
|
+
|
6
|
+
get '/' do
|
7
|
+
"Hello"
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/defaults' do
|
11
|
+
cross_origin
|
12
|
+
"Defaults"
|
13
|
+
end
|
14
|
+
|
15
|
+
get '/same_origin' do
|
16
|
+
"Same origin!"
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/allow_any_origin' do
|
20
|
+
cross_origin
|
21
|
+
"Allowing any origin"
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/allow_specific_origin' do
|
25
|
+
cross_origin :allow_origin => 'http://example.com'
|
26
|
+
"Allowing any origin"
|
27
|
+
end
|
28
|
+
|
29
|
+
get '/allow_methods' do
|
30
|
+
cross_origin :allow_methods => params[:methods].split(', ')
|
31
|
+
"Allowing methods"
|
32
|
+
end
|
33
|
+
|
34
|
+
get '/dont_allow_credentials' do
|
35
|
+
cross_origin :allow_credentials => false
|
36
|
+
"Not allowing credentials"
|
37
|
+
end
|
38
|
+
|
39
|
+
get '/set_max_age' do
|
40
|
+
cross_origin :max_age => params[:maxage]
|
41
|
+
"Setting max age"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/test/test_all.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HelloTest < Test::Unit::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
def app
|
7
|
+
TestApp
|
8
|
+
end
|
9
|
+
|
10
|
+
def _defaults
|
11
|
+
get '/defaults', {}, {'HTTP_ORIGIN' => 'http://localhost'}
|
12
|
+
assert last_response.ok?
|
13
|
+
assert_equal 'http://localhost', last_response.headers['Access-Control-Allow-Origin']
|
14
|
+
assert_equal 'POST, GET, OPTIONS', last_response.headers['Access-Control-Allow-Methods']
|
15
|
+
assert_equal 'true', last_response.headers['Access-Control-Allow-Credentials']
|
16
|
+
assert_equal "1728000", last_response.headers['Access-Control-Max-Age']
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_it_says_hello
|
20
|
+
get '/'
|
21
|
+
assert last_response.ok?
|
22
|
+
assert_equal 'Hello', last_response.body
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_cross_origin_is_silent_on_same_origin_request
|
26
|
+
get '/same_origin'
|
27
|
+
assert last_response.ok?
|
28
|
+
assert_equal nil, last_response.headers['Access-Control-Allow-Origin']
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_allow_any_origin
|
32
|
+
get '/allow_any_origin', {}, {'HTTP_ORIGIN' => 'http://localhost'}
|
33
|
+
assert last_response.ok?
|
34
|
+
assert_equal 'http://localhost', last_response.headers['Access-Control-Allow-Origin']
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_allow_specific_origin
|
38
|
+
get '/allow_specific_origin', {}, {'HTTP_ORIGIN' => 'http://example.com'}
|
39
|
+
assert last_response.ok?
|
40
|
+
assert_equal 'http://example.com', last_response.headers['Access-Control-Allow-Origin']
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_allow_methods
|
44
|
+
get '/allow_methods', {:methods=>'get, post'}, {'HTTP_ORIGIN' => 'http://localhost'}
|
45
|
+
assert last_response.ok?
|
46
|
+
assert_equal 'GET, POST', last_response.headers['Access-Control-Allow-Methods']
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_dont_allow_credentials
|
50
|
+
get '/dont_allow_credentials', {}, {'HTTP_ORIGIN' => 'http://localhost'}
|
51
|
+
assert last_response.ok?
|
52
|
+
assert_equal "false", last_response.headers['Access-Control-Allow-Credentials']
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_set_max_age
|
56
|
+
get '/set_max_age', {:maxage=>"60"}, {'HTTP_ORIGIN' => 'http://localhost'}
|
57
|
+
assert last_response.ok?
|
58
|
+
assert_equal "60", last_response.headers['Access-Control-Max-Age']
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-cross_origin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brit Gardner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-28 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Cross Origin Resource Sharing helper for Sinatra
|
17
|
+
email: brit@britg.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- lib/sinatra/cross_origin.rb
|
29
|
+
- sinatra-cross_origin.gemspec
|
30
|
+
- test/app/test_app.rb
|
31
|
+
- test/test_all.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/britg/sinatra-cross_origin
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Cross Origin Resource Sharing helper for Sinatra
|
61
|
+
test_files:
|
62
|
+
- test/app/test_app.rb
|
63
|
+
- test/test_all.rb
|
64
|
+
- test/test_helper.rb
|