rack-cors-halt 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGQ5NGVmYWY3MmIzYmJhMjNiMGQyNGZjZDg5ODcyMzdkZTVkNjdjNQ==
5
+ data.tar.gz: !binary |-
6
+ NTQ0YmI3MTU5NDExOTFhZjc4MmNmZjI4YmUxNzAxZTA2ZjA1Zjg2NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MGI4N2I1NmJlZDM2MzFmY2NhZTA0Y2FkMjkxMzVhOWRmNzllOGIxYjk5ZTNk
10
+ MzY3NjBkN2IzZDExZjVlYTljYjQyZjZlNDk2NDNkMDllYjE2NjIzYjBmZDM2
11
+ ZDUxYWRlOTBmNGQ2MDZlOTRiZjUzNzU3YWJjYjk4NTM2YjM2YWQ=
12
+ data.tar.gz: !binary |-
13
+ YzIwNmEyNjQ0NTZjNWUxM2VhNDA0NmZmOWY3NWJlZWY4YWZiZWEzOTZhNGI0
14
+ YTJiM2ZjYTY5ZWUxMzc2ZjA4OTg0NzhkNDdhMjczNzQ4ZWU3MTExYTRhNjJh
15
+ MWQ3ZGQ4MzZlNWU4NGQ2ZDJiM2Y5YzI1Njk5NzQ0MTRkYmYzN2E=
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-cors-halt.gemspec
4
+ gemspec
5
+
6
+ group :development,:test do
7
+ gem 'pry'
8
+ gem 'rack-test', require: "rack/test"
9
+ gem 'rspec'
10
+ gem 'rack-cors', require: 'rack/cors', github: 'cyu/rack-cors', branch: 'master'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ GIT
2
+ remote: git://github.com/cyu/rack-cors.git
3
+ revision: 1b4aa7fe9d23bf6dfa1a5c8ddf6e3ce5b01dc291
4
+ branch: master
5
+ specs:
6
+ rack-cors (0.3.0)
7
+
8
+ PATH
9
+ remote: .
10
+ specs:
11
+ rack-cors-halt (0.0.1)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ coderay (1.1.0)
17
+ diff-lcs (1.2.5)
18
+ method_source (0.8.2)
19
+ pry (0.10.1)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ rack (1.5.2)
24
+ rack-test (0.6.2)
25
+ rack (>= 1.0)
26
+ rspec (3.1.0)
27
+ rspec-core (~> 3.1.0)
28
+ rspec-expectations (~> 3.1.0)
29
+ rspec-mocks (~> 3.1.0)
30
+ rspec-core (3.1.7)
31
+ rspec-support (~> 3.1.0)
32
+ rspec-expectations (3.1.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.1.0)
35
+ rspec-mocks (3.1.3)
36
+ rspec-support (~> 3.1.0)
37
+ rspec-support (3.1.2)
38
+ slop (3.6.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ pry
45
+ rack-cors!
46
+ rack-cors-halt!
47
+ rack-test
48
+ rspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ rack-cors-halt
2
+ ==============
3
+
4
+ Halt a CORS request before hitting the controller.
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+
3
+ module Rack
4
+ class Cors
5
+ class Halt
6
+
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ if(env['X_RACK_CORS'].hit)
13
+ @app.call(env)
14
+ else
15
+ not_valid_reason = {
16
+ hit: env['X_RACK_CORS'].hit,
17
+ miss_reason: env['X_RACK_CORS'].miss_reason,
18
+ preflight: env['X_RACK_CORS'].preflight
19
+ }
20
+ [200, {"Content-Type" => "application/json"}, [not_valid_reason.to_json]]
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ class Cors
3
+ class Halt
4
+
5
+ VERSION = "0.0.1"
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/cors/halt/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'rack-cors-halt'
8
+ s.version = Rack::Cors::Halt::VERSION
9
+ s.date = '2014-11-18'
10
+ s.summary = 'Rack-Cors-Halt'
11
+ s.description = "Halt a CORS request before hitting the controller."
12
+ s.authors = ["Ricardo Brazão"]
13
+ s.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' }
14
+ s.license = 'MIT'
15
+
16
+
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ end
@@ -0,0 +1,56 @@
1
+ require 'pry'
2
+ require 'rack/test'
3
+ require 'rspec'
4
+
5
+ describe Rack::Cors::Halt do
6
+ include Rack::Test::Methods
7
+
8
+ attr_accessor :halt_result
9
+
10
+ def load_app(name)
11
+ test = self
12
+ Rack::Builder.new do
13
+ eval File.read(File.dirname(__FILE__) + "/#{name}.ru")
14
+
15
+ map('/valid') do
16
+ run proc { |env|
17
+ test.halt_result = env['X_RACK_CORS']
18
+ [200, {'Content-Type' => 'text/html'}, ['success']]
19
+ }
20
+ end
21
+
22
+ map('/not_to_everyone') do
23
+ run proc { |env|
24
+ test.halt_result = env['X_RACK_CORS']
25
+ [200, {'Content-Type' => 'text/html'}, ['success']]
26
+ }
27
+ end
28
+ end
29
+ end
30
+
31
+ let(:app) { load_app('test') }
32
+
33
+ it 'should make a successfull request because CORS allows it from localhost:3000' do
34
+ get '/valid', nil, {'HTTP_ORIGIN' => 'http://localhost:3000' }
35
+ expect(halt_result).not_to be_nil
36
+ expect(last_response.body).to eq('success')
37
+ end
38
+
39
+ it 'should make a successfull request because CORS allows it from localhost:9000' do
40
+ get '/valid', nil, {'HTTP_ORIGIN' => 'http://localhost:9000' }
41
+ expect(halt_result).not_to be_nil
42
+ expect(last_response.body).to eq('success')
43
+ end
44
+
45
+ it "should fail the request because CORS blocks it from localhost:3000 and it doesn't hit the controller" do
46
+ get '/not_to_everyone', nil, {'HTTP_ORIGIN' => 'http://localhost:3000' }
47
+ expect(halt_result).to be_nil
48
+ expect(JSON.parse(last_response.body)).to be_kind_of(Hash)
49
+ end
50
+
51
+ it "should make a successfull request because CORS allows it from yadayada:3000" do
52
+ get '/not_to_everyone', nil, {'HTTP_ORIGIN' => 'http://yadayada:3000' }
53
+ expect(halt_result).not_to be_nil
54
+ expect(last_response.body).to eq('success')
55
+ end
56
+ end
data/spec/halt/test.ru ADDED
@@ -0,0 +1,16 @@
1
+ require 'rack/cors/halt'
2
+ require 'rack/cors'
3
+
4
+ use Rack::Cors do
5
+ allow do
6
+ origins '*'
7
+ resource '/valid', :headers => :any, :methods => [:get, :post, :put, :path, :delete]
8
+ end
9
+
10
+ allow do
11
+ origins 'http://yadayada:3000'
12
+ resource '/not_to_everyone', :headers => :any, :methods => [:get, :post, :put, :path, :delete]
13
+ end
14
+ end
15
+
16
+ use Rack::Cors::Halt
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-cors-halt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Brazão
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Halt a CORS request before hitting the controller.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - Gemfile.lock
21
+ - README.md
22
+ - lib/rack/cors/halt.rb
23
+ - lib/rack/cors/halt/version.rb
24
+ - rack-cors-halt.gemspec
25
+ - spec/halt/halt_spec.rb
26
+ - spec/halt/test.ru
27
+ homepage:
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.2.2
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Rack-Cors-Halt
51
+ test_files:
52
+ - spec/halt/halt_spec.rb
53
+ - spec/halt/test.ru