rack-jsonp-tools 0.2.0 → 0.3.0
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/VERSION +1 -1
- data/lib/rack/jsonp/callback.rb +1 -1
- data/lib/rack/jsonp/status_wrapper.rb +30 -3
- data/rack-jsonp-tools.gemspec +4 -2
- data/test/callback_test.rb +2 -2
- data/test/status_wrapper_test.rb +45 -0
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/rack/jsonp/callback.rb
CHANGED
@@ -1,9 +1,36 @@
|
|
1
1
|
module Rack
|
2
2
|
module JSONP
|
3
3
|
|
4
|
-
class
|
5
|
-
|
4
|
+
class StatusWrapper
|
5
|
+
def initialize(app, callback_param = "_callback")
|
6
|
+
@app, @callback_param = app, callback_param
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
if env["jsonp.callback"]
|
11
|
+
# Call original app
|
12
|
+
status, headers, @body = @app.call(env)
|
13
|
+
|
14
|
+
# Wrap
|
15
|
+
@pre, @post = '{"body": ', ', "status": ' + status.to_s + '}'
|
16
|
+
|
17
|
+
# Fix content length if present
|
18
|
+
if content_length = headers["Content-Length"]
|
19
|
+
headers["Content-Length"] = (@pre.size + content_length.to_i + @post.size).to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
[status, headers, self]
|
23
|
+
else
|
24
|
+
@app.call(env)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def each(&block)
|
29
|
+
block.call(@pre)
|
30
|
+
@body.each(&block)
|
31
|
+
block.call(@post)
|
32
|
+
end
|
6
33
|
end
|
7
34
|
|
8
35
|
end
|
9
|
-
end
|
36
|
+
end
|
data/rack-jsonp-tools.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rack-jsonp-tools}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jacek Becela"]
|
12
|
-
s.date = %q{2010-06-
|
12
|
+
s.date = %q{2010-06-25}
|
13
13
|
s.description = %q{A collection of rack middlewares helping you add JSONP to your app}
|
14
14
|
s.email = %q{jacek.becela@gmail.com}
|
15
15
|
s.files = [
|
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
"rack-jsonp-tools.gemspec",
|
24
24
|
"test/callback_test.rb",
|
25
25
|
"test/method_override_test.rb",
|
26
|
+
"test/status_wrapper_test.rb",
|
26
27
|
"test/test_helper.rb"
|
27
28
|
]
|
28
29
|
s.homepage = %q{http://github.com/ncr/rack-jsonp-tools}
|
@@ -33,6 +34,7 @@ Gem::Specification.new do |s|
|
|
33
34
|
s.test_files = [
|
34
35
|
"test/callback_test.rb",
|
35
36
|
"test/method_override_test.rb",
|
37
|
+
"test/status_wrapper_test.rb",
|
36
38
|
"test/test_helper.rb"
|
37
39
|
]
|
38
40
|
|
data/test/callback_test.rb
CHANGED
@@ -3,7 +3,7 @@ require "rack/jsonp/callback"
|
|
3
3
|
|
4
4
|
class CallbackTest < Test::Unit::TestCase
|
5
5
|
def setup
|
6
|
-
@status =
|
6
|
+
@status = 401
|
7
7
|
@headers = { "Content-Type" => "application/json", "Content-Length" => "2" }
|
8
8
|
@body = "{}"
|
9
9
|
end
|
@@ -29,7 +29,7 @@ class CallbackTest < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
expected_body = "callback(#{@body})"
|
31
31
|
|
32
|
-
assert_equal
|
32
|
+
assert_equal 200, status
|
33
33
|
assert_equal @headers, {
|
34
34
|
"Content-Type" => "application/javascript",
|
35
35
|
"Content-Length" => expected_body.size.to_s
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "rack/jsonp/status_wrapper"
|
3
|
+
|
4
|
+
class StatusWrapperTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@status = 401
|
7
|
+
@body = "{ number: 666 }"
|
8
|
+
@headers = { "Content-Type" => "application/json", "Content-Length" => @body.size }
|
9
|
+
end
|
10
|
+
|
11
|
+
def app
|
12
|
+
app = Rack::Builder.new # instance_eval fail
|
13
|
+
app.use Rack::JSONP::Callback
|
14
|
+
app.use Rack::JSONP::StatusWrapper
|
15
|
+
app.run lambda { |env| [@status, @headers, [@body]] }
|
16
|
+
app
|
17
|
+
end
|
18
|
+
|
19
|
+
test "doesn't change anything when callback is missing" do
|
20
|
+
env = Rack::MockRequest.env_for("/")
|
21
|
+
|
22
|
+
status, headers, iterable = app.call(env)
|
23
|
+
body = ""; iterable.each { |l| body << l }
|
24
|
+
|
25
|
+
assert_equal @status, status
|
26
|
+
assert_equal @headers, headers
|
27
|
+
assert_equal @body, body
|
28
|
+
end
|
29
|
+
|
30
|
+
test "wraps response with status and updates headers" do
|
31
|
+
env = Rack::MockRequest.env_for("/?_callback=callback")
|
32
|
+
|
33
|
+
status, headers, iterable = app.call(env)
|
34
|
+
body = ""; iterable.each { |l| body << l }
|
35
|
+
|
36
|
+
expected_body = 'callback({"body": ' + @body + ', "status": ' + @status.to_s + '})'
|
37
|
+
|
38
|
+
assert_equal 200, status
|
39
|
+
assert_equal @headers, {
|
40
|
+
"Content-Type" => "application/javascript",
|
41
|
+
"Content-Length" => expected_body.size.to_s
|
42
|
+
}
|
43
|
+
assert_equal expected_body, body
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jacek Becela
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-06-
|
17
|
+
date: 2010-06-25 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- rack-jsonp-tools.gemspec
|
74
74
|
- test/callback_test.rb
|
75
75
|
- test/method_override_test.rb
|
76
|
+
- test/status_wrapper_test.rb
|
76
77
|
- test/test_helper.rb
|
77
78
|
has_rdoc: true
|
78
79
|
homepage: http://github.com/ncr/rack-jsonp-tools
|
@@ -107,4 +108,5 @@ summary: Add JSONP support to your app in less than 3 minutes! Method Override i
|
|
107
108
|
test_files:
|
108
109
|
- test/callback_test.rb
|
109
110
|
- test/method_override_test.rb
|
111
|
+
- test/status_wrapper_test.rb
|
110
112
|
- test/test_helper.rb
|