Permalink
Browse files

Adding specs, Adding Bundler, Fixing some rack specification errors

  • Loading branch information...
robertodecurnex committed Dec 28, 2010
1 parent 66f7212 commit 08dc384f342fa6f8c3e19be93ca9f78ab25d5f45
Showing with 141 additions and 9 deletions.
  1. +2 −0 .bundle/config
  2. +1 −0 .rspec
  3. +3 −0 Gemfile
  4. +27 −0 Gemfile.lock
  5. +12 −9 lib/rack/jsonp.rb
  6. +2 −0 rack-jsonp-middleware.gemspec
  7. +90 −0 spec/jsonp_spec.rb
  8. +4 −0 spec/spec_helper.rb
View
@@ -0,0 +1,2 @@
+---
+BUNDLE_WITHOUT: ""
View
1 .rspec
@@ -0,0 +1 @@
+--color
View
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
View
@@ -0,0 +1,27 @@
+PATH
+ remote: .
+ specs:
+ rack-jsonp-middleware (0.0.1)
+ rack
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.2)
+ rack (1.2.1)
+ rspec (2.3.0)
+ rspec-core (~> 2.3.0)
+ rspec-expectations (~> 2.3.0)
+ rspec-mocks (~> 2.3.0)
+ rspec-core (2.3.1)
+ rspec-expectations (2.3.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.3.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rack
+ rack-jsonp-middleware!
+ rspec (>= 1.3.0)
View
@@ -1,30 +1,33 @@
module Rack
+
class JSONP
- include Rack::Utils
def initialize(app)
@app = app
end
-
def call(env)
request = Rack::Request.new(env)
- requesting_jsonp = Pathname(request.env['REQUEST_PATH']).extname =~ /^\.jsonp$/i
+ requesting_jsonp = Pathname(request.env['PATH_INFO']).extname =~ /^\.jsonp$/i
callback = request.params['callback']
return [400,{},[]] if requesting_jsonp && !callback
- env['REQUEST_URI'].sub!(/\.jsonp/i, '.json') if requesting_jsonp
+ env['PATH_INFO'].sub!(/\.jsonp/i, '.json') if requesting_jsonp
- status, headers, response = @app.call(env)
+ status, headers, body = @app.call(env)
if requesting_jsonp
- response.body = "#{callback}(#{response.body});"
- headers['Content-Length'] = response.body.length.to_s
+ json = ""
+ body.each { |s| json << s }
+ body = ["#{callback}(#{json});"]
+ headers['Content-Length'] = body[0].length.to_s
headers['Content-Type'] = 'application/javascript'
end
- [status, headers, response]
+ [status, headers, body]
end
+
end
-end
+
+end
@@ -11,6 +11,8 @@ Gem::Specification.new do |s|
s.add_dependency 'rack', '>=0'
+ s.add_development_dependency 'rspec', '>= 1.3.0'
+
s.rubygems_version = '1.3.7'
s.files = [
'README.md',
View
@@ -0,0 +1,90 @@
+require 'spec_helper.rb'
+
+describe Rack::JSONP do
+
+ before :each do
+ @response_status = 200
+ @response_headers = {
+ 'Content-Type' => 'application/json',
+ 'Content-Length' => '15',
+ }
+ @response_body = ['{"key":"value"}']
+
+ @app = lambda do
+ [@response_status, @response_headers, @response_body]
+ end
+
+ @callback = 'J50Npi.success'
+ end
+
+ describe 'when a valid jsonp request is made' do
+
+ before :each do
+ @request = Rack::MockRequest.env_for('/action.jsonp', :params => "callback=#{@callback}")
+ @jsonp_response = Rack::JSONP.new(@app).call(@request)
+ @jsonp_response_status, @jsonp_response_headers, @jsonp_response_body = @jsonp_response
+ end
+
+ it 'should not modify the response status code' do
+ @jsonp_response_status.should equal @response_status
+ end
+
+ it 'should update the response content length to the new value' do
+ @jsonp_response_headers['Content-Length'].should == '32'
+ end
+
+ it 'should set the response content type as application/javascript' do
+ @jsonp_response_headers['Content-Type'].should == 'application/javascript'
+ end
+
+ it 'should wrap the response body in the Javasript callback' do
+ @jsonp_response_body.should == ["#{@callback}(#{@response_body});"]
+ end
+
+ end
+
+ describe 'when a jsonp request is made wihtout a callback parameter present' do
+
+ before :each do
+ @request = Rack::MockRequest.env_for('/action.jsonp')
+ @jsonp_response = Rack::JSONP.new(@app).call(@request)
+ @jsonp_response_status, @jsonp_response_headers, @jsonp_response_body = @jsonp_response
+ end
+
+ it 'should set the response status to 400' do
+ @jsonp_response_status.should equal 400
+ end
+
+ it 'should return an empty body' do
+ @jsonp_response_body.should == []
+ end
+
+ it 'should return empty headers' do
+ @jsonp_response_headers.should == {}
+ end
+
+ end
+
+ describe 'when a non jsonp request is made' do
+
+ before :each do
+ @request = Rack::MockRequest.env_for('/action.json')
+ @jsonp_response = Rack::JSONP.new(@app).call(@request)
+ @jsonp_response_status, @jsonp_response_headers, @jsonp_response_body = @jsonp_response
+ end
+
+ it 'should not modify the response status' do
+ @jsonp_response_status.should equal @response_status
+ end
+
+ it 'should not modify the response headers' do
+ @jsonp_response_headers.should equal @response_headers
+ end
+
+ it 'should not modify the response body' do
+ @jsonp_response_body.should equal @response_body
+ end
+
+ end
+
+end
View
@@ -0,0 +1,4 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'rack'
+require 'rack/jsonp'

0 comments on commit 08dc384

Please sign in to comment.