rack-jsonp 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -3,10 +3,6 @@ Bundler.setup
3
3
 
4
4
  require 'rake'
5
5
 
6
- lib = File.expand_path('../lib/', __FILE__)
7
- $:.unshift lib unless $:.include?(lib)
8
- require 'rack/jsonp'
9
-
10
6
  require 'spec/rake/spectask'
11
7
  Spec::Rake::SpecTask.new(:spec) do |spec|
12
8
  spec.libs << 'lib' << 'spec'
@@ -20,12 +16,3 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
20
16
  end
21
17
 
22
18
  task :default => :spec
23
-
24
- require 'rake/rdoctask'
25
- Rake::RDocTask.new do |rdoc|
26
- version = Rack::JSONP::VERSION
27
- rdoc.rdoc_dir = 'rdoc'
28
- rdoc.title = "rack-jsonp #{version}"
29
- rdoc.rdoc_files.include('README*')
30
- rdoc.rdoc_files.include('lib/**/*.rb')
31
- end
data/lib/rack/jsonp.rb CHANGED
@@ -7,8 +7,6 @@ module Rack
7
7
  #
8
8
  class JSONP
9
9
 
10
- VERSION = "1.2.0"
11
-
12
10
  def initialize(app, options = {})
13
11
  @app = app
14
12
  @carriage_return = options[:carriage_return] || false
@@ -30,12 +28,12 @@ module Rack
30
28
  status, headers, response = @app.call(env)
31
29
  if callback && headers['Content-Type'] =~ /json/i
32
30
  response = pad(callback, response)
33
- headers['Content-Length'] = response.first.length.to_s
31
+ headers['Content-Length'] = response.first.bytesize.to_s
34
32
  headers['Content-Type'] = 'application/javascript'
35
33
  elsif @carriage_return && headers['Content-Type'] =~ /json/i
36
34
  # add a \n after the response if this is a json (not JSONP) response
37
35
  response = carriage_return(response)
38
- headers['Content-Length'] = response.first.length.to_s
36
+ headers['Content-Length'] = response.first.bytesize.to_s
39
37
  end
40
38
  [status, headers, response]
41
39
  end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3
 
3
4
 
@@ -12,7 +13,7 @@ describe Rack::JSONP do
12
13
  body = Rack::JSONP.new(app).call(request).last
13
14
  body.should == ["#{callback}(#{test_body})"]
14
15
  end
15
-
16
+
16
17
  it "should wrap the response body in the Javascript callback [custom callback param]" do
17
18
  test_body = '{"bar":"foo"}'
18
19
  callback = 'foo'
@@ -45,16 +46,22 @@ describe Rack::JSONP do
45
46
  Rack::JSONP.new(app).call(request)
46
47
  app_querystring.should == "a=b&_c=saveme"
47
48
  end
48
-
49
- it "should modify the content length to the correct value" do
50
- test_body = '{"bar":"foo"}'
51
- callback = 'foo'
52
- app = lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] }
53
- request = Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}")
54
- headers = Rack::JSONP.new(app).call(request)[1]
55
- headers['Content-Length'].should == ((test_body.length + callback.length + 2).to_s) # 2 parentheses
49
+
50
+ describe "content length" do
51
+ let(:callback) {'foo'}
52
+ let(:app) { lambda { |env| [200, {'Content-Type' => 'application/json'}, [test_body]] } }
53
+ let(:request) { Rack::MockRequest.env_for("/", :params => "foo=bar&callback=#{callback}") }
54
+ subject {Rack::JSONP.new(app).call(request)[1]['Content-Length']}
55
+ context "with all single byte chars" do
56
+ let(:test_body) {'{"bar":"foo"}'}
57
+ it { should == "18" }
58
+ end
59
+ context "when the body contains an umlaut" do
60
+ let(:test_body) {'{"bär":"foo"}'}
61
+ it { should == "19" }
62
+ end
56
63
  end
57
-
64
+
58
65
  it "should change the response Content-Type to application/javascript" do
59
66
  test_body = '{"bar":"foo"}'
60
67
  callback = 'foo'
@@ -63,7 +70,7 @@ describe Rack::JSONP do
63
70
  headers = Rack::JSONP.new(app).call(request)[1]
64
71
  headers['Content-Type'].should == "application/javascript"
65
72
  end
66
-
73
+
67
74
  it "should not wrap content unless response is json" do
68
75
  test_body = '<html><body>Hello, World!</body></html>'
69
76
  callback = 'foo'
@@ -73,7 +80,7 @@ describe Rack::JSONP do
73
80
  body.should == [test_body]
74
81
  end
75
82
  end
76
-
83
+
77
84
  describe "when json content is returned" do
78
85
  it "should do nothing if no carriage return has been requested" do
79
86
  test_body = '{"bar":"foo"}'
@@ -98,7 +105,7 @@ describe Rack::JSONP do
98
105
  body.should == ["#{callback}(#{test_body})"]
99
106
  end
100
107
  end
101
-
108
+
102
109
  it "should not change anything if no callback param is provided" do
103
110
  app = lambda { |env| [200, {'Content-Type' => 'application/json'}, ['{"bar":"foo"}']] }
104
111
  request = Rack::MockRequest.env_for("/", :params => "foo=bar")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-jsonp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 0
10
- version: 1.2.0
9
+ - 1
10
+ version: 1.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Cyril Rohr
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-06 00:00:00 +02:00
18
+ date: 2011-09-29 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements: []
112
112
 
113
113
  rubyforge_project:
114
- rubygems_version: 1.5.2
114
+ rubygems_version: 1.6.2
115
115
  signing_key:
116
116
  specification_version: 3
117
117
  summary: A Rack middleware for providing JSON-P support.