Sending custom headers through RSpec

前端 未结 3 1001
一整个雨季
一整个雨季 2021-02-04 02:28

Given my API consumers are required to send a customer HTTP header like this:

# curl -H \'X-SomeHeader: 123\' http://127.0.0.1:3000/api/api_call.json
         


        
3条回答
  •  失恋的感觉
    2021-02-04 02:59

    RSpec request specs changed in Rails 5 so that custom headers and params must now be defined using key-value hash arguments. E.g.:

    Before in Rails 4:

    it "creates a Widget and redirects to the Widget's page" do
      headers = { "CONTENT_TYPE" => "application/json" }
      post "/widgets", '{ "widget": { "name":"My Widget" } }', headers
      expect(response).to redirect_to(assigns(:widget))
    end
    

    Now for Rails 5:

    it "creates a Widget and redirects to the Widget's page" do
      headers = { "CONTENT_TYPE" => "application/json" }
      post "/widgets", :params => '{ "widget": { "name":"My Widget" } }', :headers => headers
      expect(response).to redirect_to(assigns(:widget))
    end
    

提交回复
热议问题