Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge my JSONP to latest rack/master

  • Loading branch information...
commit 3e26836ea0b20f506c0aa37e1b59514b697f9c72 2 parents f95cffc + c0742aa
@runeb runeb authored
View
26 AUTHORS
@@ -0,0 +1,26 @@
+Ryan Tomayko <rtomayko@gmail.com>
+Joshua Peek <josh@joshpeek.com>
+Jeremy Kemper <jeremy@bitsweat.net>
+mynyml <mynyml@gmail.com>
+Cameron Walters <cameron.walters@gmail.com>
+Jon Crosby <jon@joncrosby.me>
+Matt Todd <chiology@gmail.com>
+Pirmin Kalberer <pka@sourcepole.ch>
+Rune Botten <rbotten@gmail.com>
+Pratik Naik <pratiknaik@gmail.com>
+Paul Sadauskas <psadauskas@gmail.com>
+Jeremy Evans <code@jeremyevans.net>
+Michael Fellinger <m.fellinger@gmail.com>
+Geoff Buesing <gbuesing@gmail.com>
+Nicolas Mérouze <nicolas.merouze@gmail.com>
+Cyril Rohr <cyril.rohr@irisa.fr>
+Harry Vangberg <harry@vangberg.name>
+James Rosen <jrosen@mitre.org>
+Mislav Marohnić <mislav.marohnic@gmail.com>
+Ben Brinckerhoff <ben@devver.net>
+Rafael Souza <rafael.ssouza@gmail.com>
+Stephen Delano <sdelano@sdelano-air.(none)>
+TJ Holowaychuk <tj@vision-media.ca>
+anupom syam <anupom.syam@gmail.com>
+ichverstehe <ichverstehe@gmail.com>
+kubicek <jiri@kubicek.cz>
View
4 README.rdoc
@@ -54,9 +54,9 @@ Git is the quickest way to the rack-contrib sources:
git clone git://github.com/rack/rack-contrib.git
-Gems are currently available from GitHub clones:
+Gems are available too:
- gem install rack-rack-contrib --source=http://gems.github.com/
+ gem install rack-contrib
Requiring 'rack/contrib' will add autoloads to the Rack modules for all of the
components included. The following example shows what a simple rackup
View
39 Rakefile
@@ -42,32 +42,31 @@ task :rdoc => ["RDOX"]
# PACKAGING =================================================================
-if defined?(Gem)
- # load gemspec
- $spec = eval(File.read('rack-contrib.gemspec'))
+require 'rubygems'
+# load gemspec
+$spec = eval(File.read('rack-contrib.gemspec'))
- def package(ext='')
- "pkg/rack-contrib-#{$spec.version}" + ext
- end
+def package(ext='')
+ "pkg/rack-contrib-#{$spec.version}" + ext
+end
- desc 'Build packages'
- task :package => %w[.gem .tar.gz].map {|e| package(e)}
+desc 'Build packages'
+task :package => %w[.gem .tar.gz].map {|e| package(e)}
- desc 'Build and install as local gem'
- task :install => package('.gem') do
- sh "gem install #{package('.gem')}"
- end
+desc 'Build and install as local gem'
+task :install => package('.gem') do
+ sh "gem install #{package('.gem')}"
+end
- directory 'pkg/'
+directory 'pkg/'
- file package('.gem') => %w[pkg/ rack-contrib.gemspec] + $spec.files do |f|
- sh "gem build rack-contrib.gemspec"
- mv File.basename(f.name), f.name
- end
+file package('.gem') => %w[pkg/ rack-contrib.gemspec] + $spec.files do |f|
+ sh "gem build rack-contrib.gemspec"
+ mv File.basename(f.name), f.name
+end
- file package('.tar.gz') => %w[pkg/] + $spec.files do |f|
- sh "git archive --format=tar HEAD | gzip > #{f.name}"
- end
+file package('.tar.gz') => %w[pkg/] + $spec.files do |f|
+ sh "git archive --format=tar HEAD | gzip > #{f.name}"
end
# GEMSPEC ===================================================================
View
2  lib/rack/contrib/access.rb
@@ -70,7 +70,7 @@ def ipmasks_for_path(env)
end
def forbidden!
- [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, '']
+ [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
end
def ip_authorized?(ipmasks)
View
2  lib/rack/contrib/deflect.rb
@@ -63,7 +63,7 @@ def call env
end
def deflect!
- [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, '']
+ [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
end
def deflect? env
View
20 lib/rack/contrib/jsonp.rb
@@ -6,6 +6,7 @@ module Rack
#
class JSONP
include Rack::Utils
+
def initialize(app)
@app = app
end
@@ -18,10 +19,11 @@ def initialize(app)
#
def call(env)
status, headers, response = @app.call(env)
+
headers = HeaderHash.new(headers)
request = Rack::Request.new(env)
- if is_json?(headers['Content-Type']) && has_callback?(request.params)
+ if is_json?(headers) && has_callback?(request)
response = pad(request.params.delete('callback'), response)
# No longer json, its javascript!
@@ -40,24 +42,24 @@ def call(env)
private
- def is_json?(header)
- header.include?('application/json')
+ def is_json?(headers)
+ headers['Content-Type'].include?('application/json')
end
- def has_callback?(params)
- params.include?('callback')
+ def has_callback?(request)
+ request.params.include?('callback')
end
# Pads the response with the appropriate callback format according to the
# JSON-P spec/requirements.
#
- # The Rack response spec indicates that it should be enumerable. The method
- # of combining all of the data into a single string makes sense since JSON
- # is returned as a full string.
+ # The Rack response spec indicates that it should be enumerable. The
+ # method of combining all of the data into a single string makes sense
+ # since JSON is returned as a full string.
#
def pad(callback, response, body = "")
response.each{ |s| body << s.to_s }
- "#{callback}(#{body})"
+ ["#{callback}(#{body})"]
end
end
View
24 rack-contrib.gemspec
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.name = 'rack-contrib'
- s.version = '0.9.3'
- s.date = '2010-01-10'
+ s.version = '1.0.0'
+ s.date = '2010-06-07'
s.description = "Contributed Rack Middleware and Utilities"
s.summary = "Contributed Rack Middleware and Utilities"
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
# = MANIFEST =
s.files = %w[
+ AUTHORS
COPYING
README.rdoc
Rakefile
@@ -27,9 +28,10 @@ Gem::Specification.new do |s|
lib/rack/contrib/cookies.rb
lib/rack/contrib/csshttprequest.rb
lib/rack/contrib/deflect.rb
- lib/rack/contrib/etag.rb
lib/rack/contrib/evil.rb
+ lib/rack/contrib/expectation_cascade.rb
lib/rack/contrib/garbagecollector.rb
+ lib/rack/contrib/host_meta.rb
lib/rack/contrib/jsonp.rb
lib/rack/contrib/lighttpd_script_name_fix.rb
lib/rack/contrib/locale.rb
@@ -41,13 +43,18 @@ Gem::Specification.new do |s|
lib/rack/contrib/profiler.rb
lib/rack/contrib/relative_redirect.rb
lib/rack/contrib/response_cache.rb
+ lib/rack/contrib/response_headers.rb
lib/rack/contrib/route_exceptions.rb
+ lib/rack/contrib/runtime.rb
lib/rack/contrib/sendfile.rb
lib/rack/contrib/signals.rb
+ lib/rack/contrib/simple_endpoint.rb
+ lib/rack/contrib/static_cache.rb
lib/rack/contrib/time_zone.rb
rack-contrib.gemspec
test/404.html
test/Maintenance.html
+ test/documents/test
test/mail_settings.rb
test/spec_rack_accept_format.rb
test/spec_rack_access.rb
@@ -55,11 +62,13 @@ Gem::Specification.new do |s|
test/spec_rack_callbacks.rb
test/spec_rack_config.rb
test/spec_rack_contrib.rb
+ test/spec_rack_cookies.rb
test/spec_rack_csshttprequest.rb
test/spec_rack_deflect.rb
- test/spec_rack_etag.rb
test/spec_rack_evil.rb
+ test/spec_rack_expectation_cascade.rb
test/spec_rack_garbagecollector.rb
+ test/spec_rack_host_meta.rb
test/spec_rack_jsonp.rb
test/spec_rack_lighttpd_script_name_fix.rb
test/spec_rack_mailexceptions.rb
@@ -70,7 +79,12 @@ Gem::Specification.new do |s|
test/spec_rack_profiler.rb
test/spec_rack_relative_redirect.rb
test/spec_rack_response_cache.rb
+ test/spec_rack_response_headers.rb
+ test/spec_rack_runtime.rb
test/spec_rack_sendfile.rb
+ test/spec_rack_simple_endpoint.rb
+ test/spec_rack_static_cache.rb
+ test/statics/test
]
# = MANIFEST =
@@ -78,7 +92,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = %w[README.rdoc COPYING]
s.add_dependency 'rack', '>= 0.9.1'
- s.add_development_dependency 'test-spec', '~> 0.9.0'
+ s.add_development_dependency 'test-spec', '>= 0.9.0'
s.add_development_dependency 'tmail', '>= 1.2'
s.add_development_dependency 'json', '>= 1.1'
View
56 test/spec_rack_access.rb
@@ -5,7 +5,7 @@
context "Rack::Access" do
setup do
- @app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, 'hello'] }
+ @app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['hello']] }
@mock_addr_1 = '111.111.111.111'
@mock_addr_2 = '192.168.1.222'
@mock_addr_localhost = '127.0.0.1'
@@ -24,42 +24,42 @@ def middleware(options = {})
app = middleware
status, headers, body = app.call(mock_env(@mock_addr_1))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
specify "default configuration should allow requests from 127.0.0.1" do
app = middleware
status, headers, body = app.call(mock_env(@mock_addr_localhost))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
end
specify "should allow remote addresses in allow_ipmasking" do
app = middleware('/' => [@mock_addr_1])
status, headers, body = app.call(mock_env(@mock_addr_1))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
end
specify "should deny remote addresses not in allow_ipmasks" do
app = middleware('/' => [@mock_addr_1])
status, headers, body = app.call(mock_env(@mock_addr_2))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
specify "should allow remote addresses in allow_ipmasks range" do
app = middleware('/' => [@mock_addr_range])
status, headers, body = app.call(mock_env(@mock_addr_2))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
end
specify "should deny remote addresses not in allow_ipmasks range" do
app = middleware('/' => [@mock_addr_range])
status, headers, body = app.call(mock_env(@mock_addr_1))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
specify "should allow remote addresses in one of allow_ipmasking" do
@@ -67,18 +67,18 @@ def middleware(options = {})
status, headers, body = app.call(mock_env(@mock_addr_2))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_localhost))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
end
specify "should deny remote addresses not in one of allow_ipmasks" do
app = middleware('/' => [@mock_addr_range, @mock_addr_localhost])
status, headers, body = app.call(mock_env(@mock_addr_1))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
specify "handles paths correctly" do
@@ -90,65 +90,65 @@ def middleware(options = {})
status, headers, body = app.call(mock_env(@mock_addr_1, "/"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/qux"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/bar"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/bar"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_2, "/foo/bar"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/bar/"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/bar/"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo///bar//quux"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo///bar//quux"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/quux"))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/quux"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/bar"))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
status, headers, body = app.call(mock_env(@mock_addr_1, "/bar").merge('HTTP_HOST' => 'foo.org'))
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
status, headers, body = app.call(mock_env(@mock_addr_localhost, "/bar").merge('HTTP_HOST' => 'foo.org'))
status.should.equal 200
- body.should.equal 'hello'
+ body.should.equal ['hello']
end
end
View
20 test/spec_rack_deflect.rb
@@ -5,7 +5,7 @@
context "Rack::Deflect" do
setup do
- @app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, 'cookies'] }
+ @app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['cookies']] }
@mock_addr_1 = '111.111.111.111'
@mock_addr_2 = '222.222.222.222'
@mock_addr_3 = '333.333.333.333'
@@ -23,7 +23,7 @@ def mock_deflect options = {}
app = mock_deflect
status, headers, body = app.call mock_env(@mock_addr_1)
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
end
specify "should deflect requests exceeding the request threshold" do
@@ -35,14 +35,14 @@ def mock_deflect options = {}
5.times do
status, headers, body = app.call env
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
end
# Remaining requests should fail for 10 seconds
10.times do
status, headers, body = app.call env
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
# Log should reflect that we have blocked an address
@@ -58,13 +58,13 @@ def mock_deflect options = {}
5.times do
status, headers, body = app.call env
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
end
# Exceeds request threshold
status, headers, body = app.call env
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
# Allow block to expire
sleep 3
@@ -73,7 +73,7 @@ def mock_deflect options = {}
5.times do
status, headers, body = app.call env
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
end
# Log should reflect block and release
@@ -88,7 +88,7 @@ def mock_deflect options = {}
10.times do
status, headers, body = app.call env
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
end
end
@@ -97,11 +97,11 @@ def mock_deflect options = {}
status, headers, body = app.call mock_env(@mock_addr_1)
status.should.equal 200
- body.should.equal 'cookies'
+ body.should.equal ['cookies']
status, headers, body = app.call mock_env(@mock_addr_2)
status.should.equal 403
- body.should.equal ''
+ body.should.equal []
end
end
View
21 test/spec_rack_jsonp.rb
@@ -11,7 +11,7 @@
app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
body = Rack::JSONP.new(app).call(request).last
- body.should.equal "#{callback}(#{test_body})"
+ body.should.equal ["#{callback}(#{test_body})"]
end
specify "should not wrap the response body in a callback if body is not JSON" do
@@ -20,7 +20,7 @@
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
body = Rack::JSONP.new(app).call(request).last
- body.join.should.equal '{"bar":"foo"}'
+ body.should.equal ['{"bar":"foo"}']
end
specify "should update content length if it was set" do
@@ -28,8 +28,10 @@
callback = 'foo'
app = lambda { |env| [200, {'Content-Type' => 'application/json', 'Content-Length' => test_body.length}, [test_body]] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
+
headers = Rack::JSONP.new(app).call(request)[1]
- headers['Content-Length'].should.equal((test_body.length + callback.length + 2).to_s) # 2 parentheses
+ expected_length = test_body.length + callback.length + "()".length
+ headers['Content-Length'].should.equal(expected_length.to_s)
end
specify "should not touch content length if not set" do
@@ -53,10 +55,19 @@
end
specify "should not change anything if no callback param is provided" do
- app = lambda { |env| [200, {'Content-Type' => 'application/json'}, ['{"bar":"foo"}']] }
+ test_body = ['{"bar":"foo"}']
+ app = lambda { |env| [200, {'Content-Type' => 'application/json'}, test_body] }
request = Rack::MockRequest.env_for("/", :params => "foo=bar")
body = Rack::JSONP.new(app).call(request).last
- body.join.should.equal '{"bar":"foo"}'
+ body.should.equal test_body
+ end
+
+ specify "should not change anything if it's not a json response" do
+ test_body = '<html><body>404 Not Found</body></html>'
+ app = lambda { |env| [404, {'Content-Type' => 'text/html'}, [test_body]] }
+ request = Rack::MockRequest.env_for("/", :params => "callback=foo", 'HTTP_ACCEPT' => 'application/json')
+ body = Rack::JSONP.new(app).call(request).last
+ body.should.equal [test_body]
end
end

0 comments on commit 3e26836

Please sign in to comment.
Something went wrong with that request. Please try again.