How to set request headers in rspec request spec?

前端 未结 10 1206
独厮守ぢ
独厮守ぢ 2020-12-04 08:33

In the controller spec, I can set http accept header like this:

request.accept = \"application/json\"

but in the request spec, \"request\"

相关标签:
10条回答
  • 2020-12-04 09:08

    I have to set up headers separately

    request.headers["Accept"] = "application/json"
    

    Trying sending it via get/delete/.... is complete garbage in rails4 and causing pain in my head because it is never send as header but as parameter.

    {"Accept" => "application/json"}
    
    0 讨论(0)
  • 2020-12-04 09:09

    I used this in Test::Unit:

    @request.env['HTTP_ACCEPT'] = "*/*, application/youtube-client"
    get :index
    
    0 讨论(0)
  • 2020-12-04 09:09

    This is working for controller specs, not request specs:

    request.headers["My Header"] = "something"
    
    0 讨论(0)
  • 2020-12-04 09:10

    Your question was already answered but in case you want to POST something to another action you have to do this:

    post :save, {format: :json, application: {param1: "test", param2: "test"}}
    
    0 讨论(0)
提交回复
热议问题