File tree 2 files changed +43
-0
lines changed
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -14,4 +14,5 @@ group :development, :test do
14
14
gem 'rack-test' , "~> 0.6.2" , :require => "rack/test"
15
15
gem 'github-markup'
16
16
gem 'cookiejar'
17
+ gem 'rack-contrib'
17
18
end
Original file line number Diff line number Diff line change @@ -222,6 +222,48 @@ def initialize(args)
222
222
last_response . body . should == '{"example":{"name":"johnnyiller"}}'
223
223
end
224
224
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
+
225
267
end
226
268
227
269
end
You can’t perform that action at this time.
0 commit comments