Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created June 15, 2012 02:47
Show Gist options
  • Save TimFletcher/2934438 to your computer and use it in GitHub Desktop.
Save TimFletcher/2934438 to your computer and use it in GitHub Desktop.
Raw post request test
# Rails parses this correctly in the controller i.e. params['_json] exists.
curl -v -H "Content-Type: application/json" -X POST -d '[{"changed_aspect": "media","subscription_id": 1939373,"object": "user","object_id": "181943333","time": 133939219}]' http://www.monogram.dev/plugins/instagram/handle_user_subscription
class Plugins::PluginInstagramsController < Plugins::BaseController
def handle_user_subscription
if request.get?
render :text => params['hub.challenge'] and return
end
if request.post?
notifications = params['_json']
for n in notifications
logger.debug "Adding new photo: " << n.to_s
plugin = PluginInstagram.find_by_instagram_user_id(n['object_id'].to_i)
job = Delayed::Job.enqueue CachePhotoJob.new(plugin)
end
end
render :nothing => true
end
end
class Plugins::PluginInstagramsControllerTest < ActionController::TestCase
def test_handle_single_subscription_callback
# This is the actual params as parsed by Rails from a genuine Instagram
# callback. How to do a proper 'raw' post request??? I can't get params['_json']
# to exist from any kind of request.
payload = {
"_json" => [{
"changed_aspect" => "media",
"subscription_id" => 1939888,
"object" => "user",
"object_id" => "24951298",
"time" => 1339391219
}],
"plugin_instagram" => {}
}
#What I've tried
#http://stackoverflow.com/questions/2103977/how-to-send-raw-post-data-in-a-rails-functional-test
#@request.env['RAW_POST_DATA'] = payload.to_json
#@request.env['HTTP_ACCEPT'] = 'application/javascript'
#@request.accept = "text/javascript"
#post :handle_user_subscription,
# payload.as_json,
# {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
assert_difference('Delayed::Job.count') do
post(:handle_user_subscription, payload)
end
assert_response :success
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment