<commit_before>require 'test/spec'
require 'rack/mock'
require 'rack/contrib/jsonp'

context "Rack::JSONP" do

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

  specify "should modify the content length to the correct value when callback is provided" 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

  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<commit_msg>Wrap callback tests in a common context<commit_after>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
{'fraction_non_alphanumeric': 0.18209067066316972, 'fraction_numerical': 0.008992131884600974, 'mean_word_length': 3.9813432835820897, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476653', 'n_tokens_mistral': 888, 'n_tokens_neox': 862, 'n_words': 224}