rack-conditional-forms 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +11 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/rack/conditional-forms/version.rb +5 -0
- data/lib/rack/conditional-forms.rb +46 -0
- data/lib/rack-conditional-forms.rb +1 -0
- data/rack-conditional-forms.gemspec +17 -0
- data/spec/conditional-forms-spec.rb +75 -0
- data/spec/helper.rb +28 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jamie Hodge
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Rack::Conditional::Forms
|
2
|
+
|
3
|
+
Rack::ConditionalForms allows forms to override `If-Match` and `If-Unmodified-Since` headers in forms, using `_if_match` and `_if_unmodified_since` keys, respectively,
|
4
|
+
allowing conditional `POST`, `PUT` and `DELETE` requests from the browser.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'rack-conditional-forms'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install rack-conditional-forms
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
require 'rack/conditional-forms'
|
23
|
+
use Rack::ConditionalForms
|
24
|
+
|
25
|
+
To use Rack::SslEnforcer in your Rails application, add the following line to your application config file (config/application.rb for Rails 3, config/environment.rb for Rails 2):
|
26
|
+
|
27
|
+
config.middleware.use Rack::ConditionalForms
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'conditional-forms/version'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
class ConditionalForms
|
6
|
+
|
7
|
+
IF_MATCH_PARAM_KEY = '_if_match'.freeze
|
8
|
+
IF_UNMODIFIED_SINCE_KEY = '_if_unmodified_since'.freeze
|
9
|
+
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
if (match = if_match(env)) && !match.empty?
|
16
|
+
env['rack.conditionalforms.original_match'] = env['HTTP_IF_MATCH']
|
17
|
+
env['HTTP_IF_MATCH'] = if_match(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
if (unmodified_since = if_unmodified_since(env)) && !unmodified_since.empty?
|
21
|
+
env['rack.conditionalforms.original_unmodified_since'] = env['HTTP_IF_UNMODIFIED_SINCE']
|
22
|
+
env['HTTP_IF_UNMODIFIED_SINCE'] = if_unmodified_since(env)
|
23
|
+
end
|
24
|
+
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
|
28
|
+
def if_match(env)
|
29
|
+
request_value(env, IF_MATCH_PARAM_KEY)
|
30
|
+
end
|
31
|
+
|
32
|
+
def if_unmodified_since(env)
|
33
|
+
request_value(env, IF_UNMODIFIED_SINCE_KEY)
|
34
|
+
end
|
35
|
+
|
36
|
+
def request_value(env, key)
|
37
|
+
req = Request.new(env)
|
38
|
+
value = req.POST[key]
|
39
|
+
value.to_s.upcase
|
40
|
+
rescue EOFError
|
41
|
+
""
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/ssl-conditional-forms'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack/conditional-forms/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jamie Hodge"]
|
6
|
+
gem.email = ["jamiehodge@me.com"]
|
7
|
+
gem.description = %q{A Rack middleware to support conditional requests from the browser}
|
8
|
+
gem.summary = %q{Rack::ConditionalForms is a Rack middleware to support conditional requests from the browser}
|
9
|
+
gem.homepage = "http://github.com/jamiehodge/rack-conditional-forms"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "rack-conditional-forms"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::ConditionalForms::VERSION
|
17
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
describe Rack::ConditionalForms do
|
4
|
+
|
5
|
+
def app
|
6
|
+
mock_app
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'If-Match' do
|
10
|
+
|
11
|
+
it 'must override If-Match on POST' do
|
12
|
+
post '/', _if_match: '123'
|
13
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_equal '123'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'wont override If-Match on POST if not present' do
|
17
|
+
post '/'
|
18
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'must override If-Match on PUT' do
|
22
|
+
put '/', _if_match: '123'
|
23
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_equal '123'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'wont override If-Match on PUT if not present' do
|
27
|
+
put '/'
|
28
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'must override If-Match on DELETE' do
|
32
|
+
delete '/', _if_match: '123'
|
33
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_equal '123'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'wont override If-Match on DELETE if not present' do
|
37
|
+
delete '/'
|
38
|
+
JSON.parse(last_response.body)['HTTP_IF_MATCH'].must_be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'If-Unmodified-Since' do
|
43
|
+
|
44
|
+
it 'must override If-Unmodified-Since on POST' do
|
45
|
+
post '/', _if_unmodified_since: '123'
|
46
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_equal '123'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'wont override If-Unmodified-Since on POST if not present' do
|
50
|
+
post '/'
|
51
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'must override If-Unmodified-Since on PUT' do
|
55
|
+
put '/', _if_unmodified_since: '123'
|
56
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_equal '123'
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'wont override If-Unmodified-Since on PUT if not present' do
|
60
|
+
put '/'
|
61
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_be_nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'must override If-Unmodified-Since on DELETE' do
|
65
|
+
delete '/', _if_unmodified_since: '123'
|
66
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_equal '123'
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'wont override If-Unmodified-Since on DELETE if not present' do
|
70
|
+
delete '/'
|
71
|
+
JSON.parse(last_response.body)['HTTP_IF_UNMODIFIED_SINCE'].must_be_nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
require 'rack/conditional-forms'
|
8
|
+
|
9
|
+
class MiniTest::Spec
|
10
|
+
include Rack::Test::Methods
|
11
|
+
|
12
|
+
def app
|
13
|
+
Rack::Lint.new(@app)
|
14
|
+
end
|
15
|
+
|
16
|
+
def mock_app
|
17
|
+
main_app = lambda { |env|
|
18
|
+
request = Rack::Request.new(env)
|
19
|
+
headers = { 'Content-Type' => 'text/html' }
|
20
|
+
[200, headers, { 'HTTP_IF_MATCH' => request.env['HTTP_IF_MATCH'], 'HTTP_IF_UNMODIFIED_SINCE' => request.env['HTTP_IF_UNMODIFIED_SINCE'] }.to_json ]
|
21
|
+
}
|
22
|
+
|
23
|
+
builder = Rack::Builder.new
|
24
|
+
builder.use Rack::ConditionalForms
|
25
|
+
builder.run main_app
|
26
|
+
@app = builder.to_app
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-conditional-forms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jamie Hodge
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A Rack middleware to support conditional requests from the browser
|
15
|
+
email:
|
16
|
+
- jamiehodge@me.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/rack-conditional-forms.rb
|
27
|
+
- lib/rack/conditional-forms.rb
|
28
|
+
- lib/rack/conditional-forms/version.rb
|
29
|
+
- rack-conditional-forms.gemspec
|
30
|
+
- spec/conditional-forms-spec.rb
|
31
|
+
- spec/helper.rb
|
32
|
+
homepage: http://github.com/jamiehodge/rack-conditional-forms
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.11
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Rack::ConditionalForms is a Rack middleware to support conditional requests
|
56
|
+
from the browser
|
57
|
+
test_files:
|
58
|
+
- spec/conditional-forms-spec.rb
|
59
|
+
- spec/helper.rb
|