Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: af709083c1
Fetching contributors…

Cannot retrieve contributors at this time

file 34 lines (29 sloc) 1.352 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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 == "#{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 == (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.should == '{"bar":"foo"}'
  end

end
Something went wrong with that request. Please try again.