How to send raw post data in a Rails functional test?

后端 未结 12 1622
感情败类
感情败类 2020-12-13 12:07

I\'m looking to send raw post data (e.g. unparamaterized JSON) to one of my controllers for testing:

class LegacyOrderUpdateControllerTest < ActionControl         


        
12条回答
  •  不思量自难忘°
    2020-12-13 12:42

    Looking at stack trace running a test you can acquire more control on request preparation: ActionDispatch::Integration::RequestHelpers.post => ActionDispatch::Integration::Session.process => Rack::Test::Session.env_for

    You can pass json string as :params AND specify a content type "application/json". In other case content type will be set to "application/x-www-form-urlencoded" and your json will be parsed properly.

    So all you need is to specify "CONTENT_TYPE":

    post :index, '{"foo":"bar", "bool":true}', "CONTENT_TYPE" => 'application/json'
    

提交回复
热议问题