Skip to content

Commit b0a8285

Browse files
committedMar 2, 2013
Added breaking test for JSONP support via Rack::JSONP (core issue is likely to be more generic and centered around middleware/formatter etc.)
1 parent 1ab26c9 commit b0a8285

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
 

‎Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ group :development, :test do
1414
gem 'rack-test', "~> 0.6.2", :require => "rack/test"
1515
gem 'github-markup'
1616
gem 'cookiejar'
17+
gem 'rack-contrib'
1718
end

‎spec/grape/entity_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,48 @@ def initialize(args)
222222
last_response.body.should == '{"example":{"name":"johnnyiller"}}'
223223
end
224224

225+
226+
it 'presents with jsonp and a custom formatter [with Rack::JSONP]' do
227+
require 'rack/contrib'
228+
229+
# Include JSONP middleware
230+
subject.use Rack::JSONP
231+
232+
# Tell rack our content-type, if it isn't application/json
233+
# then Rack::JSONP will not process it. When the formatter is fixed
234+
# and executes before Rack::JSONP we can
235+
# stop setting this directly
236+
subject.use Rack::ContentType, "application/json"
237+
238+
239+
entity = Class.new(Grape::Entity)
240+
entity.root "examples", "example"
241+
entity.expose :name
242+
243+
244+
# Rack::JSONP manages modifying the content-type and expects a
245+
# standard JSON response so we don't actually need our
246+
# custom :jsonp 'type' anymore
247+
subject.format :json
248+
249+
250+
subject.get '/example' do
251+
c = Class.new do
252+
attr_reader :name
253+
def initialize(args)
254+
@name = args[:name] || "no name set"
255+
end
256+
end
257+
258+
present c.new({:name => "johnnyiller"}), :with => entity
259+
end
260+
261+
get '/example?callback=abcDef'
262+
last_response.status.should == 200
263+
last_response.headers['Content-type'].should == "application/javascript"
264+
last_response.body.should == 'abcDef({"example":{"name":"johnnyiller"}})'
265+
end
266+
225267
end
226268

227269
end

0 commit comments

Comments
 (0)
Please sign in to comment.