How to post JSON data in rails 3 functional test -
i plan use json data in both request , response in project , having problems in testing.
after searching while, find following code uses curl post json data:
curl -h "content-type:application/json" -h "accept:application/json" \ -d '{ "foo" : "bar" }' localhost:3000/api/new in controller can access json data using params[:foo] easy. functional testing, find post , xhr (alias xml_http_request).
how can write functional test in rails achieve same effect using curl? or should test in other ways?
here's i've tried. find implementation xhr in action_controller/test_case.rb, , tried add jhr method changing 'conetent-type' , 'http_accept'. (added in test/test_helpers.rb.)
def json_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @request.env['content-type'] = 'application/json' @request.env['http_accept'] ||= [mime::json, mime::js, mime::html, mime::xml, 'text/xml', mime::all].join(', ') __send__(request_method, action, parameters, session, flash).tap @request.env.delete 'content-type' @request.env.delete 'http_accept' end end alias jhr :json_http_request i used in same way xhr, not work. inspected @response object , sees body " ".
i find one similar question on stack overflow it's rails 2 , answer posting raw data not work in rails 3.
i found want – post json controller's action.
post :create, {:format => 'json', :user => { :email => "test@test.com", :password => "foobar"}}
Comments
Post a Comment