Sha256: f222b076eec4bfd77778c2b93bdcc990464d26789b6909f7d462890b60d9e947

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 KB

Contents

require 'test/spec'
require 'rack/mock'
require 'rack/contrib/jsonp'

context "Rack::JSONP" do

  context "when a callback parameter is provided" do
    specify "should wrap the response body in the Javascript callback" do
      test_body = '{"bar":"foo"}'
      callback = 'foo'
      app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
      request = Rack::MockRequest.env_for("/", :input => "foo=bar&callback=#{callback}")
      body = Rack::JSONP.new(app).call(request).last
      body.should.equal "#{callback}(#{test_body})"
    end

    specify "should modify the content length to the correct value" do
      test_body = '{"bar":"foo"}'
      callback = 'foo'
      app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [test_body]] }
      request = Rack::MockRequest.env_for("/", :input => "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
    end
  end

  specify "should not change anything if no callback param is provided" do
    app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['{"bar":"foo"}']] }
    request = Rack::MockRequest.env_for("/", :input => "foo=bar")
    body = Rack::JSONP.new(app).call(request).last
    body.join.should.equal '{"bar":"foo"}'
  end

end

Version data entries

11 entries across 11 versions & 4 rubygems

Version Path
devver-rack-contrib-0.9.7 test/spec_rack_jsonp.rb
devver-rack-contrib-0.9.6 test/spec_rack_jsonp.rb
devver-rack-contrib-0.9.5 test/spec_rack_jsonp.rb
devver-rack-contrib-0.9.4 test/spec_rack_jsonp.rb
devver-rack-contrib-0.9.3 test/spec_rack_jsonp.rb
rack-contrib_stringbot-0.9.3 test/spec_rack_jsonp.rb
tricycle-rack-contrib-0.9.7 test/spec_rack_jsonp.rb
tricycle-rack-contrib-0.9.6 test/spec_rack_jsonp.rb
tricycle-rack-contrib-0.9.5 test/spec_rack_jsonp.rb
tricycle-rack-contrib-0.9.0 test/spec_rack_jsonp.rb
rack-contrib-with-working-jsonp-0.9.2.1 test/spec_rack_jsonp.rb